コード例 #1
0
    def test_load_local_asl_library(self):
        DLL = find_GSL()
        if not DLL:
            self.skipTest("Could not find the amplgsl.dll library")

        LIB = 'test_pyomo_external_gsl.dll'

        model = ConcreteModel()
        model.gamma = ExternalFunction(library=LIB, function="gsl_sf_gamma")
        model.x = Var(initialize=3, bounds=(1e-5, None))
        model.o = Objective(expr=model.gamma(model.x))

        with TempfileManager.new_context() as tempfile:
            dname = tempfile.mkdtemp()
            shutil.copyfile(DLL, os.path.join(dname, LIB))
            # Without changing directories, the load should fail
            with self.assertRaises(OSError):
                value(model.o)
            # Changing directories should pick up the library
            try:
                orig_dir = os.getcwd()
                os.chdir(dname)
                self.assertAlmostEqual(value(model.o), 2.0, 7)
            finally:
                os.chdir(orig_dir)
コード例 #2
0
 def test_capture_output_logfile_string(self):
     with TempfileManager.new_context() as tempfile:
         logfile = tempfile.create_tempfile()
         self.assertTrue(isinstance(logfile, str))
         with tee.capture_output(logfile):
             print('HELLO WORLD')
         with open(logfile, 'r') as f:
             result = f.read()
         self.assertEqual('HELLO WORLD\n', result)
コード例 #3
0
    def _get_files(self):
        """
        Method to get/set paths for .alm and .trc files based on filename
        configuration argument.

        If filename is None, temporary files will be created.

        Args:
            None

        Returns:
            None
        """
        if self._temp_context is None:
            self._temp_context = TempfileManager.new_context()

        if self.config.filename is None:
            # Get a temporary file from the manager
            almfile = self._temp_context.create_tempfile(suffix=".alm")
        else:
            almfile = self.config.filename

            if not self.config.overwrite_files:
                # It is OK if the trace file exists, as ALAMO will append to it
                # The trace file reader handles this case
                if os.path.isfile(almfile):
                    raise FileExistsError(
                        f"A file with the name {almfile} already exists. "
                        f"Either choose a new file name or set "
                        f"overwrite_files = True.")

            self._temp_context.add_tempfile(almfile, exists=False)

        trcfile = os.path.splitext(almfile)[0] + ".trc"
        self._temp_context.add_tempfile(trcfile, exists=False)

        if self.config.working_directory is None:
            wrkdir = self._temp_context.create_tempdir()
        else:
            wrkdir = self.config.working_directory

        # Set attributes to track file names
        self._almfile = almfile
        self._trcfile = trcfile
        self._wrkdir = wrkdir
コード例 #4
0
    def test_call_alamo_w_input(self, alamo_trainer):
        cwd = os.getcwd()

        alamo_trainer._temp_context = TempfileManager.new_context()
        alamo_trainer._almfile = os.path.join(dirpath, "alamo_test.alm")
        alamo_trainer._wrkdir = dirpath

        alamo_trainer._temp_context.add_tempfile(
            os.path.join(dirpath, "GUI_trace.trc"), exists=False)
        alamo_trainer._temp_context.add_tempfile(
            os.path.join(dirpath, "alamo_test.lst"), exists=False)
        rc, almlog = alamo_trainer._call_alamo()
        assert rc == 0
        assert "Normal termination" in almlog

        # Check for clean up
        alamo_trainer._temp_context.release(remove=True)
        assert not os.path.exists(os.path.join(dirpath, "GUI_trace.trc"))
        assert not os.path.exists(os.path.join(dirpath, "alamo_test.lst"))
        assert cwd == os.getcwd()
