def test_direct_collocation_polynomial_control(self): model, problem = self._create_model_and_problem() solution_method = DirectMethod(problem, degree=3, degree_control=3, finite_elements=20, discretization_scheme='collocation', nlpsol_opts=self.nlpsol_opts) result = solution_method.solve() print(result.objective_opt_problem) self.assertAlmostEqual(result.objective_opt_problem, self.answer_obj_value['direct_polynomial'], delta=self.obj_tol)
def test_direct_multiple_shooting_explicit_polynomial_control(self): model, problem = self._create_model_and_problem() solution_method = DirectMethod( problem, degree=3, degree_control=3, finite_elements=20, integrator_type='explicit', discretization_scheme='multiple-shooting', nlpsol_opts=self.nlpsol_opts) result = solution_method.solve() print(result.objective_opt_problem) self.assertAlmostEqual(result.objective_opt_problem, self.answer_obj_value['direct_polynomial'], delta=self.obj_tol)
from yaocptool.modelling import SystemModel from yaocptool.modelling import OptimalControlProblem from yaocptool.methods import DirectMethod # PART 1 model = SystemModel(name="simple_model") x = model.create_state("x") # vector of state variables u = model.create_control("u") # vector of control variables # Include the dynamic equation ode = [-x + u] model.include_equations(ode=ode) # Print model information print(model) # Part 2 problem = OptimalControlProblem(model, x_0=[1], t_f=10, obj={"Q": 1, "R": 1}) # Part 3 # Initialize a DirectMethod to solve the OCP using collocation solution_method = DirectMethod( problem, finite_elements=20, discretization_scheme="collocation" ) # Solve the problem and get the result result = solution_method.solve() # Make one plot with the element x[0] (the first state) and one plot with the control u[0] result.plot([{"x": [0]}, {"u": [0]}])
model.include_equations(ode=vertcat(mtimes(a, x) + mtimes(b, u))) problem = OptimalControlProblem( model, obj={ "Q": DM.eye(2), "R": DM.eye(2) }, x_0=[1, 1], ) problem.delta_u_max = [0.05, 0.05] problem.delta_u_min = [-0.05, -0.05] problem.last_u = [0, 0] solution_method = DirectMethod( problem, degree=3, degree_control=1, finite_elements=20, # integrator_type='implicit', # discretization_scheme = 'collocation' ) result = solution_method.solve() result.plot([{"x": [0, 1]}, {"u": [0, 1]}]) # {'x': [0]}, result = solution_method.solve(last_u=[0, 1]) result.plot([{"x": [0, 1]}, {"u": [0, 1]}]) # {'x': [0]},
) # create ocp problem = OptimalControlProblem(model) problem.t_f = 10 # problem.L = mtimes(x.T, x) + u ** 2 problem.S = mtimes(x.T, x) + u**2 + b**2 problem.x_0 = [0, 1] problem.set_theta_as_optimization_theta(b, -0.5, 0.5) # problem.include_equality(problem.p_opt + 0.25) problem.include_time_inequality(+u + x[0], when="end") # instantiate a solution method solution_method = DirectMethod( problem, discretization_scheme="collocation", degree_control=1, ) # theta = create_constant_theta(1, 1, 10) # initial_guess = solution_method.discretizer.create_initial_guess_with_simulation(p=[1]) # solution = solution_method.solve(p=[1], theta=theta, initial_guess=initial_guess) solution = solution_method.solve(p=[1], x_0=[2, 3, 0]) solution.plot([ { "x": ["x_0", "x_1"] }, { "y": ["y_0", "y_1"] },
from manual_tests.models.linear_models import MIMO2x2, StabilizationMIMO2x2 from manual_tests.models.vanderpol import VanDerPol, VanDerPolStabilization from yaocptool.methods import DirectMethod sys.path.append(abspath(dirname(dirname(__file__)))) # model = VanDerPol() # problem = VanDerPolStabilization(model) model = MIMO2x2() problem = StabilizationMIMO2x2(model) for discretization_scheme in ['collocation', 'multiple-shooting'][1:]: solution_method = DirectMethod(problem, degree=3, degree_control=3, finite_elements=20, integrator_type='implicit', discretization_scheme=discretization_scheme) result = solution_method.solve() result.plot([ # {'x': [0]}, { 'x': 'all' }, { 'u': 'all' } ]) # x, y, u, t= solution_method.plot_simulate(x_sol, u_sol, [{'x':[0]},{'x':[2,3]},{'u':[0]}], 5)
# -*- coding: utf-8 -*- """ Created on Wed Nov 02 18:57:40 2016 @author: marco """ from __future__ import print_function from manual_tests.models.twotanks import TwoTanks, StabilizationTwoTanks from yaocptool.methods import IndirectMethod, AugmentedLagrangian, DirectMethod import time model = TwoTanks() problem = StabilizationTwoTanks(model) solution_method = DirectMethod( problem, # discretization_scheme='collocation', degree=3, finite_elements=20) solution = solution_method.solve() solution.plot([{'x': 'all'}, {'y': 'all'}, {'u': 'all'}])
from tests.models import create_2x2_mimo from yaocptool.methods import DirectMethod nlpsol_opts = {"ipopt.print_level": 0, "print_time": False} # test_direct_collocation_polynomial_control model, problem = create_2x2_mimo() solution_method = DirectMethod( problem, degree=3, degree_control=3, finite_elements=20, discretization_scheme="collocation", nlpsol_opts=nlpsol_opts, ) result = solution_method.solve() print(result.objective_opt_problem)
result2 = solution_method2.solve(p=[1], x_0=[2, 3, 0, 0]) figs = result2.plot([{ 'x': [0, 1] }, { 'u': 'all' }, { 'y': [2, 3] }], figures=figs, show=True) if DIRECT: model = MIMO2x2DAE('direct') problem = StabilizationMIMO2x2WithInequality(model, t_f=10) solution_method2 = DirectMethod( problem, degree=degree, degree_control=degree_control, finite_elements=finite_elements, discretization_scheme=discretization_scheme) result2 = solution_method2.solve(p=[1], x_0=[2, 3, 0]) figs = result2.plot( [ # {'x': [0]}, { 'x': [0, 1] }, { 'u': [0] } ], figures=figs)
# Apply PCE to transform the Stochastic OCP into a 'standard' OCP pce_converter = PCEConverter(problem) # It is possible to include some expressions if we want to calculate the statistics (mean and variance) with PCE # pce_converter.stochastic_variables.append(a * sqrt(2 * g * h_1)) # pce_converter.stochastic_variables.append(h_1) # Transform the Stochastic OCP into a OCP pce_problem = pce_converter.convert_socp_to_ocp_with_pce() ###################### # Solution Method # ###################### # Initialize a DirectMethod to solve the OCP using collocation solution_method = DirectMethod(pce_problem, finite_elements=20, discretization_scheme="collocation") # Solve the problem and get the result result = solution_method.solve(p=[0.071e-2] + [0] * 6) # Make one plot with the element with h_0 and h_1 for every sample, notice that it has an additional state that is the # cost at each sample. result.plot([ { "x": range(0, 3 * pce_converter.n_samples, 3) }, { "x": range(1, 3 * pce_converter.n_samples, 3) }, {