Exemple #1
0
 def test_constraint_set_problem_to_None_caches_the_latest_expression_from_solver_instance(
         self):
     x = Variable('x', lb=-83.3, ub=1324422.)
     y = Variable('y', lb=-181133.3, ub=12000.)
     constraint = Constraint(0.3 * x + 0.4 * y, lb=-100, name='test')
     self.model.add(constraint)
     z = Variable('z', lb=2, ub=5, type='integer')
     constraint += 77. * z
     self.model.remove(constraint)
     self.assertEqual(constraint.__str__(),
                      'test: -100 <= 0.4*y + 0.3*x + 77.0*z')
Exemple #2
0
 def setUp(self):
     self.var1 = var1 = Variable("var1", lb=0, ub=1, type="continuous")
     self.var2 = var2 = Variable("var2", lb=0, ub=1, type="continuous")
     self.const1 = const1 = Constraint(0.5 * var1, lb=0, ub=1, name="c1")
     self.const2 = const2 = Constraint(0.1 * var2 + 0.4 * var1, name="c2")
     self.model = model = Model()
     model.add([var1, var2])
     model.add([const1, const2])
     model.objective = Objective(var1 + var2)
     model.update()
     self.json_string = json.dumps(model.to_json())
    def _MakeMDFProblemDual(self):
        """Create a CVXOPT problem for finding the Maximal Thermodynamic
        Driving Force (MDF).

        Does not set the objective function... leaves that to the caller.

        Returns:
            the linear problem object, and the four types of variables as arrays
        """
        A, b, c, w, g, z, u = self._GetDualVariablesAndConstants()
        x = w + g + z + u
        lp = Model(name="MDF_DUAL")

        cnstr_names = ["y_%02d" % j for j in range(self.Nr)] + \
                      ["l_%02d" % j for j in range(self.Nc)] + \
                      ["MDF"]

        constraints = []
        for i in range(A.shape[1]):
            row = [A[j, i] * x[j] for j in range(A.shape[0])]
            constraints.append(
                Constraint(sum(row),
                           lb=c[i, 0],
                           ub=c[i, 0],
                           name=cnstr_names[i]))

        lp.add(constraints)

        row = [b[i, 0] * x[i] for i in range(A.shape[0])]
        lp.objective = Objective(sum(row), direction='min')

        return lp, w, g, z, u
    def _MakeMDFProblem(self):
        """Create a CVXOPT problem for finding the Maximal Thermodynamic
        Driving Force (MDF).

        Does not set the objective function... leaves that to the caller.

        Returns:
            the linear problem object, and the three types of variables as arrays
        """
        A, b, c, y, l = self._GetPrimalVariablesAndConstants()
        B = Variable('mdf')
        x = y + l + [B]
        lp = Model(name="MDF_PRIMAL")

        cnstr_names = ["driving_force_%02d" % j for j in range(self.Nr_active)] + \
                      ["covariance_var_ub_%02d" % j for j in range(self.Nr)] + \
                      ["covariance_var_lb_%02d" % j for j in range(self.Nr)] + \
                      ["log_conc_ub_%02d" % j for j in range(self.Nc)] + \
                      ["log_conc_lb_%02d" % j for j in range(self.Nc)]

        constraints = []
        for j in range(A.shape[0]):
            row = [A[j, i] * x[i] for i in range(A.shape[1])]
            constraints.append(
                Constraint(sum(row), ub=b[j, 0], name=cnstr_names[j]))

        lp.add(constraints)

        row = [c[i, 0] * x[i] for i in range(c.shape[0])]
        lp.objective = Objective(sum(row), direction='max')

        return lp, y, l, B