コード例 #5
0
    def test_obj_con_cache(self):
        model = ConcreteModel()
        model.x = Var()
        model.c = Constraint(expr=model.x >= 1)
        model.obj = Objective(expr=model.x * 2)

        with TempfileManager.new_context() as TMP:
            lp_file = TMP.create_tempfile(suffix='.lp')
            model.write(lp_file, format='lp')
            self.assertFalse(hasattr(model, '_repn'))
            with open(lp_file) as FILE:
                lp_ref = FILE.read()

            lp_file = TMP.create_tempfile(suffix='.lp')
            model._gen_obj_repn = True
            model.write(lp_file)
            self.assertEqual(len(model._repn), 1)
            self.assertIn(model.obj, model._repn)
            obj_repn = model._repn[model.obj]
            with open(lp_file) as FILE:
                lp_test = FILE.read()
            self.assertEqual(lp_ref, lp_test)

            lp_file = TMP.create_tempfile(suffix='.lp')
            model._gen_obj_repn = None
            model._gen_con_repn = True
            model.write(lp_file)
            self.assertEqual(len(model._repn), 2)
            self.assertIn(model.obj, model._repn)
            self.assertIn(model.c, model._repn)
            self.assertIs(obj_repn, model._repn[model.obj])
            obj_repn = model._repn[model.obj]
            c_repn = model._repn[model.c]
            with open(lp_file) as FILE:
                lp_test = FILE.read()
            self.assertEqual(lp_ref, lp_test)

            lp_file = TMP.create_tempfile(suffix='.lp')
            model._gen_obj_repn = None
            model._gen_con_repn = None
            model.write(lp_file)
            self.assertEqual(len(model._repn), 2)
            self.assertIn(model.obj, model._repn)
            self.assertIn(model.c, model._repn)
            self.assertIs(obj_repn, model._repn[model.obj])
            self.assertIs(c_repn, model._repn[model.c])
            with open(lp_file) as FILE:
                lp_test = FILE.read()
            self.assertEqual(lp_ref, lp_test)

            lp_file = TMP.create_tempfile(suffix='.lp')
            model._gen_obj_repn = True
            model._gen_con_repn = True
            model.write(lp_file)
            self.assertEqual(len(model._repn), 2)
            self.assertIn(model.obj, model._repn)
            self.assertIn(model.c, model._repn)
            self.assertIsNot(obj_repn, model._repn[model.obj])
            self.assertIsNot(c_repn, model._repn[model.c])
            obj_repn = model._repn[model.obj]
            c_repn = model._repn[model.c]
            with open(lp_file) as FILE:
                lp_test = FILE.read()
            self.assertEqual(lp_ref, lp_test)

            lp_file = TMP.create_tempfile(suffix='.lp')
            model._gen_obj_repn = False
            model._gen_con_repn = False
            import pyomo.repn.plugins.ampl.ampl_ as ampl_
            gsr = ampl_.generate_standard_repn
            try:

                def dont_call_gsr(*args, **kwargs):
                    self.fail("generate_standard_repn should not be called")

                ampl_.generate_standard_repn = dont_call_gsr
                model.write(lp_file)
            finally:
                ampl_.generate_standard_repn = gsr
            self.assertEqual(len(model._repn), 2)
            self.assertIn(model.obj, model._repn)
            self.assertIn(model.c, model._repn)
            self.assertIs(obj_repn, model._repn[model.obj])
            self.assertIs(c_repn, model._repn[model.c])
            with open(lp_file) as FILE:
                lp_test = FILE.read()
            self.assertEqual(lp_ref, lp_test)
