def run():
    model = Model("example_01_solvable")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")

    model.add_constraint(x1 <= 150)
    model.add_constraint(x2 <= 250)
    model.add_constraint(2 * x1 + x2 <= 500)

    model.maximize(8 * x1 + 5 * x2)

    try:
        solution = model.solve()
    except:
        raise AssertionError(
            "This problem has a solution and your algorithm hasn't found it!")

    logging.info(solution)

    assert (solution.assignment == [
        125.0, 250.0
    ]), "Your algorithm found an incorrect solution!"

    logging.info("Congratulations! This solution seems to be alright :)")
Ejemplo n.º 2
0
def run():

    # fill missing test based on the example_01_solvable.py
    # to make the test a bit more interesting:
    # * make the model unfeasible in a way detectable by the 2-phase simplex
    #
    model = Model("example_05_unfeasible")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")

    model.maximize(x1)
    model.add_constraint(x1 + x2 == 150)
    model.add_constraint(x1 - x2 >= 250)

    try:
        solution = model.solve()
    except PositiveArtificialVariablesError:
        logging.info(
            "Congratulations! You found an unfeasible solution detectable with artificial variables :)"
        )
    else:
        raise AssertionError(
            "This problem has no solution but your algorithm hasn't figured it out!"
        )
def run():
    #TODO:
    # fill missing test based on the example_01_solvable.py
    # to make the test a bit more interesting:
    # * make the model unbounded!
    #
    # TIP: you may use other solvers (e.g. https://online-optimizer.appspot.com)
    #      to check if the problem is unbounded

    # logging.info("This test is empty but it shouldn't be, fix it!")
    # raise AssertionError("Test is empty")

    model = Model("example_03_unbounded")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")
    x3 = model.create_variable("x3")

    model.add_constraint(x1 - x2 + x3 <= 5)
    model.add_constraint(-2 * x1 + x2 <= 3)
    model.add_constraint(x2 - 2 * x3 <= 5)

    model.maximize(3 * x2 + x3)

    try:
        solution = model.solve()
    except Exception as e:
        logging.info("Solver throw an Exception: " + e.__str__())
        # raise AssertionError(e.__str__())
        assert (
            e.__eq__("Linear Programming model is unbounded")
        ), "Your algorithm throw an Exception -> \"Linear Programming model is unbounded\""

    logging.info("Congratulations! This Exception seems to be alright :)")
Ejemplo n.º 4
0
def run():
    model = Model("example_07_cost_sensitivity")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")
    x3 = model.create_variable("x3")

    model.add_constraint(6 * x1 + 5 * x2 + 8 * x3 <= 60)
    model.add_constraint(10 * x1 + 20 * x2 + 10 * x3 <= 150)
    model.add_constraint(x1 <= 8)

    model.maximize(5 * x1 + 4.5 * x2 + 6 * x3)

    solution = model.solve()

    analyser = Analyser()
    analysis_results = analyser.analyse(solution)
    analyser.interpret_results(solution, analysis_results, logging.info)

    objective_analysis_results = analysis_results[
        ObjectiveSensitivityAnalyser.name()]
    expected_bounds = [(4.636, 5.4), (4.167, 6.5), (float("-inf"), 6.571)]
    tolerance = 0.001
    for (i, bounds_pair) in enumerate(objective_analysis_results):
        assert math.isclose(
            bounds_pair[0], expected_bounds[i][0], abs_tol=tolerance
        ), f"left bound of the coefficient range seems to be incorrect, expected {expected_bounds[i][0]}, got {bounds_pair[0]}"
        assert math.isclose(
            bounds_pair[1], expected_bounds[i][1], abs_tol=tolerance
        ), f"right bound of the coefficient range seems to be incorrect, expected {expected_bounds[i][1]}, got {bounds_pair[1]}"

    logging.info(
        "Congratulations! This cost coefficients analysis look alright :)")
