Exemplo n.º 1
0
 def test_add_quadratic_constraints(self):
     x = Variable('x', lb=-83.3, ub=1324422., type='binary')
     y = Variable('y', lb=-181133.3, ub=12000., type='continuous')
     z = Variable('z', lb=0.000003, ub=0.000003, type='integer')
     constr1 = Constraint(0.3 * x * y + 0.4 * y**2 + 66. * z,
                          lb=-100,
                          ub=0.,
                          name='test')
     constr2 = Constraint(2.333 * x * x + y + 3.333,
                          ub=100.33,
                          name='test2')
     constr3 = Constraint(2.333 * x + y**2 + z + 33, ub=100.33, lb=-300)
     self.model.add(constr1)
     self.model.add(constr2)
     self.model.add(constr3)
     self.assertIn(constr1, self.model.constraints)
     self.assertIn(constr2, self.model.constraints)
     self.assertIn(constr3, self.model.constraints)
     cplex_lines = [
         line.strip() for line in str(self.model).split('\n')
     ]
     self.assertIn('test:       0.4 y + 66 z + 0.3 x - Rgtest  = -100',
                   cplex_lines)
     self.assertIn('test2:      y + 2.333 x <= 96.997', cplex_lines)
     # Dummy_21:   y + z + 2.333 x - RgDummy_21  = -300
     self.assertRegexpMatches(
         str(self.model),
         '\s*Dummy_\d+:\s*y \+ z \+ 2\.333 x - .*  = -300')
     print(self.model)
Exemplo n.º 2
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')
Exemplo n.º 3
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')
Exemplo n.º 4
0
    def _createSVO(self, reset=False):
        """
        Create the S.V = 0 restrictions for each metabolite for later addition to the optimization problem. The storage
        of these constraints as an attribute avoids its creation every iteration of the algorithm, thus
        improving its performance.

        reset: boolean, indicates if the restrictions should be created again. Useful when reactions are changed.

        Returns: list, list of Restriction objects(optlang)

        """

        if self._SVO is None or reset:
            SVO = []
            for m_id in self.model.metabolites:
                row = []
                for r_id, reaction in self.model.reactions.items():
                    if m_id in reaction.stoichiometry:
                        row.append(reaction.stoichiometry[m_id] *
                                   self._VI[r_id])
                con_row = Constraint(sum(row), lb=0, ub=0)
                SVO.append(con_row)

            self._SVO = SVO
            return self._SVO

        else:
            return self._SVO
Exemplo n.º 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.model.add(constraint)
     self.assertEqual(self.model.constraints['test'].__str__(), 'test: -100 <= 0.4*y + 0.3*x')
     self.assertEqual(self.model.problem.linear_constraints.get_coefficients([('test', 'x'), ('test', 'y')]),
                      [0.3, 0.4])
     z = Variable('z', lb=3, ub=4, type='integer')
     constraint += 77. * z
     self.assertEqual(
         self.model.problem.linear_constraints.get_coefficients([('test', 'x'), ('test', 'y'), ('test', 'z')]),
         [0.3, 0.4, 77.])
     self.assertEqual(self.model.constraints['test'].__str__(), 'test: -100 <= 0.4*y + 0.3*x + 77.0*z')
     print(self.model)
Exemplo n.º 6
0
 def setUp(self):
     self.model = Model()
     self.x1 = Variable("x1", lb=0)
     self.x2 = Variable("x2", lb=0)
     self.c1 = Constraint(self.x1 + self.x2, lb=1)
     self.model.add([self.x1, self.x2, self.c1])