コード例 #6
0
def test_save_load():
    keras_surrogate = create_keras_model(name='PT_data_2_10_10_2_sigmoid',
                                         return_keras_model_only=False)

    new_keras_surrogate = None
    dname = None
    with TempfileManager.new_context() as tf:
        dname = tf.mkdtemp()
        keras_surrogate.save_to_folder(dname)
        assert os.path.isdir(dname)
        assert os.path.isfile(os.path.join(dname, 'idaes_info.json'))

        new_keras_surrogate = KerasSurrogate.load_from_folder(dname)

    # Check for clean up
    assert not os.path.isdir(dname)

    # check surrogate data members
    assert new_keras_surrogate._input_labels == [
        'Temperature_K', 'Pressure_Pa'
    ]
    assert new_keras_surrogate._output_labels == ['EnthMol', 'VapFrac']
    assert sorted(new_keras_surrogate._input_bounds.keys()) == [
        'Pressure_Pa', 'Temperature_K'
    ]
    assert new_keras_surrogate._input_bounds['Temperature_K'][
        0] == pytest.approx(360.0)
    assert new_keras_surrogate._input_bounds['Temperature_K'][
        1] == pytest.approx(380.0)
    assert new_keras_surrogate._input_bounds['Pressure_Pa'][
        0] == pytest.approx(101325.0)
    assert new_keras_surrogate._input_bounds['Pressure_Pa'][
        1] == pytest.approx(1.2 * 101325.0)

    # check input scaler
    expected_columns = ['Temperature_K', 'Pressure_Pa']
    offset_series = pd.Series({
        'Temperature_K': 369.983611,
        'Pressure_Pa': 111421.319811
    })
    factor_series = pd.Series({
        'Temperature_K': 5.836047,
        'Pressure_Pa': 5917.954504
    })
    scaler = new_keras_surrogate._input_scaler
    assert scaler._expected_columns == expected_columns
    pd.testing.assert_series_equal(scaler._offset,
                                   offset_series,
                                   rtol=rtol,
                                   atol=atol)
    pd.testing.assert_series_equal(scaler._factor,
                                   factor_series,
                                   rtol=rtol,
                                   atol=atol)

    # check output scaler
    expected_columns = ['EnthMol', 'VapFrac']
    offset_series = pd.Series({'EnthMol': 54599.629980, 'VapFrac': 0.403307})
    factor_series = pd.Series({'EnthMol': 14654.226615, 'VapFrac': 0.430181})
    scaler = new_keras_surrogate._output_scaler
    assert scaler._expected_columns == expected_columns
    pd.testing.assert_series_equal(scaler._offset,
                                   offset_series,
                                   rtol=rtol,
                                   atol=atol)
    pd.testing.assert_series_equal(scaler._factor,
                                   factor_series,
                                   rtol=rtol,
                                   atol=atol)

    # check evaluation
    x_test = pd.DataFrame({
        'Temperature_K': [360, 370, 380],
        'Pressure_Pa': [1.05 * 101325, 1.10 * 101325, 1.15 * 101325]
    })
    y_test = keras_surrogate.evaluate_surrogate(x_test)
    expected_y = pd.DataFrame({
        'EnthMol': [40194.5586954288, 48660.288218426984, 75178.30324367314],
        'VapFrac':
        [0.002291496299564877, 0.21942246438431742, 0.9996716243380308]
    })
    pd.testing.assert_frame_equal(y_test, expected_y, rtol=rtol, atol=atol)

    # check solve with pyomo
    x_test = pd.DataFrame({
        'Temperature_K': [370],
        'Pressure_Pa': [1.1 * 101325]
    })
    y_test = keras_surrogate.evaluate_surrogate(x_test)

    m = ConcreteModel()
    m.obj = Objective(expr=1)
    m.surrogate = SurrogateBlock()
    m.surrogate.build_model(surrogate_object=keras_surrogate,
                            formulation=KerasSurrogate.Formulation.FULL_SPACE)
    m.surrogate.inputs['Temperature_K'].fix(370)
    m.surrogate.inputs['Pressure_Pa'].fix(1.1 * 101325)
    solver = SolverFactory('ipopt')
    status = solver.solve(m, tee=True)
    assert_optimal_termination(status)

    y_test_pyomo = pd.DataFrame({
        'EnthMol': [value(m.surrogate.outputs['EnthMol'])],
        'VapFrac': [value(m.surrogate.outputs['VapFrac'])]
    })
    pd.testing.assert_frame_equal(y_test, y_test_pyomo, rtol=rtol, atol=atol)