Ejemplo n.º 5
0
def run():
    #TODO:
    # fill missing test based on the example_01_solvable.py
    # to make the test a bit more interesting:
    # * minimize the objective (so the solver would have to normalize it)
    # * make some "=>" constraints
    # * the model still has to be solvable by the basix simplex withour artificial variables
    #
    # TIP: you may use other solvers (e.g. https://online-optimizer.appspot.com)
    #      to find the correct solution
    model = Model("example_02_solvable")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")
    x3 = model.create_variable("x3")

    model.add_constraint(2 * x1 + 85 * x2 + x3 >= -26)
    model.add_constraint(6 * x1 + x2 + 46 * x3 <= 156)
    model.add_constraint(2 * x1 + 65 * x2 + 26 * x3 * 2 <= 561)

    model.maximize(3 * x1 + 8 * x2 + 6 * x3)

    try:
        solution = model.solve()
    except:
        raise AssertionError("PROBLEM")

    logging.info(solution)
    assert (list(map(lambda x: round(x, 2), solution.assignment)) == [
        24.69, 7.87, 0.0, 744.42, 0.0, 0.0
    ]), "Your algorithm found an incorrect solution!"

    logging.info("Congratulations! This solution seems to be alright :)")
Ejemplo n.º 6
0
def run():
    #TODO:
    # fill missing test based on the example_01_solvable.py
    # to make the test a bit more interesting:
    # * make the solver use artificial variables!
    model = Model("example_01_solvable")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")

    model.add_constraint(x1 + x2 <= 10)
    model.add_constraint(-1 * x1 + x2 >= 2)

    model.maximize(2 * x1 + x2)

    try:
        solution = model.solve()
    except:
        raise AssertionError(
            "This problem has a solution and your algorithm hasn't found it!")

    logging.info(solution)

    assert (solution.assignment == [
        4.0, 6.0
    ]), "Your algorithm found an incorrect solution!"

    logging.info("Congratulations! This solution seems to be alright :)")
def run():
    primal = Model("example_06_dual")

    x0 = primal.create_variable("x0")
    x1 = primal.create_variable("x1")
    x2 = primal.create_variable("x2")

    primal.add_constraint(4*x0 + 8*x1 - x2 <= 5)
    primal.add_constraint(7*x0 - 2*x1 + 2*x2 >= 4)
    
    primal.maximize(3*x0 + 2*x1 - 6*x2)

    expected_dual = Model("example_06_dual (expected dual)")

    y0 = expected_dual.create_variable("y0")
    y1 = expected_dual.create_variable("y1")

    expected_dual.add_constraint(4*y0 - 7*y1 >= 3)
    expected_dual.add_constraint(8*y0 + 2*y1 >= 2)
    expected_dual.add_constraint(-1*y0 - 2*y1 >= -6)

    expected_dual.minimize(5 * y0 - 4 * y1)
    
    dual = primal.dual()

    assert dual.is_equivalent(expected_dual), "dual wasn't calculated as expected"
    assert primal.is_equivalent(dual.dual()), "double dual should equal the initial model"
    
    primal_solution = primal.solve()
    dual_solution = dual.solve()

    assert primal_solution.objective_value() == dual_solution.objective_value(), "dual and primal should have the same value at optimum"
    
    logging.info("Congratulations! The dual creation seems to be implemented correctly :)")
Ejemplo n.º 8
0
def run():
    primal = Model("zad 2")
    l = primal.create_variable("l")
    s = primal.create_variable("s")
    k = primal.create_variable("k")
    primal.add_constraint(8 * l + 6 * s + k <= 960)
    primal.add_constraint(8 * l + 4 * s + 3 * k <= 800)
    primal.add_constraint(4 * l + 3 * s + k <= 320)
    primal.maximize(60 * l + 30 * s + 20 * k)
    dual = primal.dual()
    primal_solution = primal.solve()
    print(dual)
    analyser = Analyser()
    analysis_results = analyser.analyse(primal_solution)
    analyser.interpret_results(primal_solution, analysis_results)
Ejemplo n.º 9
0
def run():
    primal = Model("zad 1")
    ss = primal.create_variable("ss")
    s = primal.create_variable("s")
    o = primal.create_variable("o")
    primal.add_constraint(2 * ss + 2 * s + 5 * o <= 40)
    primal.add_constraint(ss + 3 * s + 2 * o <= 30)
    primal.add_constraint(3 * ss + s + 3 * o <= 30)
    primal.maximize(32 * ss + 24 * s + 48 * o)
    dual = primal.dual()
    primal_solution = primal.solve()
    print(dual)
    analyser = Analyser()
    analysis_results = analyser.analyse(primal_solution)
    analyser.interpret_results(primal_solution, analysis_results)
