def main(): bounds = [] s = pywraplp.Solver('', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) a = [1, 2] x = [s.NumVar(3, 5, 'x[%i]' % i) for i in range(2)] b = 10 bounds = bounds_on_box(a, x, b) print(bounds == [-1, 5])
def main(): a, b = [2, 3], 5 s = pywraplp.Solver('Test Box', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) x = [s.NumVar(-1, 6, ''), s.NumVar(-3, 5, '')] bounds = bounds_on_box(a, x, b) print(bounds == [-16, 22])
def main(): # Test force bounds = [] s = pywraplp.Solver('', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) a = [[0, 1], [1, 0]] b = [4, 5] x = [s.IntVar(0, 10, 'x[%i]' % i) for i in range(2)] bounds, delta, gamma = [], [], [] for j in range(len(a)): bounds.append(bounds_on_box(a[j], x, b[j])) d = reify_force(s, a[j], x, b[j], rel='==') delta.append(d) s.Maximize(x[0] + x[1]) rc = s.Solve() if rc == 0: print(rc == 0, ObjVal(s) == 20, SolVal(delta) == [0, 0], SolVal(x) == [10, 10]) else: print(rc) s.Add(delta[0] == 1) rc = s.Solve() if rc == 0: print(rc == 0, ObjVal(s) == 14, SolVal(delta) == [1, 0], SolVal(x) == [10, 4]) else: print(rc) s.Add(delta[1] == 1) #s.Add(a[0][0]*x[0]+a[0][1]*x[1] == b[0]) #s.Add(a[1][0]*x[0]+a[1][1]*x[1] == b[1]) rc = s.Solve() if rc == 0: print(rc == 0, ObjVal(s) == 9, SolVal(delta) == [1, 1], SolVal(x) == [5, 4]) else: print(rc) # Test raise s = pywraplp.Solver('', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) a = [[0, 1], [1, 0]] b = [4, 5] x = [s.IntVar(0, 10, 'x[%i]' % i) for i in range(2)] bounds, delta, gamma = [], [], [] for j in range(len(a)): bounds.append(bounds_on_box(a[j], x, b[j])) d = reify_raise(s, a[j], x, b[j], rel='==') delta.append(d) s.Minimize(x[0] + x[1] + delta[0] + delta[1]) rc = s.Solve() if rc == 0: #print rc,ObjVal(s),SolVal(delta),SolVal(x) print(rc == 0, ObjVal(s) == 0, SolVal(delta) == [0, 0], SolVal(x) == [0, 0]) else: print(rc) s.Add(a[0][0] * x[0] + a[0][1] * x[1] == b[0]) #s.Add(delta[0] == 1) rc = s.Solve() if rc == 0: #print rc,ObjVal(s),SolVal(delta),SolVal(x) print(rc == 0, ObjVal(s) == 5, SolVal(delta) == [1, 0], SolVal(x) == [0, 4]) else: print(rc) #s.Add(delta[1] == 1) s.Add(a[1][0] * x[0] + a[1][1] * x[1] == b[1]) rc = s.Solve() if rc == 0: #print rc,ObjVal(s),SolVal(delta),SolVal(x) print(rc == 0, ObjVal(s) == 11, SolVal(delta) == [1, 1], SolVal(x) == [5, 4]) else: print(rc) # Test iff s = pywraplp.Solver('', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) a = [[0, 1], [1, 0]] b = [4, 5] x = [s.IntVar(0, 10, 'x[%i]' % i) for i in range(2)] q = [s.IntVar(0, 1, '') for _ in range(2)] bounds, delta, gamma = [], [], [] for j in range(len(a)): bounds.append(bounds_on_box(a[j], x, b[j])) d = reify(s, a[j], x, b[j], rel='==') delta.append(d) s.Minimize(x[0] + x[1]) sosn(s, 1, q, '==') rc = s.Solve() if rc == 0: #print rc,ObjVal(s),SolVal(delta),SolVal(x) print(rc == 0, ObjVal(s) == 0, SolVal(delta) == [0, 0], SolVal(x) == [0, 0], sum(SolVal(q)) == 1) else: print(rc) s.Add(a[0][0] * x[0] + a[0][1] * x[1] == b[0]) #s.Add(delta[0] == 1) rc = s.Solve() if rc == 0: #print rc,ObjVal(s),SolVal(delta),SolVal(x) print(rc == 0, ObjVal(s) == 4, SolVal(delta) == [1, 0], SolVal(x) == [0, 4]) else: print(rc) #s.Add(delta[1] == 1) s.Add(a[1][0] * x[0] + a[1][1] * x[1] == b[1]) rc = s.Solve() if rc == 0: #print rc,ObjVal(s),SolVal(delta),SolVal(x) print(rc == 0, ObjVal(s) == 9, SolVal(delta) == [1, 1], SolVal(x) == [5, 4]) else: print(rc)