예제 #1
0
 def __init__(self, model, **kwargs):
     OptimalControlProblem.__init__(self,
                                    model,
                                    name=model.name + "_stabilization",
                                    **kwargs)
     self.t_f = 10
     self.L = model.x[0]**2 + model.x[1]**2 + model.u**2
     self.x_0 = [0, 1]
예제 #2
0
 def __init__(self, model, **kwargs):
     OptimalControlProblem.__init__(self,
                                    model,
                                    name=model.name + "_stabilization",
                                    obj={
                                        "Q": DM.eye(2),
                                        "R": DM.eye(2)
                                    },
                                    x_0=[1, 1],
                                    **kwargs)
예제 #3
0
    def __init__(self, model, **kwargs):
        self.g_stochastic_ineq = DM([])
        self.g_stochastic_rhs = DM([])
        self.g_stochastic_prob = DM([])

        self.p_unc = DM([])
        self.p_unc_mean = DM([])
        self.p_unc_cov = DM([])
        self.p_unc_dist = []

        self.uncertain_initial_conditions = DM([])
        self.uncertain_initial_conditions_mean = DM([])
        self.uncertain_initial_conditions_cov = DM([])
        self.uncertain_initial_conditions_distribution = []

        OptimalControlProblem.__init__(self, model, **kwargs)
예제 #4
0
def get_ocp(model):
    # create ocp
    problem = OptimalControlProblem(model)
    problem.t_f = 10
    problem.L = mtimes(model.x.T, model.x) + model.u**2
    problem.x_0 = [0, 1]
    problem.include_time_inequality(+model.u + model.x[0], when="end")

    return problem
예제 #5
0
def create_2x2_mimo():
    a = DM([[-1, -2], [5, -1]])
    b = DM([[1, 0], [0, 1]])

    model = _create_linear_system(n_x=2, n_u=2, a=a, b=b, name="MIMO_2x2")
    problem = OptimalControlProblem(model,
                                    obj={
                                        "Q": DM.eye(2),
                                        "R": DM.eye(2)
                                    },
                                    x_0=[1, 1])
    return model, problem
예제 #6
0
def create_siso():
    a = DM([-1])
    b = DM([1])

    model = _create_linear_system(n_x=1, n_u=1, a=a, b=b, name="SISO")
    problem = OptimalControlProblem(model,
                                    obj={
                                        "Q": DM.eye(1),
                                        "R": DM.eye(1)
                                    },
                                    x_0=[1])
    return model, problem
예제 #7
0
    def get_problem(self):
        """
            Create a single OCP which is the composition of all problems and connections.

        :return: problem
        :rtype: OptimalControlProblem
        """
        model = SystemModel(name=self.name + '_model')
        problem = OptimalControlProblem(model=model,
                                        name=self.name + '_problem')
        problem.t_0 = self.problems[0].t_0
        problem.t_f = self.problems[0].t_f
        problem.merge(self.problems)
        for edge in self.graph.edges:
            problem.connect(u=self.graph.edges[edge]['u'],
                            y=self.graph.edges[edge]['y'])

        return problem
예제 #8
0
    def insert_intermediary_nodes(self):
        old_connections = list(self.connections)

        for (node1, node2) in old_connections:
            y = self.graph.edges[node1, node2]['y']
            u = self.graph.edges[node1, node2]['u']

            y_guess = vertcat(
                *node1.problem.y_guess)[find_variables_indices_in_vector(
                    y, node1.problem.model.y)]
            u_guess = vertcat(
                *node2.problem.u_guess)[find_variables_indices_in_vector(
                    u, node2.problem.model.u)]

            copy_y = vertcat(
                *
                [SX.sym('Dummy_' + y[ind].name()) for ind in range(y.numel())])
            copy_u = vertcat(
                *
                [SX.sym('Dummy_' + u[ind].name()) for ind in range(u.numel())])

            new_model = SystemModel(
                name='Dummy_Model_{}_to_{}'.format(node1.name, node2.name))
            new_model.include_variables(u=copy_y, y=copy_u)
            new_model.include_equations(alg=copy_u - copy_y)
            new_problem = OptimalControlProblem(
                name='OCP_Dummy_{}_to_{}'.format(node1.name, node2.name),
                model=new_model,
                t_f=node1.problem.t_f,
                y_guess=u_guess,
                u_guess=y_guess)

            new_node = Node(name='Dummy_node_{}_to_{}'.format(
                node1.name, node2.name),
                            model=new_model,
                            problem=new_problem,
                            color=0.75)

            self.include_nodes(new_node)

            self.remove_connection(node1, node2)
            self.connect(y, copy_y, node1, new_node)
            self.connect(copy_u, u, new_node, node2)

        for node in self.nodes:
            print(node.node_id, node.name)
