Пример #1
0
class TestIpoptException:
    """
    Check that an exception is thrown if Ipopt returns with return status != 0, 1, 6 (succeeded, acceptable, feasible point found)
    """
    @classmethod
    def setUpClass(cls):
        """
        Compile the test model.
        """
        # compile vdp
        fpath_vdp = os.path.join(get_files_path(), 'Modelica', 'VDP.mop')
        cpath_vdp = "VDP_pack.VDP_Opt_Min_Time"
        compile_jmu(cpath_vdp, fpath_vdp,
            compiler_options={'state_start_values_fixed':True}) 
    
    def setUp(self):
        """Test setUp. Load the test model."""   
        cpath_vdp = "VDP_pack.VDP_Opt_Min_Time"
        fname_vdp = cpath_vdp.replace('.','_',1)
        self.fname_vdp = fname_vdp   
        self.vdp = JMUModel(fname_vdp+'.jmu')

    @testattr(ipopt = True)
    def test_exception_thrown(self):
        """
        Test that an IpoptException is thrown if optimization fails.
        """
        opts = self.vdp.optimize_options()
        opts['IPOPT_options']['max_iter'] = 1
        try:
            self.vdp.optimize(options = opts)
        except IpoptException as e:
            assert str(e) == "Ipopt failed with return code: -1 Please see Ipopt documentation for more information."
        else:
            assert False, "No IpoptException thrown when optimizing fails (running VDP with max_iter = 1)."
Пример #2
0
 def test_optimize(self):
     """ Test the pyjmi.JMUModel.optimize function using all default parameters. """
     fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop')
     cpath_pend = "Pendulum_pack.Pendulum_Opt"
     jmu_pend = compile_jmu(cpath_pend, fpath_pend,compiler_options={'state_start_values_fixed':True})
     pend = JMUModel(jmu_pend)
     
     res = pend.optimize()
     
     assert N.abs(res.final('cost') - 1.2921683e-01) < 1e-3
Пример #3
0
 def test_optimize(self):
     """ Test the pyjmi.JMUModel.optimize function using all default parameters. """
     fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop')
     cpath_pend = "Pendulum_pack.Pendulum_Opt"
     jmu_pend = compile_jmu(cpath_pend, fpath_pend,compiler_options={'state_start_values_fixed':True})
     pend = JMUModel(jmu_pend)
     
     res = pend.optimize()
     
     assert N.abs(res.final('cost') - 1.2921683e-01) < 1e-3
Пример #4
0
class Test_init_ipopt:
    """ Class which contains ipopt tests for the init module. """
    
    @classmethod
    def setUpClass(cls):
        """
        Sets up the test class.
        """
        # VDP model
        fpath_vdp = os.path.join(get_files_path(),'Modelica','VDP.mop')
        cpath_vdp = "VDP_pack.VDP_Opt"
        
        cls.jmu_name = compile_jmu(cpath_vdp, fpath_vdp)
        
    def setUp(self):
        """
        Sets up the test case.
        """
        self.model_vdp = JMUModel(Test_init_ipopt.jmu_name)

    @testattr(ipopt = True)
    def test_initialize(self):
        """ Test the pyjmi.JMUModel.initialize function using all default parameters. """
        fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop')
        cpath_pend = "Pendulum_pack.Pendulum"
        
        jmu_pend = compile_jmu(cpath_pend, fpath_pend)
        pend = JMUModel(jmu_pend)
        
        res = pend.initialize()

        assert N.abs(res.final('theta') - 0.1)              < 1e-3
        assert N.abs(res.final('dtheta') - 0.)              < 1e-3
        assert N.abs(res.final('x') - 0)                    < 1e-3
        assert N.abs(res.final('dx') - 0)                   < 1e-3
        assert N.abs(res.final('der(theta)') - 0)           < 1e-3
        assert N.abs(res.final('der(dtheta)') - 0.09983341) < 1e-3
        assert N.abs(res.final('der(x)') - 0)               < 1e-3
        assert N.abs(res.final('der(dx)') - 0)              < 1e-3
        
    @testattr(ipopt = True)
    def test_initialize_with_solverargs(self):
        """ Test the pyjmi.JMUModel.initialize function using all default parameters. """
        fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop')
        cpath_pend = "Pendulum_pack.Pendulum"
        
        jmu_pend = compile_jmu(cpath_pend, fpath_pend)
        pend = JMUModel(jmu_pend)
        
        res = pend.initialize(options={'IPOPT_options':{'max_iter':1000}})

        assert N.abs(res.final('theta') - 0.1)              < 1e-3
        assert N.abs(res.final('dtheta') - 0.)              < 1e-3
        assert N.abs(res.final('x') - 0)                    < 1e-3
        assert N.abs(res.final('dx') - 0)                   < 1e-3
        assert N.abs(res.final('der(theta)') - 0)           < 1e-3
        assert N.abs(res.final('der(dtheta)') - 0.09983341) < 1e-3
        assert N.abs(res.final('der(x)') - 0)               < 1e-3
        assert N.abs(res.final('der(dx)') - 0)              < 1e-3

    @testattr(ipopt = True)
    def test_optimize(self):
        """ Test the pyjmi.JMUModel.optimize function using all default parameters. """
        fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop')
        cpath_pend = "Pendulum_pack.Pendulum_Opt"
        jmu_pend = compile_jmu(cpath_pend, fpath_pend,compiler_options={'state_start_values_fixed':True})
        pend = JMUModel(jmu_pend)
        
        res = pend.optimize()
        
        assert N.abs(res.final('cost') - 1.2921683e-01) < 1e-3
   

    @testattr(ipopt = True)
    def test_optimize_set_n_cp(self):
        """ Test the pyjmi.JMUModel.optimize function and setting n_cp in alg_args.
        """
        res = self.model_vdp.optimize(options={'n_cp':10})
        
        assert N.abs(res.final('cost') - 2.34602647e+01 ) < 1e-3
            
    @testattr(ipopt = True)
    def test_optimize_set_args(self):
        """Test the pyjmi.JMUModel.optimize function and setting some 
        algorithm and solver args.
        """
        res_file_name = 'test_optimize_set_result_mesh.txt'
        res = self.model_vdp.optimize(
            options={'result_mesh':'element_interpolation', 
                     'result_file_name':res_file_name,
                     'IPOPT_options':{'max_iter':100}})
        
        assert N.abs(res.final('cost') - 2.3469089e+01) < 1e-3


    @testattr(ipopt = True)
    def test_optimize_invalid_options(self):
        """ Test that the pyjmi.JMUModel.optimize function raises exception 
        for an invalid algorithm option.
        """
        nose.tools.assert_raises(UnrecognizedOptionError,
                                 self.model_vdp.optimize,
                                 options={'ne':10})