def run():
    model = Model("example_05_unfeasible")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")

    model.add_constraint(2*x1 - x2 <= -1)
    model.add_constraint(x1 + x2 == 3)
    model.add_constraint(x1 + x2 >= 4)
    
    model.maximize(x1 + 3 * x2)

    solution = model.solve()
    assert solution.is_feasible == False, "Your algorithm found a solution to an unfeasible problem. This shouldn't happen..."
    logging.info("Congratulations! This problem is unfeasible and your algorithm has found that :)")
def run():
    model = Model("example_03_unbounded")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")
    x3 = model.create_variable("x3")

    model.add_constraint(x1 + 3*x2 + 2*x3 >= 10)
    model.add_constraint(x1 + 5*x2 + 1*x3 >= -7)

    model.maximize(5 * x1 + 8 * x2)

    solution = model.solve()

    assert solution.is_bounded == False, "Your algorithm found a solution to an unbounded problem. This shouldn't happen..."
    logging.info("Congratulations! This problem is unbounded and your algorithm has found that :)")
def run():
    model = Model("example_01_solvable")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")

    model.add_constraint(-1 * x1 <= 150)
    model.add_constraint(-1 * x2 <= 250)
    model.add_constraint(-2 * x1 - x2 <= 500)

    model.maximize(8 * x1 + 5 * x2)

    try:
        solution = model.solve()
    except UnboundedException:
        logging.info("Congratulations! You found an unfesiable solution :)")
    else:
        raise AssertionError(
            "This problem has no solution but your algorithm hasn't figured it out!"
        )
Ejemplo n.º 13
0
def run():
    model = Model("example_03")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")
    x3 = model.create_variable("x3")

    model.add_constraint(x1 + x2 + x3 >= -250)
    model.add_constraint(x1 + 100 * x2 + 1000 * x3 >= -459)
    model.add_constraint(260 * x1 + 2 * x2 + 500 * x3 >= -5156)

    model.maximize(80 * x1 + 45 * x2 + 2 * x3)

    try:
        solution = model.solve()
    except UnboundeLinearProgramException:
        logging.info("Congratulations! You found an unfesiable solution :)")
    else:
        raise AssertionError(
            "This problem has no solution but your algorithm hasn't figured it out!"
        )
Ejemplo n.º 14
0
def run():
    model = Model("example_03_unbounded")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")
    x3 = model.create_variable("x3")

    model.add_constraint(x1 + x2 >= -100)
    model.add_constraint(x1 - 2 * x3 >= -200)
    model.add_constraint(5 * x2 + 3 * x3 >= -300)

    model.maximize(9 * x1 + 9 * x2 + 7 * x3)

    try:
        solution = model.solve()
    except UnboundeLinearProgramException:
        logging.info("Congratulations! You found an infeasible solution!")
    else:
        raise AssertionError(
            "This problem has no solution but your algorithm hasn't figured it out!"
        )
Ejemplo n.º 15
0
def run():
    model = Model("example_05_unfeasible")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")

    model.add_constraint(x1 >= 150)
    model.add_constraint(x2 >= 250)
    model.add_constraint(2 * x1 + x2 <= 500)

    model.maximize(8 * x1 + 5 * x2)

    try:
        model.solve()
        raise AssertionError(
            "Your algorithm found a solution to an unfeasible problem. This shouldn't happen..."
        )
    except:
        logging.info(
            "Congratulations! This problem is unfeasible and your algorithm has found that :)"
        )
Ejemplo n.º 16
0
def run():
    #TODO:
    # fill missing test based on the example_01_solvable.py
    # to make the test a bit more interesting:
    # * make the model unfeasible in a way detectable by the 2-phase simplex
    #
    model = Model("ex5")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")

    model.maximize(x1)
    model.add_constraint(x1 + x2 == 10)
    model.add_constraint(x1 - x2 >= 100)

    try:
        solution = model.solve()
    except ErrorEx5:
        logging.info("OK")
    else:
        raise AssertionError("NOT OK!!!")
Ejemplo n.º 17
0
def run():
    model = Model("example_04_solvable_artificial_vars")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")

    model.add_constraint(2*x1 - x2 <= -1)
    model.add_constraint(x1 + x2 == 3)
    
    model.maximize(x1 + 3 * x2)

    try:
        solution = model.solve()
    except Exception as e:
        print(e)
        raise AssertionError("This problem has a solution and your algorithm hasn't found it!")

    logging.info(solution)

    assert (solution.assignment == [0.0, 3.0]), "Your algorithm found an incorrect solution!"

    logging.info("Congratulations! This solution seems to be alright :)")
