Skip to content

Commit

Permalink
#2 Fixing Github issue 13
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickHenkel committed Feb 21, 2024
1 parent b986f37 commit f4e562c
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ddmpc/controller/model_predictive/nlp.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import time
import warnings
from abc import ABC, abstractmethod
from typing import Optional, Iterator
from typing import Union

import numpy as np
import pandas as pd
from casadi import MX, inf, nlpsol, vertcat

Expand Down Expand Up @@ -670,6 +672,31 @@ def _add_objectives(self, *objectives: Objective):

self._objectives.append(NLPObjective(objective(nlp_value.mx)))

def _get_coldstart(self):

cold_start_values: list = list()
for opt_var in self._opt_vars:

if isinstance(opt_var.feature, Control):
cold_start_values.append(opt_var.feature.default)

elif isinstance(opt_var.feature, Controlled):

lb, ub = opt_var.feature.mode.bounds(0)
target = opt_var.feature.mode.target(0)

if target is not np.NAN:
cold_start_values.append(target)
elif lb is not np.NAN and ub is not np.NAN:
cold_start_values.append((lb + ub) / 2)
else:
cold_start_values.append(0)
warnings.warn(f'Mode for feature {opt_var.feature.source.name} does not provide lb, ub or target for cold start initialization, using 0 as default.')
else:
cold_start_values.append(0)
warnings.warn(f'Cold start initialization for class {opt_var.feature.source} not implemented yet, using 0 as default.')
return np.array(cold_start_values)

def summary(self):

print('------------------------- NLP SUMMARY -------------------------')
Expand Down Expand Up @@ -750,6 +777,8 @@ def solve(self, par_vals: list[float]) -> NLPSolution:
# warm start if a solution is available
if self.solution is not None and self.lastSolutionFailed is False:
nlp_instance['x0'] = self.solution.opt_vals
else:
nlp_instance['x0'] = self._get_coldstart()


# call to the solver
Expand Down

0 comments on commit f4e562c

Please sign in to comment.