Пример #5
0
class Test_init_ipopt:
    """ Class which contains ipopt tests for the init module. """
    
    @classmethod
    def setUpClass(cls):
        """
        Sets up the test class.
        """
        # VDP model
        fpath_vdp = os.path.join(get_files_path(),'Modelica','VDP.mop')
        cpath_vdp = "VDP_pack.VDP_Opt"
        
        cls.jmu_name = compile_jmu(cpath_vdp, fpath_vdp)
        
    def setUp(self):
        """
        Sets up the test case.
        """
        self.model_vdp = JMUModel(Test_init_ipopt.jmu_name)

    @testattr(ipopt = True)
    def test_initialize(self):
        """ Test the pyjmi.JMUModel.initialize function using all default parameters. """
        fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop')
        cpath_pend = "Pendulum_pack.Pendulum"
        
        jmu_pend = compile_jmu(cpath_pend, fpath_pend)
        pend = JMUModel(jmu_pend)
        
        res = pend.initialize()

        assert N.abs(res.final('theta') - 0.1)              < 1e-3
        assert N.abs(res.final('dtheta') - 0.)              < 1e-3
        assert N.abs(res.final('x') - 0)                    < 1e-3
        assert N.abs(res.final('dx') - 0)                   < 1e-3
        assert N.abs(res.final('der(theta)') - 0)           < 1e-3
        assert N.abs(res.final('der(dtheta)') - 0.09983341) < 1e-3
        assert N.abs(res.final('der(x)') - 0)               < 1e-3
        assert N.abs(res.final('der(dx)') - 0)              < 1e-3
        
    @testattr(ipopt = True)
    def test_initialize_with_solverargs(self):
        """ Test the pyjmi.JMUModel.initialize function using all default parameters. """
        fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop')
        cpath_pend = "Pendulum_pack.Pendulum"
        
        jmu_pend = compile_jmu(cpath_pend, fpath_pend)
        pend = JMUModel(jmu_pend)
        
        res = pend.initialize(options={'IPOPT_options':{'max_iter':1000}})

        assert N.abs(res.final('theta') - 0.1)              < 1e-3
        assert N.abs(res.final('dtheta') - 0.)              < 1e-3
        assert N.abs(res.final('x') - 0)                    < 1e-3
        assert N.abs(res.final('dx') - 0)                   < 1e-3
        assert N.abs(res.final('der(theta)') - 0)           < 1e-3
        assert N.abs(res.final('der(dtheta)') - 0.09983341) < 1e-3
        assert N.abs(res.final('der(x)') - 0)               < 1e-3
        assert N.abs(res.final('der(dx)') - 0)              < 1e-3

    @testattr(ipopt = True)
    def test_optimize(self):
        """ Test the pyjmi.JMUModel.optimize function using all default parameters. """
        fpath_pend = os.path.join(get_files_path(), 'Modelica', 'Pendulum_pack.mop')
        cpath_pend = "Pendulum_pack.Pendulum_Opt"
        jmu_pend = compile_jmu(cpath_pend, fpath_pend,compiler_options={'state_start_values_fixed':True})
        pend = JMUModel(jmu_pend)
        
        res = pend.optimize()
        
        assert N.abs(res.final('cost') - 1.2921683e-01) < 1e-3
   

    @testattr(ipopt = True)
    def test_optimize_set_n_cp(self):
        """ Test the pyjmi.JMUModel.optimize function and setting n_cp in alg_args.
        """
        res = self.model_vdp.optimize(options={'n_cp':10})
        
        assert N.abs(res.final('cost') - 2.34602647e+01 ) < 1e-3
            
    @testattr(ipopt = True)
    def test_optimize_set_args(self):
        """Test the pyjmi.JMUModel.optimize function and setting some 
        algorithm and solver args.
        """
        res_file_name = 'test_optimize_set_result_mesh.txt'
        res = self.model_vdp.optimize(
            options={'result_mesh':'element_interpolation', 
                     'result_file_name':res_file_name,
                     'IPOPT_options':{'max_iter':100}})
        
        assert N.abs(res.final('cost') - 2.3469089e+01) < 1e-3


    @testattr(ipopt = True)
    def test_optimize_invalid_options(self):
        """ Test that the pyjmi.JMUModel.optimize function raises exception 
        for an invalid algorithm option.
        """
        nose.tools.assert_raises(UnrecognizedOptionError,
                                 self.model_vdp.optimize,
                                 options={'ne':10})