def test_change_of_objective_is_reflected_in_low_level_solver(self): x = Variable('x', lb=-83.3, ub=1324422.) y = Variable('y', lb=-181133.3, ub=12000.) objective = Objective(0.3 * x + 0.4 * y, name='test', direction='max') self.model.objective = objective self.assertEqual((self.model.objective.expression - (0.4 * y + 0.3 * x)).expand() - 0, 0) self.assertEqual(self.model.objective.direction, "max") self.assertEqual(glp_get_obj_coef(self.model.problem, x._index), 0.3) self.assertEqual(glp_get_obj_coef(self.model.problem, y._index), 0.4) for i in range(1, glp_get_num_cols(self.model.problem) + 1): if i != x._index and i != y._index: self.assertEqual(glp_get_obj_coef(self.model.problem, i), 0) z = Variable('z', lb=4, ub=4, type='integer') self.model.objective += 77. * z self.assertEqual((self.model.objective.expression - (0.4 * y + 0.3 * x + 77.0 * z)).expand() - 0, 0) self.assertEqual(self.model.objective.direction, "max") self.assertEqual(glp_get_obj_coef(self.model.problem, x._index), 0.3) self.assertEqual(glp_get_obj_coef(self.model.problem, y._index), 0.4) self.assertEqual(glp_get_obj_coef(self.model.problem, z._index), 77.) for i in range(1, glp_get_num_cols(self.model.problem) + 1): if i != x._index and i != y._index and i != z._index: self.assertEqual(glp_get_obj_coef(self.model.problem, i), 0)
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
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 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 _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