예제 #9
0
from yaocptool.methods import IndirectMethod
from yaocptool.modelling import SystemModel, OptimalControlProblem

model = SystemModel()
model.create_state('x')
model.create_control('x')

model.include_equations(ode=(-model.x[0] + model.u[0]))

problem = OptimalControlProblem(model, obj={"Q": 1, "R": 1}, x_0=[1], t_f=5.0)
# problem.u_min[0] = 0
# problem.u_max[0] = 1

# problem.x_min[0] = 0.6

solution_method = IndirectMethod(
    problem,
    degree_control=3,
    discretization_scheme="multiple-shooting",
    # discretization_scheme='collocation',
    degree=3,
    finite_elements=30,
    integrator_type="implicit",
)

solution = solution_method.solve()

solution.plot([{"x": [0, 1]}, {"u": [0]}])
예제 #10
0
# create model
model = SystemModel(name="dae_system")

x = model.create_state("x", 2)
y = model.create_algebraic_variable("y", 2)
u = model.create_control("u")
a = model.create_parameter("a")
b = model.create_theta("b")

model.include_equations(
    ode=[-a * x[0] + b * y[0], -x[1] + y[1] + u[0]],
    alg=[-y[0] - x[1]**2, -y[1] - x[0]**1],
)

# 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)
예제 #11
0
sys.path.append(abspath(dirname(dirname(__file__))))

model = SystemModel()
x = model.create_state("x", 2)
u = model.create_control("u", 2)

a = DM([[-1, -2], [5, -1]])
b = DM([[1, 0], [0, 1]])

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'
예제 #12
0
ode = [
    (u - a * sqrt(2 * g * h[0])) / A,
    (a * sqrt(2 * g * h[0]) - a * sqrt(2 * g * h[1])) / A,
]

# Include the equations in the model
model.include_equations(ode=ode)

# Print the model for debugging purposes
print(model)

######################
#      OCP           #
######################
# Create the optimal control problem
problem = OptimalControlProblem(model)

# Define the problem initial conditions and final time
problem.x_0 = x_0
problem.u_guess = initial_control
problem.t_f = prediction_window

# Define the integrative cost ( cost = V(..., t_f) + \int_{t_0}^{t_f} L(...) dt) + \sum_{k} S(..., t_k)
problem.L = (h[0] - 2)**2 + 0 * (h[1] - 1)**2 + u**2

# We can change the bounds, e.g.: u_min, x_max, y_min, ...
problem.u_min = 0.001
problem.x_min = [0.05, 0.05]