コード例 #7
0
    def test_obj_con_cache(self):
        model = ConcreteModel()
        model.x = Var()
        model.c = Constraint(expr=model.x**2 >= 1)
        model.obj = Objective(expr=model.x**2)

        with TempfileManager.new_context() as TMP:
            nl_file = TMP.create_tempfile(suffix='.nl')
            model.write(nl_file, format='nl')
            self.assertFalse(hasattr(model, '_repn'))
            with open(nl_file) as FILE:
                nl_ref = FILE.read()

            nl_file = TMP.create_tempfile(suffix='.nl')
            model._gen_obj_repn = True
            model.write(nl_file)
            self.assertEqual(len(model._repn), 1)
            self.assertIn(model.obj, model._repn)
            obj_repn = model._repn[model.obj]
            with open(nl_file) as FILE:
                nl_test = FILE.read()
            self.assertEqual(nl_ref, nl_test)

            nl_file = TMP.create_tempfile(suffix='.nl')
            del model._repn
            model._gen_obj_repn = None
            model._gen_con_repn = True
            model.write(nl_file)
            self.assertEqual(len(model._repn), 1)
            self.assertIn(model.c, model._repn)
            c_repn = model._repn[model.c]
            with open(nl_file) as FILE:
                nl_test = FILE.read()
            self.assertEqual(nl_ref, nl_test)

            nl_file = TMP.create_tempfile(suffix='.nl')
            del model._repn
            model._gen_obj_repn = True
            model._gen_con_repn = True
            model.write(nl_file)
            self.assertEqual(len(model._repn), 2)
            self.assertIn(model.obj, model._repn)
            self.assertIn(model.c, model._repn)
            obj_repn = model._repn[model.obj]
            c_repn = model._repn[model.c]
            with open(nl_file) as FILE:
                nl_test = FILE.read()
            self.assertEqual(nl_ref, nl_test)

            nl_file = TMP.create_tempfile(suffix='.nl')
            model._gen_obj_repn = None
            model._gen_con_repn = None
            model.write(nl_file)
            self.assertEqual(len(model._repn), 2)
            self.assertIn(model.obj, model._repn)
            self.assertIn(model.c, model._repn)
            self.assertIs(obj_repn, model._repn[model.obj])
            self.assertIs(c_repn, model._repn[model.c])
            with open(nl_file) as FILE:
                nl_test = FILE.read()
            self.assertEqual(nl_ref, nl_test)

            nl_file = TMP.create_tempfile(suffix='.nl')
            model._gen_obj_repn = True
            model._gen_con_repn = True
            model.write(nl_file)
            self.assertEqual(len(model._repn), 2)
            self.assertIn(model.obj, model._repn)
            self.assertIn(model.c, model._repn)
            self.assertIsNot(obj_repn, model._repn[model.obj])
            self.assertIsNot(c_repn, model._repn[model.c])
            obj_repn = model._repn[model.obj]
            c_repn = model._repn[model.c]
            with open(nl_file) as FILE:
                nl_test = FILE.read()
            self.assertEqual(nl_ref, nl_test)

            nl_file = TMP.create_tempfile(suffix='.nl')
            model._gen_obj_repn = False
            model._gen_con_repn = False
            try:

                def dont_call_gsr(*args, **kwargs):
                    self.fail("generate_standard_repn should not be called")

                ampl_.generate_standard_repn = dont_call_gsr
                model.write(nl_file)
            finally:
                ampl_.generate_standard_repn = gsr
            self.assertEqual(len(model._repn), 2)
            self.assertIn(model.obj, model._repn)
            self.assertIn(model.c, model._repn)
            self.assertIs(obj_repn, model._repn[model.obj])
            self.assertIs(c_repn, model._repn[model.c])
            with open(nl_file) as FILE:
                nl_test = FILE.read()
            self.assertEqual(nl_ref, nl_test)

            # Check that repns generated by the LP wrter will be
            # processed correctly
            model._repn[model.c] = c_repn = gsr(model.c.body, quadratic=True)
            model._repn[model.obj] = obj_repn = gsr(model.obj.expr,
                                                    quadratic=True)
            nl_file = TMP.create_tempfile(suffix='.nl')
            try:

                def dont_call_gsr(*args, **kwargs):
                    self.fail("generate_standard_repn should not be called")

                ampl_.generate_standard_repn = dont_call_gsr
                model.write(nl_file)
            finally:
                ampl_.generate_standard_repn = gsr
            self.assertEqual(len(model._repn), 2)
            self.assertIn(model.obj, model._repn)
            self.assertIn(model.c, model._repn)
            self.assertIs(obj_repn, model._repn[model.obj])
            self.assertIs(c_repn, model._repn[model.c])
            with open(nl_file) as FILE:
                nl_test = FILE.read()
            self.assertEqual(nl_ref, nl_test)