Пример #1
0
def main():
    t = 'k out of n'
    n = 9
    seed(100)
    bound = [randint(5, 15) for _ in range(n)]
    tableutils.printmat([['Max sum of'] + bound])
    for k in range(1, n + 1):
        s = pywraplp.Solver(t, pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
        x = [s.NumVar(0, bound[i], '') for i in range(n)]
        y = [s.NumVar(0, bound[i], '') for i in range(n)]
        Costx = sum(x[i] for i in range(n))
        Costy = sum(y[i] for i in range(n))
        s.Maximize(Costx + Costy)
        k_out_of_n(s, k, x, '==')
        ldg = sosn(s, k, y)
        rc = s.Solve()
        if rc != 0:
            print('Error', rc)
        sy = SolVal(y)
        sx = SolVal(x)
        yy = [[' ', 'x'][e > 0] for e in sy]
        xx = [[' ', 'x'][e > 0] for e in sx]

        tableutils.printmat(
            tableutils.wrapmat(
                [xx, yy],
                ['{0}/{1}'.format(k, n), 'Adjacent {0}/{1}'.format(k, n)],
                None), 0, False)
    return rc
Пример #2
0
def minimize_piecewise_linear(Points,B,convex=True):
    s,n = newSolver('Piecewise', True),len(Points)
    x = s.NumVar(Points[0][0],Points[n-1][0],'x')
    l = [s.NumVar(0,1,'l[%i]' % (i,)) for i in range(n)]  
    s.Add(1 == sum(l[i] for i in range(n)))               
    d = sosn(s, 2, l)
    s.Add(x == sum(l[i]*Points[i][0] for i in range(n)))  
    s.Add(x >= B)                                                 
    Cost = s.Sum(l[i]*Points[i][1] for i in range(n))     
    s.Minimize(Cost)
    rc = s.Solve()
    return rc,SolVal(l),SolVal(d[1])
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)