Ejemplo n.º 18
0
def run():
    #TODO:
    # fill missing test based on the example_01_solvable.py
    # to make the test a bit more interesting:
    # * make the solver use artificial variables!
    model = Model("ex4")

    x1 = model.create_variable("x1")
    x2 = model.create_variable("x2")

    model.add_constraint(2*x1 - x2 <= -1)
    model.add_constraint(x1 + x2 == 3)

    model.maximize(x1 + 3*x2)

    try:
        solution = model.solve()
    except:
        raise AssertionError("Solution not found!")

    logging.info(solution)

    assert (solution.assignment == [0.0, 3.0]), "NOT OK!!!"
    logging.info("OK")
Ejemplo n.º 19
0
def run():
    primal = Model("Zad1")

    # x1 -> SS - super
    # x2 -> S - standardowy
    # x3 -> O - oszczędny

    x1 = primal.create_variable("x1")
    x2 = primal.create_variable("x2")
    x3 = primal.create_variable("x3")

    primal.add_constraint(2 * x1 + 2 * x2 + 5 * x3 <= 40)
    primal.add_constraint(x1 + 3 * x2 + 2 * x3 <= 30)
    primal.add_constraint(3 * x1 + x2 + 3 * x3 <= 30)

    primal.maximize(32 * x1 + 24 * x2 + 48 * x3)

    expected_dual = Model("Zad1")

    y1 = expected_dual.create_variable("y1")
    y2 = expected_dual.create_variable("y2")
    y3 = expected_dual.create_variable("y3")

    expected_dual.add_constraint(2 * y1 + y2 + 3 * y3 >= 32)
    expected_dual.add_constraint(2 * y1 + 3 * y2 + y3 >= 24)
    expected_dual.add_constraint(5 * y1 + 2 * y2 + 3 * y3 >= 48)

    expected_dual.minimize(40 * y1 + 30 * y2 + 30 * y3)

    expected_bounds = [(19.6363636363, 44.57142857),
                       (17.77777777, 53.333333333), (37.0, 61.99999999999)]

    dual = primal.dual()
    assert dual.is_equivalent(
        expected_dual), "dual wasn't calculated as expected"
    assert primal.is_equivalent(
        dual.dual()), "double dual should equal the initial model"

    primal_solution = primal.solve()
    dual_solution = dual.solve()
    assert math.isclose(
        primal_solution.objective_value(),
        dual_solution.objective_value(),
        abs_tol=0.000001
    ), "dual and primal should have the same value at optimum"

    logging.info(
        "Congratulations! The dual creation seems to be implemented correctly :)\n"
    )

    analyser = Analyser()
    analysis_results = analyser.analyse(primal_solution)
    analyser.interpret_results(primal_solution, analysis_results, logging.info)

    objective_analysis_results = analysis_results[
        ObjectiveSensitivityAnalyser.name()]
    tolerance = 0.001

    for (i, bounds_pair) in enumerate(objective_analysis_results):
        assert math.isclose(
            bounds_pair[0], expected_bounds[i][0], abs_tol=tolerance
        ), f"left bound of the coefficient range seems to be incorrect, expected {expected_bounds[i][0]}, got {bounds_pair[0]}"
        assert math.isclose(
            bounds_pair[1], expected_bounds[i][1], abs_tol=tolerance
        ), f"right bound of the coefficient range seems to be incorrect, expected {expected_bounds[i][1]}, got {bounds_pair[1]}"

    logging.info(
        "Congratulations! This cost coefficients analysis look alright :)")

    print(dual)
    print(analyser.interpret_results(primal_solution, analysis_results))
