コード例 #1
0
ファイル: bab_test.py プロジェクト: coverney/SpecialTopicsHW
def problem1():
    print("Problem 1")
    fail_count = 0

    x = RealVariable("x")
    y = RealVariable("y")
    z = RealVariable("z")
    vars = [x, y, z]
    objective = z
    constraints = [z == x + y, -5*x + 4* y <= 0, 6*x+2*y <= 17, x >= 0, y >= 0]
    root = BBTreeNode(constraints = constraints, objective = objective, vars = vars)
    res, sol_vars = root.bbsolve()

    correct_vals = [2.0, 2.0, 4.0]

    check_index = 0

    while check_index < len(correct_vals):
        try:
            assert(abs(correct_vals[check_index] - float(sol_vars[check_index])) < 1e-4)
        except AssertionError:
            print("Test case 1 failed on variable at index " + str(check_index))
            fail_count = 1
        check_index += 1

    return fail_count
コード例 #2
0
ファイル: bab_test.py プロジェクト: coverney/SpecialTopicsHW
def problem3():
    print("Problem 3")
    fail_count = 0
    x = RealVariable("x")
    y = RealVariable("y")
    z = RealVariable("z")
    vars = [x, y, z]
    objective = z
    constraints = [z == 4*x+5*y, 3*x +1 * y <= 27, 6 *x + 4*y >= 6, 5 * x + 5 * y >= 1, x >= 0, y >=0]
    root = BBTreeNode(constraints = constraints, objective = objective, vars = vars)
    res, sol_vars = root.bbsolve()

    correct_vals = [0.0, 27.0, 135]

    check_index = 0

    while check_index < len(correct_vals):
        try:
            assert(abs(correct_vals[check_index] - float(sol_vars[check_index])) < 1e-4)
        except AssertionError:
            print("Test case 3 failed on variable at index " + str(check_index))
            fail_count = 1
        check_index += 1

    return fail_count
コード例 #3
0
ファイル: bab_test.py プロジェクト: coverney/SpecialTopicsHW
def problem2():
    print("Problem 2")
    fail_count = 0
    x = RealVariable("x")
    y = RealVariable("y")
    z = RealVariable("z")
    vars = [x, y, z]
    objective = z
    constraints = [x+y <= 7, 12*x+ 5*y <= 60, x >= 0, y >=0, z == 80 * x + 45 * y]
    root = BBTreeNode(constraints = constraints, objective = objective, vars = vars)
    res, sol_vars = root.bbsolve()

    correct_vals = [3.0, 4.0, 420]

    check_index = 0

    while check_index < len(correct_vals):
        try:
            assert(abs(correct_vals[check_index] - float(sol_vars[check_index])) < 1e-4)
        except AssertionError:
            print("Test case 2 failed on variable at index " + str(check_index))
            fail_count = 1
        check_index += 1

    return fail_count
コード例 #4
0
ファイル: bab_test.py プロジェクト: aselker/SpecialTopicsHW
def problem5():
    print("Problem 5")
    fail_count = 0
    x = RealVariable("x")
    y = RealVariable("y")
    z = RealVariable("z")
    vars = [x, y, z]
    objective = z
    constraints = [
        z == 5 * x + 6 * y, x + y <= 5, 4 * x + 7 * y <= 28, x >= 0, y >= 0
    ]
    root = BBTreeNode(constraints=constraints, objective=objective, vars=vars)
    res, sol_vars = root.bbsolve()

    correct_vals = [3.0, 2.0, 27]

    check_index = 0

    while check_index < len(correct_vals):
        try:
            assert abs(correct_vals[check_index] -
                       float(sol_vars[check_index])) < 1e-4
        except AssertionError:
            print("Test case 5 failed on variable at index " +
                  str(check_index))
            fail_count = 1
        check_index += 1

    return fail_count