# Or include other constraints (g(...) <= 0)
problem.include_time_inequality(h[0] + h[1] - 10)
예제 #13
0
    def _create_problem(self, model, sampled_parameter):
        # Problem
        problem = OptimalControlProblem(model)
        problem.name = self.socp.name + '_PCE'

        problem.p_opt = substitute(self.socp.p_opt,
                                   self.socp.get_p_without_p_unc(),
                                   problem.model.p_sym)
        problem.theta_opt = substitute(self.socp.theta_opt,
                                       self.socp.model.theta_sym,
                                       problem.model.theta_sym)

        problem.x_max = repmat(vertcat(self.socp.x_max, inf), self.n_samples)
        problem.y_max = repmat(self.socp.y_max, self.n_samples)
        problem.u_max = self.socp.u_max
        problem.delta_u_max = self.socp.delta_u_max
        problem.p_opt_max = self.socp.p_opt_max
        problem.theta_opt_max = self.socp.theta_opt_max

        problem.x_min = repmat(vertcat(self.socp.x_min, -inf), self.n_samples)
        problem.y_min = repmat(self.socp.y_min, self.n_samples)
        problem.u_min = self.socp.u_min
        problem.delta_u_min = self.socp.delta_u_min
        problem.p_opt_min = self.socp.p_opt_min
        problem.theta_opt_min = self.socp.theta_opt_min

        problem.t_f = self.socp.t_f

        if depends_on(self.socp.g_eq, self.socp.model.x_sym) or depends_on(
                self.socp.g_eq, self.socp.model.y_sym):
            raise NotImplementedError(
                'Case where "g_eq" depends on "model.x_sym" or "model.y_sym" is not implemented '
            )

        if depends_on(self.socp.g_ineq, self.socp.model.x_sym) or depends_on(
                self.socp.g_ineq, self.socp.model.y_sym):
            raise NotImplementedError(
                'Case where "g_ineq" depends on "model.x_sym" '
                'or "model.y_sym" is not implemented ')

        original_vars = vertcat(self.socp.model.u_sym,
                                self.socp.get_p_without_p_unc(),
                                self.socp.model.theta_sym,
                                self.socp.model.t_sym, self.socp.model.tau_sym)

        new_vars = vertcat(problem.model.u_sym, problem.model.p_sym,
                           problem.model.theta_sym, problem.model.t_sym,
                           problem.model.tau_sym)

        if not self.socp.n_h_initial == self.socp.model.n_x:
            problem.h_initial = vertcat(
                problem.h_initial,
                substitute(self.socp.h_initial[:self.socp.model.n_x],
                           original_vars, new_vars))
        problem.h_final = substitute(self.socp.h_final, original_vars,
                                     new_vars)

        problem.g_eq = substitute(self.socp.g_eq, original_vars, new_vars)
        problem.g_ineq = substitute(self.socp.g_ineq, original_vars, new_vars)
        problem.time_g_eq = self.socp.time_g_eq
        problem.time_g_ineq = self.socp.time_g_ineq

        for i in range(self.socp.n_uncertain_initial_condition):
            ind = self.socp.get_uncertain_initial_cond_indices()[i]
            x_ind_s = problem.model.x_0_sym[ind::(self.socp.model.n_x + 1)]
            problem.h_initial = substitute(
                problem.h_initial, x_ind_s,
                sampled_parameter[self.socp.n_p_unc + i, :].T)
            problem.h_final = substitute(
                problem.h_final, x_ind_s,
                sampled_parameter[self.socp.n_p_unc + i, :].T)

            problem.g_eq = substitute(
                problem.g_eq, x_ind_s,
                sampled_parameter[self.socp.n_p_unc + i, :].T)
            problem.g_ineq = substitute(
                problem.g_ineq, x_ind_s,
                sampled_parameter[self.socp.n_p_unc + i, :].T)

        problem.last_u = self.socp.last_u

        problem.y_guess = repmat(
            self.socp.y_guess,
            self.n_samples) if self.socp.y_guess is not None else None
        problem.u_guess = self.socp.u_guess
        problem.x_0 = repmat(
            vertcat(self.socp.x_0,
                    0), self.n_samples) if self.socp.x_0 is not None else None

        problem.parametrized_control = self.socp.parametrized_control
        problem.positive_objective = self.socp.parametrized_control
        problem.NULL_OBJ = self.socp.NULL_OBJ

        if not is_equal(self.socp.S, 0) or not is_equal(self.socp.V, 0):
            raise NotImplementedError

        return problem