Пример #1
0
def run():
    model = Model("example_02_solvable")

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

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

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

    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 == [
        0.0, 0.0, 0.0, 100.0, 200.0, 300.0
    ]), "Your algorithm found an incorrect solution!"

    logging.info("Congratulations! This solution seems to be alright!")
Пример #2
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 :)")
Пример #3
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 :)")
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 :)")
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 :)")
Пример #6
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)
Пример #7
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)
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 :)")
Пример #9
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("example_05_unfeasible")

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

    model.add_constraint(x1 >= 6)
    model.add_constraint(x2 >= 6)
    model.add_constraint(x1 + x2 <= 11)

    model.minimize(x1 + x2)

    try:
        model.solve()
        raise AssertionError(
            "Your algorithm found a solution to an unfeasible problem. This shouldn't happen..."
        )
    except Exception as e:
        logging.info(
            "Congratulations! This problem is unfeasible and your algorithm has found that :) \n"
            + "Alghoritm raise an exception: " + e.__str__())
Пример #10
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 :)")
Пример #11
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():
    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_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.minimize(5 * x1 + 8 * x2)

    try:
        model.solve()
        raise AssertionError("Your algorithm found a solution to an unbounded problem. This shouldn't happen...")
    except:
        logging.info("Congratulations! This problem is unbounded and your algorithm has found that :)")
Пример #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!"
        )
Пример #15
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!"
        )
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")
    x4 = model.create_variable("x4")

    model.add_constraint(-2 * x1 - 4 * x2 - 3 * x3 - 7 * x4 >= -800)
    model.add_constraint(-4 * x1 - 5 * x2 - 3 * x3 - 2 * x4 >= -640)
    model.add_constraint(-4 * x1 - 1 * x2 - 4 * x3 - 1 * x4 >= -600)

    model.minimize(-80 * x1 - 60 * x2 - 30 * x3 - 50 * x4)

    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 == [
        120.0, 0, 0, 80.0, 0.0, 0.0, 40
    ]), "Your algorithm found an incorrect solution!"

    logging.info("Congratulations! This solution seems to be alright :)")
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_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!"
        )
Пример #19
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 :)"
        )
Пример #20
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!!!")
Пример #21
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 :)")
Пример #22
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")
Пример #23
0
from saport.simplex.model import Model

model = Model("z3")

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

model.add_constraint(15 * x + 2 * y <= 60)
model.add_constraint(5 * x + 15 * y >= 50)
model.add_constraint(20 * x + 5 * y >= 40)

model.minimize(8 * x + 4 * y)

solution = model.solve()
print(solution)
Пример #24
0
# TODO: model and solve assignment 2 from the PDF

from saport.simplex.solver import Solver
from saport.simplex.model import Model

model = Model("Zadanie 2")

x1 = model.create_variable("p1")
x2 = model.create_variable("p2")
x3 = model.create_variable("p3")
x4 = model.create_variable("p4")

expr1 = 0.8 * x1 + 2.4 * x2 + 0.9 * x3 + 0.4 * x4
expr2 = 0.6 * x1 + 0.6 * x2 + 0.3 * x3 + 0.3 * x4

model.add_constraint(expr1 >= 1200)
model.add_constraint(expr2 >= 600)

model.minimize(9.6 * x1 + 14.4 * x2 + 10.8 * x3 + 7.2 * x4)

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

solution = model.solve()

print("Solution: ")
print(solution)
Пример #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)

Пример #26
0
# TODO: model and solve assignment 4 from the PDF
# How to use SAPORT?

from saport.simplex.solver import Solver
from saport.simplex.model import Model

model = Model("Zadanie 4")

# LICZBA SPOSOBÓW OGRANICZONA DO 5 PONIEWAŻ INNE NIE MAJĄ SENSU I MOŻNA JE ODRZUCIC
x1 = model.create_variable("sps_1")  #1x 105cm + 1x 75cm + 0x35cm => odpad: 20
x2 = model.create_variable("sps_2")  #1x 105cm + 0x 75cm + 2x35cm => odpad: 25
x3 = model.create_variable("sps_3")  #0x 105cm + 2x 75cm + 1x35cm => odpad: 15
x4 = model.create_variable("sps_4")  #0x 105cm + 1x 75cm + 3x35cm => odpad: 20
x5 = model.create_variable("sps_5")  #0x 105cm + 0x 75cm + 5x35cm => odpad: 25

expr1 = x1 + x2
expr2 = x1 + 2 * x3 + x4
expr3 = 2 * x2 + x3 + 3 * x4 + 5 * x5

model.add_constraint(expr1 == 150)
model.add_constraint(expr2 == 200)
model.add_constraint(expr3 == 150)

model.minimize(20 * x1 + 25 * x2 + 15 * x3 + 20 * x4 + 25 * x5)

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

solution = model.solve()

print("Solution: ")