f = some_lin_funcs[15] + some_lin_funcs[80] - sum(a) + sum(b) point = {} for i in range(N): point[a[i]] = 1.5 * i**2 point[b[i]] = 1.5 * i**3 # BTW we could use 1-dimensional arrays here, eg point[a[25]] = [2,3,4,5], point[a[27]] = [1,2,5] etc print f(point) # prints 40899980. from openopt import LP # define prob p = LP(f, point) # add some box-bound constraints aLBs = [a[i]>-10 for i in range(N)] bLBs = [b[i]>-10 for i in range(N)] aUBs = [a[i]<15 for i in range(N)] bUBs = [b[i]<15 for i in range(N)] p.constraints = aLBs + bLBs + aUBs + bUBs # add some general linear constraints p.constraints.append(a[4] + b[15] + a[20].size - f.size>-9) # array size, here a[20].size = f.size = 1 # or p.constraints += [a[4] + b[15] + a[20].size - f.size>-9] for i in range(N): p.constraints.append(2 * some_lin_funcs[i] + a[i] < i / 2.0 + some_lin_funcs[N-i-1] + 1.5*b[i]) # or p.constraints += [2 * some_lin_funcs[i] + a[i] < i / 2.0 + some_lin_funcs[N-i-1] + 1.5*b[i] for i in range(N] # solve r = p.solve('cplex') print('opt a[15]=%f'%a[15](r)) # opt a[15]=-10.000000
for i in range(N): point[a[i]] = 1.5 * i**2 point[b[i]] = 1.5 * i**3 # BTW we could use 1-dimensional arrays here, eg point[a[25]] = [2,3,4,5], point[a[27]] = [1,2,5] etc print f(point) # prints 40899980. from openopt import LP # define prob p = LP(f, point) # add some box-bound constraints aLBs = [a[i] > -10 for i in range(N)] bLBs = [b[i] > -10 for i in range(N)] aUBs = [a[i] < 15 for i in range(N)] bUBs = [b[i] < 15 for i in range(N)] p.constraints = aLBs + bLBs + aUBs + bUBs # add some general linear constraints p.constraints.append(a[4] + b[15] + a[20].size - f.size > -9) # array size, here a[20].size = f.size = 1 # or p.constraints += [a[4] + b[15] + a[20].size - f.size>-9] for i in range(N): p.constraints.append(2 * some_lin_funcs[i] + a[i] < i / 2.0 + some_lin_funcs[N - i - 1] + 1.5 * b[i]) # or p.constraints += [2 * some_lin_funcs[i] + a[i] < i / 2.0 + some_lin_funcs[N-i-1] + 1.5*b[i] for i in range(N] # solve r = p.solve('cplex') print('opt a[15]=%f' % a[15](r)) # opt a[15]=-10.000000
f3 = 5 * f1 + 4 * f2 + 20 # Define objective; sum(a) and a.sum() are same as well as for numpy arrays obj = sum(f1) + x.sum() + y - 50 * z + 2 * f2.sum() + 4064.6 # Start point - currently matters only size of variables startPoint = { x: [8, 15], y: 25, z: 80 } # however, using numpy.arrays is more recommended than Python lists # Create prob p = LP(obj, startPoint) # Define some constraints p.constraints = [ x + 5 * y < 15, x[0] < 4, f1 < [25, 35], f1 > -100, 2 * f1 + 4 * z < [80, 800], 5 * f2 + 4 * z < 100, -5 < x, x < 1, -20 < y, y < 20, -4000 < z, z < 4 ] # Solve r = p.solve( 'lpSolve', fixedVars=t ) # glpk is name of solver involved, see OOF doc for more arguments # Decode solution print('Solution: x = %s y = %f z = %f' % (r(x), r(y), r(z))) # Solution: x = [-4.25 -4.25] y = -20.000000 z = 4.000000
from FuncDesigner import * from openopt import LP # Define some oovars x, y, z = oovars(3) # Let's define some linear functions f1 = 4*x+5*y + 3*z + 5 f2 = f1.sum() + 2*x + 4*y + 15 f3 = 5*f1 + 4*f2 + 20 # Define objective; sum(a) and a.sum() are same as well as for numpy arrays obj = sum(f1)+ x.sum()+ y - 50*z + 2*f2.sum() + 4064.6 # Start point - currently matters only size of variables startPoint = {x:[8, 15], y:25, z:80} # however, using numpy.arrays is more recommended than Python lists # Create prob p = LP(obj, startPoint) # Define some constraints p.constraints = [x+5*y<15, x[0]<4, f1<[25, 35], f1>-100, 2*f1+4*z<[80, 800], 5*f2+4*z<100, -5<x, x<1, -20<y, y<20, -4000<z, z<4] # Solve r = p.solve('lpSolve', fixedVars=t) # glpk is name of solver involved, see OOF doc for more arguments # Decode solution print('Solution: x = %s y = %f z = %f' % (r(x), r(y), r(z))) # Solution: x = [-4.25 -4.25] y = -20.000000 z = 4.000000