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_unbound(self): m = Model(print_obj={}) a = m.add_var("real+", name="a") b = m.add_var("real+", name="b") m.maximize(3 * a - b) m.add_constraint(-3 * a + 3 * b <= 6) m.add_constraint(-8 * a + 4 * b <= 4) try: m.solve() except Unbounded as e: pass else: self.fail("Should raise Unbounded but didn't")
def test_minimize_2v_3c_2o(self): m = Model(print_obj={}) x = [] for i in range(1, 3): x.append(m.add_var("real+", name="x_%d" % i)) m.minimize(0.12 * x[0] + 0.15 * x[1]) m.add_constraint(60 * x[0] + 60 * x[1] >= 300) m.add_constraint(12 * x[0] + 6 * x[1] >= 36) m.add_constraint(10 * x[0] + 30 * x[1] >= 90) m.solve() computed_solution = m.get_solution_object() real_sol = [3, 2, 0.66] for x_idx in range(len(real_sol)): self.assertAlmostEqual(computed_solution[x_idx], real_sol[x_idx])
def test_maximize_4v_3c_3o(self): m = Model(print_obj={}) x = [] for i in range(1, 5): x.append(m.add_var("real+", name="x_%d" % i)) m.maximize(3 * x[1] + x[2] + 4 * x[3]) m.add_constraint(x[0] + x[1] + x[2] + x[3] <= 40) m.add_constraint(2 * x[0] + x[1] + (-1 * x[2]) + (-1 * x[3]) <= 10) m.add_constraint(x[3] + (-1 * x[1]) <= 10) m.solve(revised=True) computed_solution = m.get_solution_object() real_sol = [0, 15.0, 0, 25.0, 145.0] for x_idx in range(len(real_sol)): self.assertAlmostEqual(computed_solution[x_idx], real_sol[x_idx])
def test_woody_max_dual_add_variable(self): m = Model() x1 = m.add_var("real+", name="a") x2 = m.add_var("real+", name="b") m.maximize(35 * x1 + 60 * x2) m.add_constraint(8 * x1 + 12 * x2 <= 120) m.add_constraint(15 * x2 <= 60) m.add_constraint(3 * x1 + 6 * x2 <= 48) m.solve(consider_dual=2) m.add_new_variable([16, 20, 9], 75) computed_solution = m.get_solution_object() real_sol = [12, 2, 0, 540] for x_idx in range(len(real_sol)): self.assertAlmostEqual(computed_solution[x_idx], real_sol[x_idx])
def test_maximize_2v_4c_2o(self): m = Model(print_obj={}) x = [] for i in range(1, 3): x.append(m.add_var("real+", name="x_%d" % i)) m.maximize(4 * x[0] + 3 * x[1]) m.add_constraint(2 * x[0] + 3 * x[1] <= 6) m.add_constraint(-3 * x[0] + 2 * x[1] <= 3) m.add_constraint(0 * x[0] + 2 * x[1] <= 5) m.add_constraint(2 * x[0] + 1 * x[1] <= 4) m.solve() computed_solution = m.get_solution_object() real_sol = [1.5, 1, 9] for x_idx in range(len(real_sol)): self.assertAlmostEqual(computed_solution[x_idx], real_sol[x_idx])
def test_revised(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.maximize(5 * x1 + 4 * x2 + 3 * x3) m.add_constraint(2 * x1 + 3 * x2 + x3 <= 5) m.add_constraint(4 * x1 + x2 + 2 * x3 <= 11) m.add_constraint(3 * x1 + 4 * x2 + 2 * x3 <= 8) m.solve(consider_dual=0, revised=True) computed_solution = m.get_solution_object() real_sol = [2, 0, 1, 13] for x_idx in range(len(real_sol)): self.assertAlmostEqual(computed_solution[x_idx], real_sol[x_idx])
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])
""" mon = m.add_var("real+", name="Monday") tue = m.add_var("real+", name="Tuesday") wed = m.add_var("real+", name="Wednesday") thu = m.add_var("real+", name="Thursday") fri = m.add_var("real+", name="Friday") sat = m.add_var("real+", name="Saturday") sun = m.add_var("real+", name="Sunday") m.minimize(mon+tue+wed+thu+fri+sat+sun) m.add_constraint(mon+thu+fri+sat+sun >= 17) m.add_constraint(tue+fri+sat+sun+mon >= 13) m.add_constraint(wed+sat+sun+mon+tue >= 15) m.add_constraint(thu+sun+mon+tue+wed >= 19) m.add_constraint(fri+mon+tue+wed+thu >= 14) m.add_constraint(sat+tue+wed+thu+fri >= 16) m.add_constraint(sun+wed+thu+fri+sat >= 11) t0 = time() m.solve() print("Solved in %f" % (time()-t0)) m.print_solution()
import numpy as np from time import time import random, string from Model.model import Model m = Model(print_obj={'end_conf': True}) x = [] for i in range(4): x.append(m.add_var("real+", name=i)) x = np.array(x) m.maximize(sum(np.array([4, 1, 5, 3]) * x)) m.add_constraint(x[0] - x[1] - x[2] + 3 * x[3] <= 1) m.add_constraint(5 * x[0] + x[1] + 3 * x[2] + 8 * x[3] <= 55) m.add_constraint(-x[0] + 2 * x[1] + 3 * x[2] - 5 * x[3] <= 3) print("all added") t0 = time() m.solve() print("Solved in %f" % (time() - t0)) m.print_solution(slack=False) print("Steps: ", m.steps)
"protein": 55, "calcium": 800, } list_of_ingredients = get_keys(ingredients) x = [] for ing in list_of_ingredients: x.append(m.add_var("real+", name=ing, ub=ingredients[ing]["max"])) 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]) 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
from Model.model import Model import numpy as np from time import time import sys m = Model(print_obj={'start_conf': True}) a = m.add_var("real+", name="a") b = m.add_var("real+", name="b") m.maximize(3 * a - b) m.add_constraint(-3 * a + 3 * b <= 6) m.add_constraint(-8 * a + 4 * b <= 4) t0 = time() m.solve() print("Solved in %f" % (time() - t0)) m.print_solution()
from Model.model import Model import numpy as np from time import time import sys m = Model(print_obj={'start_conf': True}) 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.add_constraint(8 * x1 + 12 * x2 + 16 * x3 + 12 * x4 <= 120) m.add_constraint(15 * x2 + 20 * x3 + 30 * x4 <= 60) m.add_constraint(3 * x1 + 6 * x2 + 9 * x3 + 15 * x4 <= 48) """ m.solve(consider_dual=0) m.print_solution() print(m.t.tableau) m.add_new_variable([12, 30, 10], 15) m.print_solution()
Material p.u. | 45$ | 40$ | 20$ Profit p.u. | 45$ | 60$ | 50$ Maximum sales | 100 | 40 | 60 Maximize profit """ a = m.add_var("real+", name="apple") b = m.add_var("real+", name="banana") c = m.add_var("real+", name="carrot") m.maximize(45*a+60*b+50*c) m.add_constraint(20*a+10*b+10*c <= 2400) m.add_constraint(12*a+28*b+16*c <= 2400) m.add_constraint(15*a+6*b+16*c <= 2400) m.add_constraint(10*a+15*b <= 2400) m.add_constraint(a <= 100) m.add_constraint(b <= 40) m.add_constraint(c <= 60) t0 = time() m.solve() print("Solved in %f" % (time()-t0)) m.print_solution()
mean = np.mean(profit) # profit /= mean # m.multiplier = mean x = [] for i in range(n_blocks): x.append(m.add_var("real+", name=i)) x = np.array(x) m.file_name = "examples/data/newman" m.maximize(sum(profit * x)) # binary for i in range(n_blocks): m.add_constraint(x[i] <= 1) # cost # m.add_constraint(sum(get_by_key(blocks,"c")*x) <= max_c) for i in range(n_blocks): if len(pred[i]) > 0: m.add_constraint(len(pred[i]) * x[i] - sum(x[pred[i]]) <= 0) print("all added") t0 = time() m.solve(revised=True) # m.solve() print("Solved first in %f" % (time() - t0))
from Model.model import Model m = Model(print_obj={ 'start_conf': True, 'end_conf': True }) x = [] for i in range(3): x.append(m.add_var("real+", name=i)) x = np.array(x) m.maximize(sum(np.array([5,5,3])*x)) m.add_constraint(x[0]+3*x[1]+x[2] <= 3) m.add_constraint(-x[0]+0*x[1]+3*x[2] <= 2) m.add_constraint(2*x[0]-x[1]+2*x[2] <= 4) m.add_constraint(2*x[0]+3*x[1]-x[2] <= 2) print("all added") t0 = time() m.solve() print("Solved in %f" % (time()-t0)) m.print_solution(slack=False) print("Steps: ",m.steps)
'c': 3000 }, # 9 ] max_c = 4000 x = [] for i in range(len(blocks)): x.append(m.add_var("real+", name=i)) x = np.array(x) m.maximize(sum(get_by_key(blocks, "p") * x)) # binary for i in range(len(blocks)): m.add_constraint(x[i] <= 1) # cost m.add_constraint(sum(get_by_key(blocks, "c") * x) <= max_c) for i in range(len(blocks)): if len(blocks[i]["pred"]) > 0: m.add_constraint( len(blocks[i]["pred"]) * x[i] - sum(x[blocks[i]["pred"]]) <= 0) print("all added") t0 = time() m.solve(revised=True) # m.solve()
def test_diet(self): m = Model() a = m.add_var("real+", name="oat") b = m.add_var("real+", name="chicken") c = m.add_var("real+", name="egg") d = m.add_var("real+", name="milk") e = m.add_var("real+", name="cake") f = m.add_var("real+", name="bean") m.minimize(25 * a + 130 * b + 85 * c + 70 * d + 95 * e + 98 * f) # calories m.add_constraint(110 * a + 205 * b + 160 * c + 160 * d + 420 * e + 260 * f >= 2000) # proteins m.add_constraint(4 * a + 32 * b + 13 * c + 8 * d + 4 * e + 14 * f >= 55) # calcium m.add_constraint(2 * a + 12 * b + 54 * c + 285 * d + 22 * e + 80 * f >= 800) # oats m.add_constraint(a <= 4) # chicken m.add_constraint(b <= 3) # egg m.add_constraint(c <= 2) # milk m.add_constraint(d <= 8) # cake m.add_constraint(e <= 1) # bean m.add_constraint(f <= 2) m.solve(revised=True, consider_dual=0) computed_solution = m.get_solution_object() real_sol = [4.0, 0, 0, 3.875, 1, 2, 662.25] for x_idx in range(len(real_sol)): self.assertAlmostEqual(computed_solution[x_idx], real_sol[x_idx])
def test_diet_integer_more_restrictions(self): m = Model(dtype="fraction") a = m.add_var("int+", name="oat") b = m.add_var("int+", name="chicken") c = m.add_var("int+", name="egg") d = m.add_var("int+", name="milk") e = m.add_var("int+", name="cake") f = m.add_var("int+", name="bean") m.minimize(25 * a + 130 * b + 85 * c + 70 * d + 95 * e + 98 * f) # calories m.add_constraint(110 * a + 205 * b + 160 * c + 160 * d + 420 * e + 260 * f >= 2000) # proteins m.add_constraint(4 * a + 32 * b + 13 * c + 8 * d + 4 * e + 14 * f >= 55) # calcium m.add_constraint(2 * a + 12 * b + 54 * c + 285 * d + 22 * e + 80 * f >= 800) # oats m.add_constraint(a <= 2) # chicken m.add_constraint(b <= 3) # egg m.add_constraint(c <= 2) # milk m.add_constraint(d <= 2) # cake m.add_constraint(e <= 1) # bean m.add_constraint(f <= 2) m.solve() computed_solution = m.get_solution_object() real_sol = [2, 1, 2, 2, 1, 2, 781] for x_idx in range(len(real_sol)): self.assertAlmostEqual(computed_solution[x_idx], real_sol[x_idx])