Exemple #5
0
 def test_change_of_constraint_is_reflected_in_low_level_solver(self):
     x = Variable('x', lb=-83.3, ub=1324422.)
     y = Variable('y', lb=-181133.3, ub=12000.)
     constraint = Constraint(0.3 * x + 0.4 * y, lb=-100, name='test')
     self.assertEqual(constraint.index, None)
     self.model.add(constraint)
     self.assertEqual(self.model.constraints['test'].__str__(),
                      'test: -100 <= 0.4*y + 0.3*x')
     self.assertEqual(constraint.index, 73)
     z = Variable('z', lb=3, ub=10, type='integer')
     self.assertEqual(z.index, None)
     constraint += 77. * z
     self.assertEqual(z.index, 98)
     self.assertEqual(self.model.constraints['test'].__str__(),
                      'test: -100 <= 0.4*y + 0.3*x + 77.0*z')
     print(self.model)
     self.assertEqual(constraint.index, 73)
    def test_change_of_constraint_is_reflected_in_low_level_solver(self):
        x = Variable('x', lb=-83.3, ub=1324422.)
        y = Variable('y', lb=-181133.3, ub=12000.)
        constraint = Constraint(0.3 * x + 0.4 * y, lb=-100, name='test')
        self.assertEqual(constraint._index, None)
        self.model.add(constraint)
        self.assertEqual((self.model.constraints["test"].expression -
                          (0.4 * y + 0.3 * x)).expand(), 0)
        self.assertEqual(self.model.constraints["test"].lb, -100)

        self.assertEqual(constraint._index, 73)
        z = Variable('z', lb=3, ub=10, type='integer')
        self.assertEqual(z._index, None)
        constraint += 77. * z
        self.assertEqual(z._index, 98)

        self.assertEqual((self.model.constraints["test"].expression -
                          (0.4 * y + 0.3 * x + 77.0 * z)).expand(), 0)
        self.assertEqual(self.model.constraints["test"].lb, -100)

        self.assertEqual(constraint._index, 73)
    def _GetTotalEnergyProblem(self, min_driving_force=0.0, direction='min'):

        A, b, _c, y, l = self._GetPrimalVariablesAndConstants()
        x = y + l + [min_driving_force]
        lp = Model(name='MDF')

        constraints = []
        for j in range(A.shape[0]):
            row = [A[j, i] * x[i] for i in range(A.shape[1])]
            constraints.append(
                Constraint(sum(row), ub=b[j, 0], name='row_%d' % j))

        total_g0 = float(self.fluxes @ self.dG0_r_prime)
        total_reaction = self.S @ self.fluxes.T
        row = [total_reaction[i, 0] * x[i] for i in range(self.Nc)]
        total_g = total_g0 + sum(row)

        lp.add(constraints)
        lp.objective = Objective(total_g, direction=direction)

        return lp
    def test_add_constraints(self):
        x = Variable('x', lb=0, ub=1, type='binary')
        y = Variable('y', lb=-181133.3, ub=12000., type='continuous')
        z = Variable('z', lb=0., ub=10., type='integer')
        constr1 = Constraint(0.3 * x + 0.4 * y + 66. * z,
                             lb=-100,
                             ub=0.,
                             name='test')
        constr2 = Constraint(2.333 * x + y + 3.333, ub=100.33, name='test2')
        constr3 = Constraint(2.333 * x + y + z, lb=-300)
        constr4 = Constraint(x, lb=-300, ub=-300)
        constr5 = Constraint(3 * x)
        self.model.add(constr1)
        self.model.add(constr2)
        self.model.add(constr3)
        self.model.add([constr4, constr5])
        self.assertIn(constr1.name, self.model.constraints)
        self.assertIn(constr2.name, self.model.constraints)
        self.assertIn(constr3.name, self.model.constraints)
        self.assertIn(constr4.name, self.model.constraints)
        self.assertIn(constr5.name, self.model.constraints)
        # constr1
        ia = intArray(glp_get_num_rows(self.model.problem) + 1)
        da = doubleArray(glp_get_num_rows(self.model.problem) + 1)
        nnz = glp_get_mat_row(self.model.problem, constr1._index, ia, da)
        coeff_dict = dict()
        for i in range(1, nnz + 1):
            coeff_dict[glp_get_col_name(self.model.problem, ia[i])] = da[i]
        self.assertDictEqual(coeff_dict, {'x': 0.3, 'y': 0.4, 'z': 66.})
        self.assertEqual(glp_get_row_type(self.model.problem, constr1._index),
                         GLP_DB)
        self.assertEqual(glp_get_row_lb(self.model.problem, constr1._index),
                         -100)
        self.assertEqual(glp_get_row_ub(self.model.problem, constr1._index), 0)
        # constr2
        ia = intArray(glp_get_num_rows(self.model.problem) + 1)
        da = doubleArray(glp_get_num_rows(self.model.problem) + 1)
        nnz = glp_get_mat_row(self.model.problem, constr2._index, ia, da)
        coeff_dict = dict()
        for i in range(1, nnz + 1):
            coeff_dict[glp_get_col_name(self.model.problem, ia[i])] = da[i]
        self.assertDictEqual(coeff_dict, {'x': 2.333, 'y': 1.})
        self.assertEqual(glp_get_row_type(self.model.problem, constr2._index),
                         GLP_UP)
        self.assertEqual(glp_get_row_lb(self.model.problem, constr2._index),
                         -1.7976931348623157e+308)
        self.assertEqual(glp_get_row_ub(self.model.problem, constr2._index),
                         96.997)
        # constr3
        ia = intArray(glp_get_num_rows(self.model.problem) + 1)
        da = doubleArray(glp_get_num_rows(self.model.problem) + 1)
        nnz = glp_get_mat_row(self.model.problem, constr3._index, ia, da)
        coeff_dict = dict()
        for i in range(1, nnz + 1):
            coeff_dict[glp_get_col_name(self.model.problem, ia[i])] = da[i]
        self.assertDictEqual(coeff_dict, {'x': 2.333, 'y': 1., 'z': 1.})
        self.assertEqual(glp_get_row_type(self.model.problem, constr3._index),
                         GLP_LO)
        self.assertEqual(glp_get_row_lb(self.model.problem, constr3._index),
                         -300)
        self.assertEqual(glp_get_row_ub(self.model.problem, constr3._index),
                         1.7976931348623157e+308)
        # constr4
        ia = intArray(glp_get_num_rows(self.model.problem) + 1)
        da = doubleArray(glp_get_num_rows(self.model.problem) + 1)
        nnz = glp_get_mat_row(self.model.problem, constr4._index, ia, da)
        coeff_dict = dict()
        for i in range(1, nnz + 1):
            coeff_dict[glp_get_col_name(self.model.problem, ia[i])] = da[i]
        self.assertDictEqual(coeff_dict, {'x': 1})
        self.assertEqual(glp_get_row_type(self.model.problem, constr4._index),
                         GLP_FX)
        self.assertEqual(glp_get_row_lb(self.model.problem, constr4._index),
                         -300)
        self.assertEqual(glp_get_row_ub(self.model.problem, constr4._index),
                         -300)

        # constr5
        ia = intArray(glp_get_num_rows(self.model.problem) + 1)
        da = doubleArray(glp_get_num_rows(self.model.problem) + 1)
        nnz = glp_get_mat_row(self.model.problem, constr5._index, ia, da)
        coeff_dict = dict()
        for i in range(1, nnz + 1):
            coeff_dict[glp_get_col_name(self.model.problem, ia[i])] = da[i]
        self.assertDictEqual(coeff_dict, {'x': 3})
        self.assertEqual(glp_get_row_type(self.model.problem, constr5._index),
                         GLP_FR)
        self.assertLess(glp_get_row_lb(self.model.problem, constr5._index),
                        -1e30)
        self.assertGreater(glp_get_row_ub(self.model.problem, constr5._index),
                           1e30)