Ejemplo n.º 1
0
    def create_timed_sensitivities(self, outputs, parameters, time_points):
        """
        Creates variables for output sensitivities at time points.
        
        Adds timed variables to the optimization problem for the sensitivities of
        the outputs with respect to the parameters at the given time points.

        Parameters::

            outputs --
                List of output names for which to compute time sensitivities.

            parameters --
                List of parameter names for which to compute time sensitivities.

            time_points --
                List of time points for the computed sensitivities.

        Returns::

            timed_sens --
                List of 2-dimensional arrays. timed_sens[i][j, k] contains the
                sensitivity of output j with respect to parameter k at time point
                i.
        """
        time_points = map(casadi.MX, time_points)
        sensitivities = N.array(
            [[self.getVariable('d%s/d%s' % (var, par)) for par in parameters]
             for var in outputs])
        timed_mx_vars = [
            N.array([[
                casadi.MX.sym(sens.getName() + "(%s)" % tp.getValue())
                for sens in sensitivities[i]
            ] for i in xrange(len(outputs))]) for tp in time_points
        ]
        timed_sens = []
        for i in xrange(len(time_points)):
            timed_sens_i = []
            for j in xrange(len(outputs)):
                timed_sens_ij = []
                for k in xrange(len(parameters)):
                    tv = ci.TimedVariable(self, timed_mx_vars[i][j, k],
                                          sensitivities[j, k], time_points[i])
                    self.addTimedVariable(tv)
                    timed_sens_ij.append(tv)
                timed_sens_i.append(timed_sens_ij)
            timed_sens.append(N.array(timed_sens_i))
        return timed_sens
Ejemplo n.º 2
0
 def _add_timed_variable(self, name,timePoint):
     """
     Adds a timed variable to the optimization problem object.
     
     Parameters::
         name --
             The name of the base variable.
         timePoint --
             The timepoint that the timed variable shall be for .
     
     Returns::
         par --
             The timed variable
     """
     var_symb = MX.sym("%s(%f)" %(name,timePoint))
     base_var = self.op.getVariable(name)
     timed_var= mc.TimedVariable(self.op, var_symb, base_var, MX(timePoint))
     self.op.addTimedVariable(timed_var)
     return timed_var
Ejemplo n.º 3
0
    def _add_u0(self):
        """
        Adds neccessary variables to make blocking factors (du_quad_pen and 
        du_bounds) valid first sample.
        """
        bf = self.options['blocking_factors']
        if bf is not None:
            for key in list(bf.du_quad_pen.keys()):
                var_par = casadi.MX.sym("%s_0" % key)
                var = ci.RealVariable(self.op, var_par, 2, 1)
                self.op.addVariable(var)
                self.op.set("%s_0" % key, 0)
                self.extra_param.append("%s_0" % key)

                # Find or create new timed variable
                var_startTime = self._getTimedVariable(key)

                if var_startTime is None:
                    var_startTime = casadi.MX.sym("%s(startTime)" % key)
                    st = self.op.getVariable('startTime').getVar()
                    variable = self.op.getVariable(key)
                    timedVar_startTime = ci.TimedVariable(
                        self.op, var_startTime, variable, st)
                    self.op.addTimedVariable(timedVar_startTime)

                # Create new variable
                du_quad_pen_par = casadi.MX.sym("%s_du_quad_pen" % key)
                du_quad_pen = ci.RealVariable(self.op, du_quad_pen_par, 2, 1)
                self.op.addVariable(du_quad_pen)
                self.op.set("%s_du_quad_pen" % key, 0)
                self.extra_param.append("%s_du_quad_pen" % key)

                extra_obj = du_quad_pen_par*(var_startTime-var_par)*\
                                                        (var_startTime-var_par)
                self.op.setObjective(self.op.getObjective() + extra_obj)

            # Save all pointconstraints
            pc = []
            for constr in self.op.getPointConstraints():
                pc.append(constr)

            for key in list(bf.du_bounds.keys()):
                # Find or create new _0 parameter
                var_par = self.op.getVariable("%s_0" % key)
                if var_par is None:
                    var_par = casadi.MX.sym("%s_0" % key)
                    var = ci.RealVariable(self.op, var_par, 2, 1)
                    self.op.addVariable(var)
                    self.op.set("%s_0" % key, 0)
                    self.extra_param.append("%s_0" % key)
                else:
                    var_par = var_par.getVar()

                # Find or create new timed variable
                var_startTime = self._getTimedVariable(key)

                if var_startTime is None:
                    var_startTime = casadi.MX.sym("%s(startTime)" % key)
                    st = self.op.getVariable('startTime').getVar()
                    variable = self.op.getVariable(key)
                    timedVar_startTime = ci.TimedVariable(
                        self.op, var_startTime, variable, st)
                    self.op.addTimedVariable(timedVar_startTime)

                # Create new parameter for pointconstraint bound
                du_bounds_par = casadi.MX.sym("%s_du_bounds" % key)
                du_bounds = ci.RealVariable(self.op, du_bounds_par, 2, 1)
                self.op.addVariable(du_bounds)
                self.op.set("%s_du_bounds" % key, 1e10)
                self.extra_param.append("%s_du_bounds" % key)

                # Create new pointconstraints
                bf_constr = var_startTime - var_par
                poc1 = ci.Constraint(bf_constr, du_bounds_par, 1)
                poc2 = ci.Constraint(bf_constr, -du_bounds_par, 2)

                # Append new pointconstraints to list
                pc.append(poc1)
                pc.append(poc2)

            # Set new pointconstraints
            self.op.setPointConstraints(pc)