from FuncDesigner import * from openopt import MINLP a, b, c = oovars('a', 'b', 'c') d = oovar('d', domain = [1, 2, 3, 3.5, -0.5, -4]) # domain should be Python list/set/tuple of allowed values e = oovar('e', domain = [1, 2, 3, 3.5, -0.5, -4]) a=oovars(2)('a') startPoint = {a:[100, 12], b:2, c:40, d:1, e:2} # however, you'd better use numpy arrays instead of Python lists f = sum(a*[1, 2])**2 + b**2 + c**2 + d**2+e**2 constraints = [(2*c+a-10)**2 < 1.5 + 0.1*b, (a-10)**2<1.5, a[0]>8.9, (a+b > [ 7.97999836, 7.8552538 ])('sum_a_b', tol=1.00000e-12), \ a < 9, b < -1.02, c > 1.01, ((b + c * log10(a).sum() - 1) ** 2==0)(tol=1e-6), b+d**2 < 100] p = MINLP(f, startPoint, constraints = constraints) r = p.minimize('interalg', iprint=10, fTol = 1e-8, implicitBounds=1000) print(r.xf) a_opt, b_opt, c_opt, d_opt = r(a, b, c, d) # or any of the following: # a_opt, b_opt, c_opt,d_opt = r(a), r(b), r(c),r(d) # r('a'), r('b'), r('c'), r('d') (provided you have assigned the names to oovars as above) # r('a', 'b', 'c', 'd') # a(r), b(r), c(r), d(r) """ Expected output: ... OpenOpt info: Solution with required tolerance 1.0e-08 is guarantied Solver: Time Elapsed = 4.72 CPU Time Elapsed = 4.48 objFunValue: 718.2574 (feasible, max(residuals/requiredTolerances) = 0.00144128) {b: -1.0200000004647196, c: 1.0618036990619659, d: -0.5, e: -0.5, a_0: 8.9999983590234365, a_1: 8.8752537991469964} """
new_ineq_vec = vstack( (ineq_constraint_matrix, -1 * equal_constraint_matrix)).tocsr() objective = lambda x: objective_helper(phi_mat * x, number_captains=number_captains) ineq_constraint = lambda x: ineq_matrix * x - ineq_constraint_matrix equal_constraint = lambda x: equal_matrix * (sts_mat * x ) - equal_constraint_matrix new_ineq_constraint = lambda x: new_ineq_mat * x - new_ineq_vec #print (new_ineq_mat * x)[-72:] #print new_ineq_vec.shape #t1 = equal_matrix * np.matrix(np.zeros(len(x))).T - equal_constraint_matrix #equal_constraint(np.matrix(np.zeros(len(x))).T) # test the equality constraint...something fishy is going on #p = MINLP(f = objective, x0 = np.matrix(np.zeros(len(x))).T, c = ineq_constraint, h = equal_constraint) p = MINLP(f=objective, x0=x, c=ineq_constraint, h=equal_constraint) #p = MINLP(f = objective, x0 = x, c = new_ineq_constraint) #p = MINLP(f = objective, x0 = x, A = ineq_matrix, b = ineq_constraint_matrix) p.discreteVars = mm.build_dictionary(number_captains) nlpSolver = 'ipopt' p.lb = [0] * len(x) p.ub = [1] * len(x) #r = p.solve('branb', nlpSolver = nlpSolver, plot = False)
from openopt import MINLP from openopt import MINLP from numpy import * N = 150 K = 50 #objective function: f = lambda x: ((x-5.45)**2).sum() #optional: 1st derivatives df = lambda x: 2*(x-5.45) # start point x0 = 8*cos(arange(N)) p = MINLP(f, x0, df=df, maxIter = 1e3) # optional: set some box constraints lb <= x <= ub p.lb = [-6.5]*N p.ub = [6.5]*N # see help(NLP) for handling of other constraints: # Ax<=b, Aeq x = beq, c(x) <= 0, h(x) = 0 # see also /examples/nlp_1.py # required tolerance for smooth constraints, default 1e-6 p.contol = 1.1e-6 p.name = 'minlp_1' nlpSolver = 'ipopt' # coords of discrete variables and sets of allowed values
#objective function: f = lambda x: ((x-5.45)**2).sum() #optional: 1st derivatives df = lambda x: 2*(x-5.45) # start point x0 = 8*cos(arange(N)) # assign prob: # 1st arg - objective function # 2nd arg - start point # for more details see # http://openopt.org/Assignment p = MINLP(f, x0, df=df, maxIter = 1e3) # optional: set some box constraints lb <= x <= ub p.lb = [-6.5]*N p.ub = [6.5]*N # see help(NLP) for handling of other constraints: # Ax<=b, Aeq x = beq, c(x) <= 0, h(x) = 0 # see also /examples/nlp_1.py # required tolerance for smooth constraints, default 1e-6 p.contol = 1.1e-6 p.name = 'minlp_1' # required field: nlpSolver - should be capable of handling box-bounds at least #nlpSolver = 'ralg'
from FuncDesigner import * from openopt import MINLP import numpy as np # two customer, one facility problem vol, dump = oovars('vol','dump') # a is mass sent to LF, b is mass recovered at landfill, c is unrecovered op = oovar('op', domain = np.array([1, 0])) # domain should be Python list/set/tuple of allowed values startPoint = {vol:np.array([100,100]), dump:np.array([20,30]), op:np.array([0])} # however, you'd better use numpy arrays instead of Python lists # variable attributes cap = np.array([400]) # facility capacity per ton demand = np.array([200,200]) # ton elec = 5 # CO2 from electricity per ton ghg = 19.54 # CO2 from transport per mile dist = np.array([ 55, 15]) # distance in miles between pv and mrf cost = 0 # social cost of dumping f = sum(sum(op*dist))*ghg + sum(op*vol)*elec + sum(dump)*cost # min ghg impact constraints = [ op*(vol + dump) == demand , op*sum(vol) < cap ,vol >= 0] p = MINLP(f, startPoint, constraints = constraints) r = p.minimize('branb', nlpSolver='ralg', plot=0, discrtol = 1e-6, xtol=1e-7) vol_opt, dump_opt, op_opt = r(vol, dump, op) # or any of the following: # a_opt, b_opt, c_opt,d_opt = r(a), r(b), r(c),r(d) # r('a'), r('b'), r('c'), r('d') (provided you have assigned the names to oovars as above) # r('a', 'b', 'c', 'd') # a(r), b(r), c(r), d(r) print(vol_opt, dump_opt, op_opt)