def test_diet_10n_10i(self): from Model.model import Model m = Model() MIN_REQ = load_obj('diet_10n_min_req') ingredients, list_of_ingredients = load_obj('diet_10n_10i_ing'), load_obj('diet_10n_10i_l_o_ing') x = [] for ing in list_of_ingredients: x.append(m.add_var("real+", name=ing)) x = np.array(x) m.minimize(sum(get_by_key(ingredients, "price", list_of_ingredients) * x)) for cst in MIN_REQ: left = get_by_key(ingredients, cst, list_of_ingredients) m.add_constraint(sum(left * x) >= MIN_REQ[cst]) m.solve(consider_dual=0) try: i = 0 for ing in list_of_ingredients: m.add_lazy_constraint(x[i] <= ingredients[ing]['max']) i += 1 except InfeasibleError as e: pass else: self.fail("Should raise InfeasibleError but didn't")
def test_woody_min_dual_lazy_constraint(self): m = Model() x1 = m.add_var("real+", name="a") x2 = m.add_var("real+", name="b") x3 = m.add_var("real+", name="c") m.minimize(35 * x1 + 60 * x2 + 75 * x3) m.add_constraint(8 * x1 + 12 * x2 + 16 * x3 >= 120) m.add_constraint(15 * x2 + 20 * x3 >= 60) m.add_constraint(3 * x1 + 6 * x2 + 9 * x3 >= 48) m.solve(consider_dual=2) m.add_lazy_constraint(x1 <= 5) m.add_lazy_constraint(x3 >= 1) computed_solution = m.get_solution_object() real_sol = [5, 0, 5, 550] for x_idx in range(len(real_sol)): self.assertAlmostEqual(computed_solution[x_idx], real_sol[x_idx])
print("all added") t0 = time() m.solve(consider_dual=0) print("Solved first in %f" % (time() - t0)) m.print_solution(slack=False) sol_obj = m.get_solution_object() solved = False while not solved: solved = True i = 0 for ing in list_of_ingredients: if sol_obj[i] > ingredients[ing]['max']: solved = False m.add_lazy_constraint(x[i] <= ingredients[ing]['max']) exit(1) sol_obj = m.get_solution_object() break i += 1 print("Solved total in %f" % (time() - t0)) print("End tableau") print(m.t.tableau) print("Steps: ", m.steps) m.print_solution(slack=False)