Ejemplo n.º 1
0
    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,

            ])
Ejemplo n.º 2
0
    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,

            ])
Ejemplo n.º 3
0
    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,

            ])
Ejemplo n.º 4
0
    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,

            ])
Ejemplo n.º 5
0
    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,

            ])
Ejemplo n.º 6
0
    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,

            ])
Ejemplo n.º 7
0
    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,

            ])
Ejemplo n.º 8
0
    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,

            ])
Ejemplo n.º 9
0
    def _plot_confidence_ellipsoids(self, pdata, properties = "initial"):

        if properties == "initial":

            covariance_matrix = {"initial": self.covariance_matrix_initial}

        elif properties == "optimized":

            covariance_matrix = {"optimized": self.covariance_matrix_optimized}

        elif properties == "all":

            covariance_matrix = {"initial" : self._covariance_matrix_initial, \
                "optimized" : self._covariance_matrix_optimized}

        else:

            raise ValueError('''
Input-value not supported, choose either "initial", "final", or "all".
''')

        plotting_directory = "confidence_ellipsoids_" + \
                os.path.basename(__main__.__file__).strip(".py")

        try:
            os.mkdir(plotting_directory)

        except OSError:
            if not os.path.isdir(plotting_directory):

                raise OSError('''
Plotting directory "confidence_ellipsoids_{0}"
does not yet exist, but could not be created.

Do you have write access within your working folder, or is
some file with this name already present within your working folder?
'''.format(plotting_directory))

        xy = np.array([np.cos(np.linspace(0, 2*np.pi, 100)), 
            np.sin(np.linspace(0, 2*np.pi, 100))])

        for p1 in range(pdata.size):

            for p2 in range(pdata.size)[p1+1:]:

                plt.figure()

                for prop, cm in covariance_matrix.iteritems():

                    covariance_matrix_p1p2 = np.array([ \

                            [cm[p1, p1], cm[p1, p2]], \
                            [cm[p2, p1], cm[p2, p2]]

                        ])

                    w, v = np.linalg.eig(covariance_matrix_p1p2)

                    ellipsoid = ci.repmat(np.array([pdata[p1], pdata[p2]]), 1, 100) + \
                        ci.mul([v, ci.diag(w), xy])

                    plt.plot(ellipsoid[0,:].T, ellipsoid[1,:].T, label = \
                        "p_" + str(p1) + ", p_" + str(p2) + " " + prop)
                
                plt.scatter(pdata[p1], pdata[p2], color = "k")    
                plt.legend(loc="upper right")                
                plt.savefig(plotting_directory + "/p_" + str(p1) + \
                    "-p_" + str(p2) + "-" + properties + ".png", bbox_inches='tight')
                plt.close()
Ejemplo n.º 10
0
    def _plot_confidence_ellipsoids(self, pdata, properties = "initial"):

        if properties == "initial":

            covariance_matrix = {"initial": self.covariance_matrix_initial}

        elif properties == "optimized":

            covariance_matrix = {"optimized": self.covariance_matrix_optimized}

        elif properties == "all":

            covariance_matrix = {"initial" : self._covariance_matrix_initial, \
                "optimized" : self._covariance_matrix_optimized}

        else:

            raise ValueError('''
Input-value not supported, choose either "initial", "final", or "all".
''')

        plotting_directory = "confidence_ellipsoids_" + \
                os.path.basename(__main__.__file__).strip(".py")

        try:
            os.mkdir(plotting_directory)

        except OSError:
            if not os.path.isdir(plotting_directory):

                raise OSError('''
Plotting directory "confidence_ellipsoids_{0}"
does not yet exist, but could not be created.

Do you have write access within your working folder, or is
some file with this name already present within your working folder?
'''.format(plotting_directory))

        xy = np.array([np.cos(np.linspace(0, 2*np.pi, 100)), 
            np.sin(np.linspace(0, 2*np.pi, 100))])

        for p1 in range(pdata.size):

            for p2 in range(pdata.size)[p1+1:]:

                plt.figure()

                for prop, cm in covariance_matrix.iteritems():

                    covariance_matrix_p1p2 = np.array([ \

                            [cm[p1, p1], cm[p1, p2]], \
                            [cm[p2, p1], cm[p2, p2]]

                        ])

                    w, v = np.linalg.eig(covariance_matrix_p1p2)

                    ellipsoid = ci.repmat(np.array([pdata[p1], pdata[p2]]), 1, 100) + \
                        ci.mul([v, ci.diag(w), xy])

                    plt.plot(ellipsoid[0,:].T, ellipsoid[1,:].T, label = \
                        "p_" + str(p1) + ", p_" + str(p2) + " " + prop)
                
                plt.scatter(pdata[p1], pdata[p2], color = "k")    
                plt.legend(loc="upper right")                
                plt.savefig(plotting_directory + "/p_" + str(p1) + \
                    "-p_" + str(p2) + "-" + properties + ".png", bbox_inches='tight')
                plt.close()