Ejemplo n.º 20
0
def run():
    primal = Model("Zad2")

    # x1 -> ławki
    # x2 -> stoły
    # x3 -> krzesła

    x1 = primal.create_variable("x1")
    x2 = primal.create_variable("x2")
    x3 = primal.create_variable("x3")

    primal.add_constraint(8 * x1 + 6 * x2 + x3 <= 960)
    primal.add_constraint(8 * x1 + 4 * x2 + 3 * x3 <= 800)
    primal.add_constraint(4 * x1 + 3 * x2 + x3 <= 320)

    primal.maximize(60 * x1 + 30 * x2 + 20 * x3)

    expected_dual = Model("Zad2")

    y1 = expected_dual.create_variable("y1")
    y2 = expected_dual.create_variable("y2")
    y3 = expected_dual.create_variable("y3")

    expected_dual.add_constraint(8 * y1 + 8 * y2 + 4 * y3 >= 60)
    expected_dual.add_constraint(6 * y1 + 4 * y2 + 3 * y3 >= 30)
    expected_dual.add_constraint(y1 + 3 * y2 + y3 >= 20)

    expected_dual.minimize(960 * y1 + 800 * y2 + 320 * y3)

    expected_bounds = [(56.0, 80.0), (float("-inf"), 35.0), (15.0, 22.5)]

    dual = primal.dual()
    assert dual.is_equivalent(
        expected_dual), "dual wasn't calculated as expected"
    assert primal.is_equivalent(
        dual.dual()), "double dual should equal the initial model"

    primal_solution = primal.solve()
    dual_solution = dual.solve()
    assert math.isclose(
        primal_solution.objective_value(),
        dual_solution.objective_value(),
        abs_tol=0.000001
    ), "dual and primal should have the same value at optimum"

    logging.info(
        "Congratulations! The dual creation seems to be implemented correctly :)\n"
    )

    analyser = Analyser()
    analysis_results = analyser.analyse(primal_solution)
    analyser.interpret_results(primal_solution, analysis_results, logging.info)

    objective_analysis_results = analysis_results[
        ObjectiveSensitivityAnalyser.name()]
    tolerance = 0.001

    for (i, bounds_pair) in enumerate(objective_analysis_results):
        assert math.isclose(
            bounds_pair[0], expected_bounds[i][0], abs_tol=tolerance
        ), f"left bound of the coefficient range seems to be incorrect, expected {expected_bounds[i][0]}, got {bounds_pair[0]}"
        assert math.isclose(
            bounds_pair[1], expected_bounds[i][1], abs_tol=tolerance
        ), f"right bound of the coefficient range seems to be incorrect, expected {expected_bounds[i][1]}, got {bounds_pair[1]}"

    logging.info(
        "Congratulations! This cost coefficients analysis look alright :)")

    print(dual)
    print(analyser.interpret_results(primal_solution, analysis_results))
Ejemplo n.º 21
0
# remember to print the dual model (just print()) and the analysis results (analyser.interpret_results(solution, analysis_results))
# in case of doubt refer to examples 06 and 07

from saport.simplex.analyser import Analyser
from saport.simplex.model import Model

model = Model("Zad2")

x1 = model.create_variable("x1")
x2 = model.create_variable("x2")
x3 = model.create_variable("x3")

model.add_constraint(8 * x1 + 6 * x2 + x3 <= 960)
model.add_constraint(8 * x1 + 4 * x2 + 3 * x3 <= 800)
model.add_constraint(4 * x1 + 3 * x2 + x3 <= 320)

model.maximize(60 * x1 + 30 * x2 + 20 * x3)

dual = model.dual()

print(dual)

solution = model.solve()

analyser = Analyser()
analysis_result = analyser.analyse(solution)
analyser.interpret_results(solution, analysis_result)
Ejemplo n.º 22
0
# 3. Add variables
x1 = model.create_variable("x1")
x2 = model.create_variable("x2")
x3 = model.create_variable("x3")
x4 = model.create_variable("x4")

# 4. FYI: You can create expression and evaluate them
# expr2 = 0.16 * x1 - 0.94 * x2 + 0.9 * x3

# 5. Then add constraints to the model
model.add_constraint(2.0 * x1 + 4.0 * x2 + 3.0 * x3 + 7.0 * x4 <= 800)
model.add_constraint(4.0 * x1 + 5.0 * x2 + 3.0 * x3 + 2.0 * x4 <= 640)
model.add_constraint(4.0 * x1 + 1.0 * x2 + 4.0 * x3 + 1.0 * x4 <= 600)

# 6. Set the objective!
model.maximize(80.0 * x1 + 60.0 * x2 + 30.0 * x3 + 50.0 * x4)

# 7. You can print the model
print("Before solving:")
print(model)

# 8. And finally solve it!
solution = model.solve()