コード例 #5
0
ファイル: bab_test.py プロジェクト: coverney/SpecialTopicsHW
def problem6():
    print("Problem 6")
    fail_count = 0
    x = RealVariable("x")
    y = RealVariable("y")
    z = RealVariable("z")
    a= RealVariable("a")
    b= RealVariable("b")
    c = RealVariable("c")
    vars = [x, y, a, b, c,  z]
    objective = z
    constraints = [z == 15*x + 20 *y + 18* a + 13 * b + 12* c, 18*x+10*y+21*a+11*b+11*c <= 50, x>= 0, y >= 0, a >= 0, b >= 0, c >= 0, x <= 1, y <= 1, a <= 1, b <= 1, c <= 1]
    root = BBTreeNode(constraints = constraints, objective = objective, vars = vars)
    res, sol_vars = root.bbsolve()

    correct_vals = [1.0, 1.0, 0.0,1.0, 1.0, 60.0]

    check_index = 0

    while check_index < len(correct_vals):
        try:
            assert(abs(correct_vals[check_index] - float(sol_vars[check_index])) < 1e-4)
        except AssertionError:
            print("Test case 6 failed on variable at index " + str(check_index))
            fail_count = 1
        check_index += 1

    return fail_count
コード例 #6
0
ファイル: solver.py プロジェクト: coverney/SpecialTopicsHW
def cvrp_ip(C,q,K,Q,obj=True):
    '''
    Solves the capacitated vehicle routing problem using an integer programming
    approach.

    C: matrix of edge costs, that represent distances between each node
    q: list of demands associated with each client node
    K: number of vehicles
    Q: capacity of each vehicle
    obj: whether to set objective (ignore unless you are doing local search)
    returns:
        objective_value: value of the minimum travel cost
        x: matrix representing number of routes that use each arc
    '''
    # Add in destination node
    # It should have the same distances as source and its demand equals 0
    q2 = np.append(q, 0)
    col = C[:,0]
    C2 = np.hstack((C, np.atleast_2d(col).T))
    row = C2[0,:]
    C3 = np.vstack ((C2, row) )
    # set up the picos problem
    prob = pic.Problem()
    K_value = K
    Q_value = Q
    # define variables
    N = Constant("N", range(len(q2))) # clients {1,2,...N} plus destination, column vector
    K = Constant("K", K_value) # number of vehicles
    Q = Constant("Q", Q_value) # vehicle capacity
    q = Constant("q", q2) # demand for each client
    c = Constant("c", C3, C3.shape) # travel cost from i to j
    x = BinaryVariable("x", C3.shape) # whether edge is in the tour
    u = RealVariable("u", len(q2)) # cumulative quantity of good delivered between origin and client i
    prob.set_objective("min", pic.sum([c[i,j]*x[i,j] for j in range(C3.shape[1]) for i in range(C3.shape[0])])) # double check this
    # add constraints
    # all u values should be >= q and <= Q
    for i in range(len(q2)):
        prob.add_constraint(u[i] >= q[i])
        prob.add_constraint(u[i] <= Q)
    # for all edges, u_i-u_j+Q*x <= Q - qj
    for i in range(C3.shape[0]) :
        for j in range(C3.shape[1]):
            prob.add_constraint(u[i] - u[j] + Q * x[i,j] <= Q - q[j])
    # make sure that only 1 vehicle leaves every client
    # make sure rows of x sum to 1
    for n in N[1:-1]:
        prob.add_constraint(pic.sum(x[n,:])==1)
    # make sure that only 1 vehicle enters every client
    # make sure cols of x sum to 1
    for n in N[1:-1]:
        prob.add_constraint(pic.sum(x[:,n])==1)
    # make sure that no more than K vehicles leave the origin
    prob.add_constraint(pic.sum(x[0,:]) <= K)
    # make sure that no more than K vehicles enter the origin
    prob.add_constraint(pic.sum(x[:,-1]) <= K)
    # make sure that the number of vehicles that leave the origin should
    # equal the number of vehicles coming back in
    prob.add_constraint(pic.sum(x[0,:]) == pic.sum(x[:,-1]))
    # solve integer program
    res = prob.solve(solver='cplex')
    objective_value = pic.sum([c[i,j]*x[i,j] for j in range(C3.shape[1]) for i in range(C3.shape[0])])
    return objective_value, x