def _apply_controls_to_measurements(self, udata, qdata): udata = inputchecks.check_controls_data(udata, \ self._discretization.system.nu, \ self._discretization.number_of_controls) qdata = inputchecks.check_constant_controls_data(qdata, \ self._discretization.system.nq) optimization_variables_for_measurements = ci.veccat([ \ self._discretization.optimization_variables["U"], self._discretization.optimization_variables["Q"], self._discretization.optimization_variables["X"], self._discretization.optimization_variables["EPS_U"], self._discretization.optimization_variables["P"], ]) optimization_variables_controls_applied = ci.veccat([ \ udata, qdata, self._discretization.optimization_variables["X"], self._discretization.optimization_variables["EPS_U"], self._discretization.optimization_variables["P"], ]) measurements_fcn = ci.mx_function( \ "measurements_fcn", \ [optimization_variables_for_measurements], \ [self._discretization.measurements]) [self._measurements_controls_applied] = \ measurements_fcn([optimization_variables_controls_applied])
def _apply_controls_to_measurements(self, udata, qdata): udata = inputchecks.check_controls_data(udata, \ self._discretization.system.nu, \ self._discretization.number_of_controls) qdata = inputchecks.check_constant_controls_data(qdata, \ self._discretization.system.nq) optimization_variables_for_measurements = ci.veccat([ \ self._discretization.optimization_variables["U"], self._discretization.optimization_variables["Q"], self._discretization.optimization_variables["X"], self._discretization.optimization_variables["EPS_U"], self._discretization.optimization_variables["P"], ]) optimization_variables_controls_applied = ci.veccat([ \ udata, qdata, self._discretization.optimization_variables["X"], self._discretization.optimization_variables["EPS_U"], self._discretization.optimization_variables["P"], ]) measurements_fcn = ci.mx_function( \ "measurements_fcn", \ [optimization_variables_for_measurements], \ [self._discretization.measurements]) self._measurements_controls_applied = \ measurements_fcn(optimization_variables_controls_applied)
def _apply_parameters_to_objective(self): # As mentioned above, the objective does not depend on the actual # values of V, but on the values of P and EPS_U, while # P is fed from pdata, and EPS_U is supposed to be 0 objective_free_variables = ci.veccat([ \ self._discretization.optimization_variables["P"], self._discretization.optimization_variables["U"], self._discretization.optimization_variables["Q"], self._discretization.optimization_variables["X"], self._discretization.optimization_variables["EPS_U"], ]) objective_free_variables_parameters_applied = ci.veccat([ \ self._pdata, self._discretization.optimization_variables["U"], self._discretization.optimization_variables["Q"], self._discretization.optimization_variables["X"], ci.mx(*self._discretization.optimization_variables["EPS_U"].shape), ]) objective_fcn = ci.mx_function("objective_fcn", \ [objective_free_variables], [self._objective_parameters_free]) self._objective = objective_fcn( \ objective_free_variables_parameters_applied)
def _apply_parameters_to_equality_constraints(self): optimization_variables_for_equality_constraints = ci.veccat([ \ self._discretization.optimization_variables["U"], self._discretization.optimization_variables["Q"], self._discretization.optimization_variables["X"], self._discretization.optimization_variables["EPS_U"], self._discretization.optimization_variables["P"], ]) optimization_variables_parameters_applied = ci.veccat([ \ self._discretization.optimization_variables["U"], self._discretization.optimization_variables["Q"], self._discretization.optimization_variables["X"], ci.mx(*self._discretization.optimization_variables["EPS_U"].shape), self._pdata, ]) equality_constraints_fcn = ci.mx_function( \ "equality_constraints_fcn", \ [optimization_variables_for_equality_constraints], \ [self._discretization.equality_constraints]) [self._equality_constraints_parameters_applied] = \ equality_constraints_fcn([optimization_variables_parameters_applied])
def _apply_parameters_to_objective(self): objective_free_variables = [] objective_free_variables_parameters_applied = [] for doe_setup in self._doe_setups: objective_free_variables.append( \ doe_setup._discretization.optimization_variables["P"]) objective_free_variables_parameters_applied.append( \ doe_setup._pdata) for key in ["U", "Q", "X"]: objective_free_variables.append( \ doe_setup._discretization.optimization_variables[key]) objective_free_variables_parameters_applied.append( \ doe_setup._discretization.optimization_variables[key]) objective_free_variables = ci.veccat(objective_free_variables) objective_free_variables_parameters_applied = \ ci.veccat(objective_free_variables_parameters_applied) objective_fcn = ci.mx_function("objective_fcn", \ [objective_free_variables], [self._objective_parameters_free]) [self._objective] = objective_fcn( \ [objective_free_variables_parameters_applied])
def _apply_parameters_to_equality_constraints(self): optimization_variables_for_equality_constraints = ci.veccat([ \ self._discretization.optimization_variables["U"], self._discretization.optimization_variables["Q"], self._discretization.optimization_variables["X"], self._discretization.optimization_variables["EPS_U"], self._discretization.optimization_variables["P"], ]) optimization_variables_parameters_applied = ci.veccat([ \ self._discretization.optimization_variables["U"], self._discretization.optimization_variables["Q"], self._discretization.optimization_variables["X"], ci.mx(*self._discretization.optimization_variables["EPS_U"].shape), self._pdata, ]) equality_constraints_fcn = ci.mx_function( \ "equality_constraints_fcn", \ [optimization_variables_for_equality_constraints], \ [self._discretization.equality_constraints]) self._equality_constraints_parameters_applied = \ equality_constraints_fcn(optimization_variables_parameters_applied)
def _apply_parameters_to_objective(self): # As mentioned above, the objective does not depend on the actual # values of V, but on the values of P and EPS_E and EPS_U, while # P is fed from pdata, and EPS_E, EPS_u are supposed to be 0 objective_free_variables = ci.veccat([ \ self._discretization.optimization_variables["P"], self._discretization.optimization_variables["U"], self._discretization.optimization_variables["Q"], self._discretization.optimization_variables["X"], self._discretization.optimization_variables["EPS_U"], ]) objective_free_variables_parameters_applied = ci.veccat([ \ self._pdata, self._discretization.optimization_variables["U"], self._discretization.optimization_variables["Q"], self._discretization.optimization_variables["X"], ci.mx(*self._discretization.optimization_variables["EPS_U"].shape), ]) objective_fcn = ci.mx_function("objective_fcn", \ [objective_free_variables], [self._objective_parameters_free]) [self._objective] = objective_fcn( \ [objective_free_variables_parameters_applied])
def _apply_parameters_to_objective(self): objective_free_variables = [] objective_free_variables_parameters_applied = [] for doe_setup in self._doe_setups: objective_free_variables.append( \ doe_setup._discretization.optimization_variables["P"]) objective_free_variables_parameters_applied.append( \ doe_setup._pdata) for key in ["U", "Q", "X"]: objective_free_variables.append( \ doe_setup._discretization.optimization_variables[key]) objective_free_variables_parameters_applied.append( \ doe_setup._discretization.optimization_variables[key]) objective_free_variables = ci.veccat(objective_free_variables) objective_free_variables_parameters_applied = \ ci.veccat(objective_free_variables_parameters_applied) objective_fcn = ci.mx_function("objective_fcn", \ [objective_free_variables], [self._objective_parameters_free]) self._objective = objective_fcn( \ objective_free_variables_parameters_applied)
def _set_optimization_variables_initials(self, pinit, xinit): xinit = inputchecks.check_states_data(xinit, \ self._discretization.system.nx, \ self._discretization.number_of_intervals) repretitions_xinit = \ self._discretization.optimization_variables["X"][:,:-1].shape[1] / \ self._discretization.number_of_intervals Xinit = ci.repmat(xinit[:, :-1], repretitions_xinit, 1) Xinit = ci.horzcat([ \ Xinit.reshape((self._discretization.system.nx, \ Xinit.size() / self._discretization.system.nx)), xinit[:, -1], ]) pinit = inputchecks.check_parameter_data(pinit, \ self._discretization.system.np) Pinit = pinit Vinit = np.zeros(self._discretization.optimization_variables["V"].shape) EPS_Uinit = np.zeros( \ self._discretization.optimization_variables["EPS_U"].shape) self._optimization_variables_initials = ci.veccat([ \ Pinit, Xinit, EPS_Uinit, Vinit, ])
def _merge_equaltiy_constraints_parameters_applied(self): equality_constraints_parameters_applied = [ \ doe_setup._equality_constraints_parameters_applied for \ doe_setup in self._doe_setups] self._equality_constraints_parameters_applied = \ ci.veccat(equality_constraints_parameters_applied)
def _setup_residuals(self): self._residuals = ci.sqrt(self._weightings_vectorized) * \ ci.veccat([ \ self._discretization.optimization_variables["EPS_U"], self._discretization.optimization_variables["V"], ])
def _setup_residuals(self): self._residuals = ci.sqrt(self._weightings_vectorized) * \ ci.veccat([ \ self._discretization.optimization_variables["V"], self._discretization.optimization_variables["EPS_U"], ])
def _set_optimization_variables(self): self._optimization_variables = ci.veccat([ \ self._discretization.optimization_variables["U"], self._discretization.optimization_variables["Q"], self._discretization.optimization_variables["X"], ])
def _compute_optimized_covariance_matrix(self): covariance_matrix_optimized_input = ci.veccat([ \ self._pdata, self.design_results["x"], np.zeros(self._discretization.optimization_variables["EPS_U"].shape) ]) self._covariance_matrix_optimized = self._covariance_matrix_fcn( \ covariance_matrix_optimized_input)
def _compute_optimized_covariance_matrix(self): covariance_matrix_optimized_input = ci.veccat([ \ self._pdata, self.design_results["x"], np.zeros(self._discretization.optimization_variables["EPS_U"].shape) ]) self._covariance_matrix_optimized = self._covariance_matrix_fcn( \ [covariance_matrix_optimized_input])[0]
def _compute_initial_covariance_matrix(self): covariance_matrix_initial_input = ci.veccat([ \ self._pdata, self._optimization_variables_initials, np.zeros(self._discretization.optimization_variables["EPS_U"].shape) ]) self._covariance_matrix_initial = self._covariance_matrix_fcn( \ [covariance_matrix_initial_input])[0]
def _compute_initial_covariance_matrix(self): covariance_matrix_initial_input = ci.veccat([ \ self._pdata, self._optimization_variables_initials, np.zeros(self._discretization.optimization_variables["EPS_U"].shape) ]) self._covariance_matrix_initial = self._covariance_matrix_fcn( \ covariance_matrix_initial_input)
def _set_optimization_variables_lower_bounds(self, umin, qmin, xmin, x0): umin_user_provided = umin umin = inputchecks.check_controls_data(umin, \ self._discretization.system.nu, 1) if umin_user_provided is None: umin = -np.inf * np.ones(umin.shape) Umin = ci.repmat(umin, 1, \ self._discretization.optimization_variables["U"].shape[1]) qmin_user_provided = qmin qmin = inputchecks.check_constant_controls_data(qmin, \ self._discretization.system.nq) if qmin_user_provided is None: qmin = -np.inf * np.ones(qmin.shape) Qmin = qmin xmin_user_provided = xmin xmin = inputchecks.check_states_data(xmin, \ self._discretization.system.nx, 0) if xmin_user_provided is None: xmin = -np.inf * np.ones(xmin.shape) Xmin = ci.repmat(xmin, 1, \ self._discretization.optimization_variables["X"].shape[1]) Xmin[:,0] = x0 self._optimization_variables_lower_bounds = ci.veccat([ \ Umin, Qmin, Xmin, ])
def _set_cov_matrix_derivative_directions(self): # These correspond to the optimization variables of the parameter # estimation problem; the evaluation of the covariance matrix, though, # does not depend on the actual values of V, EPS_E and EPS_U, and with # this, the DoE problem does not self._cov_matrix_derivative_directions = ci.veccat([ \ self._discretization.optimization_variables["P"], self._discretization.optimization_variables["X"], self._discretization.optimization_variables["V"], self._discretization.optimization_variables["EPS_U"], ])
def _set_cov_matrix_derivative_directions(self): # These correspond to the optimization variables of the parameter # estimation problem; the evaluation of the covariance matrix, though, # does not depend on the actual values of V, EPS_E and EPS_U, and with # this, the DoE problem does not self._cov_matrix_derivative_directions = ci.veccat([ \ self._discretization.optimization_variables["P"], self._discretization.optimization_variables["X"], self._discretization.optimization_variables["EPS_U"], self._discretization.optimization_variables["V"], ])
def _setup_covariance_matrix_for_evaluation(self): covariance_matrix_free_variables = ci.veccat([ \ self._discretization.optimization_variables["P"], self._discretization.optimization_variables["U"], self._discretization.optimization_variables["Q"], self._discretization.optimization_variables["X"], self._discretization.optimization_variables["EPS_U"], ]) self._covariance_matrix_fcn = ci.mx_function("covariance_matrix_fcn", \ [covariance_matrix_free_variables], \ [self._covariance_matrix.covariance_matrix])
def _set_optimization_variables_upper_bounds(self, umax, qmax, xmax, x0): umax_user_provided = umax umax = inputchecks.check_controls_data(umax, \ self._discretization.system.nu, 1) if umax_user_provided is None: umax = np.inf * np.ones(umax.shape) Umax = ci.repmat(umax, 1, \ self._discretization.optimization_variables["U"].shape[1]) qmax_user_provided = qmax qmax = inputchecks.check_constant_controls_data(qmax, \ self._discretization.system.nq) if qmax_user_provided is None: qmax = -np.inf * np.ones(qmax.shape) Qmax = qmax xmax_user_provided = xmax xmax = inputchecks.check_states_data(xmax, \ self._discretization.system.nx, 0) if xmax_user_provided is None: xmax = np.inf * np.ones(xmax.shape) Xmax = ci.repmat(xmax, 1, \ self._discretization.optimization_variables["X"].shape[1]) Xmax[:,0] = x0 self._optimization_variables_upper_bounds = ci.veccat([ \ Umax, Xmax, ])
def _compute_initial_covariance_matrix(self): covariance_matrix_initial_input = [] for doe_setup in self._doe_setups: covariance_matrix_initial_input.append(doe_setup._pdata) covariance_matrix_initial_input.append( \ doe_setup._optimization_variables_initials) covariance_matrix_initial_input = ci.veccat( \ covariance_matrix_initial_input) self._covariance_matrix_initial = self._covariance_matrix_fcn( \ [covariance_matrix_initial_input])[0]
def _compute_initial_covariance_matrix(self): covariance_matrix_initial_input = [] for doe_setup in self._doe_setups: covariance_matrix_initial_input.append(doe_setup._pdata) covariance_matrix_initial_input.append( \ doe_setup._optimization_variables_initials) covariance_matrix_initial_input = ci.veccat( \ covariance_matrix_initial_input) self._covariance_matrix_initial = self._covariance_matrix_fcn( \ covariance_matrix_initial_input)
def _setup_covariance_matrix_for_evaluation(self): covariance_matrix_free_variables = [] for doe_setup in self._doe_setups: for key in ["P", "U", "Q", "X"]: covariance_matrix_free_variables.append( \ doe_setup._discretization.optimization_variables[key]) covariance_matrix_free_variables = ci.veccat( \ covariance_matrix_free_variables) self._covariance_matrix_fcn = ci.mx_function("covariance_matrix_fcn", \ [covariance_matrix_free_variables], \ [self._covariance_matrix.covariance_matrix])
def _set_weightings(self, wv, weps_u): input_error_weightings = \ inputchecks.check_input_error_weightings(weps_u, \ self._discretization.system.neps_u, self._discretization.number_of_intervals) measurement_weightings = \ inputchecks.check_measurement_weightings(wv, \ self._discretization.system.nphi, \ self._discretization.number_of_intervals + 1) self._weightings_vectorized = ci.veccat([ \ input_error_weightings, measurement_weightings, ])
def _set_weightings(self, wv, weps_u): input_error_weightings = \ inputchecks.check_input_error_weightings(weps_u, \ self._discretization.system.neps_u, \ self._discretization.number_of_intervals) measurement_weightings = \ inputchecks.check_measurement_weightings(wv, \ self._discretization.system.nphi, \ self._discretization.number_of_intervals + 1) self._weightings_vectorized = ci.veccat([ \ input_error_weightings, measurement_weightings, ])
def _set_optimization_variables_initials(self, qinit, x0, uinit): self.simulation = Simulation(self._discretization.system, \ self._pdata, qinit) self.simulation.run_system_simulation(x0, \ self._discretization.time_points, uinit, print_status = False) xinit = self.simulation.simulation_results repretitions_xinit = \ self._discretization.optimization_variables["X"][:,:-1].shape[1] / \ self._discretization.number_of_intervals Xinit = ci.repmat(xinit[:, :-1], repretitions_xinit, 1) Xinit = ci.horzcat([ \ Xinit.reshape((self._discretization.system.nx, \ Xinit.size() / self._discretization.system.nx)), xinit[:, -1], ]) uinit = inputchecks.check_controls_data(uinit, \ self._discretization.system.nu, \ self._discretization.number_of_intervals) Uinit = uinit qinit = inputchecks.check_constant_controls_data(qinit, \ self._discretization.system.nq) Qinit = qinit self._optimization_variables_initials = ci.veccat([ \ Uinit, Qinit, Xinit, ])
def _set_optimization_variables_initials(self, qinit, x0, uinit): self.simulation = Simulation(self._discretization.system, \ self._pdata, qinit) self.simulation.run_system_simulation(x0, \ self._discretization.time_points, uinit, print_status = False) xinit = self.simulation.simulation_results repretitions_xinit = \ self._discretization.optimization_variables["X"][:,:-1].shape[1] / \ self._discretization.number_of_intervals Xinit = ci.repmat(xinit[:, :-1], repretitions_xinit, 1) Xinit = ci.horzcat([ \ Xinit.reshape((self._discretization.system.nx, \ Xinit.numel() / self._discretization.system.nx)), xinit[:, -1], ]) uinit = inputchecks.check_controls_data(uinit, \ self._discretization.system.nu, \ self._discretization.number_of_intervals) Uinit = uinit qinit = inputchecks.check_constant_controls_data(qinit, \ self._discretization.system.nq) Qinit = qinit self._optimization_variables_initials = ci.veccat([ \ Uinit, Qinit, Xinit, ])
def _compute_optimized_covariance_matrix(self): starting_position_design_results = 0 covariance_matrix_optimized_input = [] for doe_setup in self._doe_setups: covariance_matrix_optimized_input.append(doe_setup._pdata) covariance_matrix_optimized_input.append(self.design_results["x"][ \ starting_position_design_results : \ starting_position_design_results + \ doe_setup._optimization_variables.shape[0]]) starting_position_design_results += \ doe_setup._optimization_variables.shape[0] covariance_matrix_optimized_input = ci.veccat( \ covariance_matrix_optimized_input) self._covariance_matrix_optimized = self._covariance_matrix_fcn( \ [covariance_matrix_optimized_input])[0]
def _compute_optimized_covariance_matrix(self): starting_position_design_results = 0 covariance_matrix_optimized_input = [] for doe_setup in self._doe_setups: covariance_matrix_optimized_input.append(doe_setup._pdata) covariance_matrix_optimized_input.append(self.design_results["x"][ \ starting_position_design_results : \ starting_position_design_results + \ doe_setup._optimization_variables.shape[0]]) starting_position_design_results += \ doe_setup._optimization_variables.shape[0] covariance_matrix_optimized_input = ci.veccat( \ covariance_matrix_optimized_input) self._covariance_matrix_optimized = self._covariance_matrix_fcn( \ covariance_matrix_optimized_input)
def _set_optimization_variables_initials(self, pinit, xinit): xinit = inputchecks.check_states_data(xinit, \ self._discretization.system.nx, \ self._discretization.number_of_intervals) repretitions_xinit = \ self._discretization.optimization_variables["X"][:,:-1].shape[1] / \ self._discretization.number_of_intervals Xinit = ci.repmat(xinit[:, :-1], repretitions_xinit, 1) Xinit = ci.horzcat([ \ Xinit.reshape((self._discretization.system.nx, \ Xinit.numel() / self._discretization.system.nx)), xinit[:, -1], ]) pinit = inputchecks.check_parameter_data(pinit, \ self._discretization.system.np) Pinit = pinit Vinit = np.zeros( self._discretization.optimization_variables["V"].shape) EPS_Uinit = np.zeros( \ self._discretization.optimization_variables["EPS_U"].shape) self._optimization_variables_initials = ci.veccat([ \ Pinit, Xinit, Vinit, EPS_Uinit, ])