Пример #1
0
    def test_woody_max_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=0)
        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])
Пример #2
0
    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])
Пример #3
0
Fulltime = 5 consecutive days + 2 free days


Weekday   | Employees needed
Monday    | 17
Tuesday   | 13
Wednesday | 15
Thursday  | 19
Friday    | 14
Saturday  | 16
Sunday    | 11

"""


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)
Пример #4
0
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)
Пример #5
0
        "price": 98,
        "max": 2
    }
}

MIN_REQ = {
    "kcal": 2000,
    "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))
Пример #6
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()
Пример #7
0
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()
Пример #8
0
K       | 15        |  6        | 16        | 2400
L       | 10        | 15        |  0        | 2400
Total   | 57        | 59        | 42        | 9600


Item          | Product A | Product B | Product C
Revenue p.u.  | 90$       | 100$      | 70$
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)