# 9. Model is being simplified before being solver
print("After solving:")
print(model)

# 10. Print solution (uncomment after finishing assignment)
print("Solution: ")
print(solution)
Ejemplo n.º 23
0
# 3. Add variables
x1 = model.create_variable("x1")
x2 = model.create_variable("x2")
x3 = model.create_variable("x3")

# 4. FYI: You can create expression and evaluate them
# expr2 = 0.16 * x1 - 0.94 * x2 + 0.9 * x3

# 5. Then add constraints to the model
model.add_constraint(1.0 * x1 + 1.0 * x2 + 1.0 * x3 == 30)
model.add_constraint(1.0 * x1 + 2.0 * x2 + 1.0 * x3 >= 10)
model.add_constraint(0.0 * x1 + 2.0 * x2 + 1.0 * x3 <= 20)

# 6. Set the objective!
model.maximize(2.0 * x1 + 1.0 * x2 + 3.0 * x3)

# 7. You can print the model
print("Before solving:")
print(model)

# 8. And finally solve it!
solution = model.solve()

# 9. Model is being simplified before being solver
print("After solving:")
print(model)

# 10. Print solution (uncomment after finishing assignment)
print("Solution: ")
print(solution)
Ejemplo n.º 24
0
from saport.simplex.model import Model 

model = Model("z1")

x = model.create_variable("x")
y = model.create_variable("y")
z = model.create_variable("z")

model.add_constraint(x + y + z <= 30)
model.add_constraint(x + 2*y + z >= 10)
model.add_constraint(2 * y + z <= 20)

model.maximize(2 * x + y + 3 * z)

solution = model.solve()
print(solution)

Ejemplo n.º 25
0
from saport.simplex.analyser import Analyser
from saport.simplex.model import Model

# remember to print the dual model (just print()) and the analysis results (analyser.interpret_results(solution, analysis_results))
# in case of doubt refer to examples 06 and 07

primal = Model("Zad1")

ss = primal.create_variable("SS")
s = primal.create_variable("S")
o = primal.create_variable("O")

primal.add_constraint(2 * ss + 2 * s + 5 * o <= 40)
primal.add_constraint(ss + 3 * s + 2 * o <= 30)
primal.add_constraint(3 * ss + s + 3 * o <= 30)

primal.maximize(32 * ss + 24 * s + 48 * o)

dual = primal.dual()

print(dual)

solution = primal.solve()

analyser = Analyser()
analysis_results = analyser.analyse(solution)
analyser.interpret_results(solution, analysis_results)

Ejemplo n.º 26
0
# TODO: model and solve assignment 1 from the PDF
# How to use SAPORT?

# 1. Import the library
from saport.simplex.solver import Solver
from saport.simplex.model import Model

model = Model("Zadanie 1")

x1 = model.create_variable("x1")
x2 = model.create_variable("x2")
x3 = model.create_variable("x3")

expr1 = x1 + x2 + x3
expr2 = x1 + 2*x2 + x3
expr3 = 2*x2 + x3

model.add_constraint(expr1 <= 30)
model.add_constraint(expr2 >= 10)
model.add_constraint(expr3 <= 20)

model.maximize(2* x1 + x2 + 3* x3)

print("Before solving:")
print(model)

solution = model.solve()

print("Solution: ")
print(solution)
Ejemplo n.º 27
0
# 3. Add variables
x1 = model.create_variable("x1")
x2 = model.create_variable("x2")
x3 = model.create_variable("x3")

# 4. FYI: You can create expression and evaluate them
expr1 = 0.16 * x1 - 0.94 * x2 + 0.9 * x3
print(f"Value of the expression for the specified assignment is  {expr1.evaluate([1, 1, 2])}\n")

# 5. Then add constraints to the model
model.add_constraint(expr1 <= 1200)
model.add_constraint(0.2 * x2 + 0.3 * x2 + 0.3 * x3 + 0.1 * x1 <= 600)

# 6. Set the objective!
model.maximize(1.1 * x1 + 3.4 * x2 + 2.2 * x3)

# 7. You can print the model
print("Before solving:")
print(model)

# 8. And finally solve it!
solution = model.solve()

# 9. Model is being simplified before being solver
print("After solving:")
print(model)

# 10. Print solution (uncomment after finishing assignment)
print("Solution: ")
print(solution)