Beispiel #1
0
 def test_build_instance(self):
     config_files = [
         '../../config/Lemon_Toy_Model.json',
         'test_model_config.json',
         '../../config/test_custom_controller.json',
     ]
     for config_file in config_files:
         config = mb.get_config(config_file)
         instance = mb.build_instance(config)
         self.assertEqual(len(instance), 1)
Beispiel #2
0
class Output(TestCase):
    # solve test problem
    config = mb.get_config('test_model_config.json')
    instance = mb.build_instance(config)
    conn = di.get_sqlite_connection()
    lookup = dv.LookupTables(conn)
    opt = pe.SolverFactory("glpk")
    instance.Cost.deactivate()
    instance.Environmental_Cost_Impact.deactivate()
    opt.solve(instance)

    def test_get_sets_frame(self):
        sets_df = mo.get_sets_frame(self.instance)
        self.assertGreater(len(sets_df), 0)

    def test_get_entity(self):
        # output the variables with and without units
        for v in self.instance.component_objects(pe.Var):
            v_dfr = mo.get_entity(v)
            self.assertIsInstance(v_dfr, pd.DataFrame)
            if len(v_dfr) > 0:
                self.assertGreater(len(v_dfr), 0)
            u_dfr = mo.get_entity(v, self.lookup, units=True)
            self.assertIsInstance(u_dfr, pd.DataFrame)
            if len(u_dfr) > 0:
                self.assertGreater(len(u_dfr), 0)

        # name the unit set for lookup
        flow_dfr = mo.get_entity(self.instance.Flow, self.lookup, units=['P_m'])
        self.assertGreater(len(flow_dfr), 0)

    def test_objectives(self):
        # do the optimisation for each objective
        for i, o in enumerate(self.instance.component_objects(pe.Objective)):
            for j, oo in enumerate(self.instance.component_objects(pe.Objective)):
                if i == j:
                    oo.activate()
                else:
                    oo.deactivate()
            opt = pe.SolverFactory("glpk")
            opt.solve(self.instance)
            o_dfr = mo.get_entity(o)
            self.assertGreater(len(o_dfr), 0)
            ou_dfr = mo.get_entity(o, self.lookup, units=True)
            self.assertGreater(len(ou_dfr), 0)

    def test_get_onset_names(self):
        l = list()
        for o in chain(self.instance.component_objects(pe.Var), self.instance.component_objects(pe.Param)):
            sets = mo.get_onset_names(o)
            l.append(sets)
        self.assertGreater(len(sets), 0)
Beispiel #3
0
 def build_button_clicked(self):
     logging.info('Build started')
     try:
         config = self.controller.get_config()
         self.concrete_model = mb.build_instance(
             config, self.controller.spec.settings)
         if self.controller.model_solve is not None:
             self.controller.model_solve.concrete_model = self.concrete_model
         self.build_list.clear()
         self.build_list.addItems(self.build_items)
         logging.info('Build completed')
         return True
     except ValueError as e:
         self.dialog_critical("Unable to find data in database", str(e),
                              traceback.format_exc())
     except UnitsError as e:
         self.dialog_critical("Unit conversion error", str(e),
                              traceback.format_exc())
     except Exception as e:
         self.dialog_critical("Uncaught exception for model build", str(e),
                              traceback.format_exc())
     return False
Beispiel #4
0
    def test_model_solve(self):

        setting = mqu.system_settings(testing=True)
        config_path = setting['config_path'].joinpath('Lemon_Toy_Model.json')
        config = mb.get_config(config_path)
        instance = mb.build_instance(config)

        # get lookups from db
        conn = di.get_sqlite_connection()
        lookup = dv.LookupTables(conn)

        # setup a model run
        model_solve = ms.ModelSolve(lookup)
        model_solve.concrete_model = instance
        model_solve.resize(800, 600)
        model_solve.show()

        # run the model
        QTest.mouseClick(model_solve.run_button, Qt.LeftButton)

        if 'IGNORE_EXEC' not in os.environ:
            app.exec()

        self.assertIsInstance(model_solve, ms.ModelSolve)
Beispiel #5
0
import mola.dataimport as di
import mola.dataview as dv
import mola.build as mb

import molaqt.view as mv
import molaqt.utils as mqu

app = QApplication(sys.argv)

# load a model and solve it for viewing
setting = mqu.system_settings(testing=True)
config_path = setting['config_path'].joinpath('Lemon_Toy_Model.json')
config = mb.get_config(config_path)
spec = mb.create_specification(config['specification'])
instance = mb.build_instance(config)
for i, obj in enumerate(instance.component_objects(pe.Objective)):
    # activate first objective
    if i == 0:
        obj.activate()
    else:
        obj.deactivate()
opt = pe.SolverFactory("glpk")
opt.solve(instance)

# get lookups from db
conn = di.get_sqlite_connection()
lookup = dv.LookupTables(conn)


class ModelViewManagerTest(TestCase):