r[1,1] = 2 * x[1] r[1,2] = 2 * x[2] + 15 #incorrect derivative return r p.dc = dc p.h = lambda x: (1e1*(x[-1]-1)**4, (x[-2]-1.5)**4) def dh(x): r = zeros((2, p.n)) r[0,-1] = 1e1*4*(x[-1]-1)**3 r[1,-2] = 4*(x[-2]-1.5)**3 + 15 #incorrect derivative return r p.dh = dh p.checkdf() p.checkdc() p.checkdh() """ you can use p.checkdF(x) for other point than x0 (F is f, c or h) p.checkdc(myX) or p.checkdc(x=myX) values with difference greater than maxViolation (default 1e-5) will be shown p.checkdh(maxViolation=1e-4) p.checkdh(myX, maxViolation=1e-4) p.checkdh(x=myX, maxViolation=1e-4) ################################################################################# Typical output (unfortunately, in terminal or other IDEs the blank space used in strings separation can have other lengths):
b=b, Aeq=Aeq, beq=beq, lb=lb, ub=ub, gtol=gtol, contol=contol, iprint=50, maxIter=10000, maxFunEvals=1e7, name='NLP_1') #optional: graphic output, requires pylab (matplotlib) p.plot = True #optional: user-supplied 1st derivatives check p.checkdf() p.checkdc() p.checkdh() solver = 'ralg' #solver = 'algencan' #solver = 'ipopt' #solver = 'scipy_slsqp' # solve the problem r = p.solve(solver, plot=1) # string argument is solver name # r.xf and r.ff are optim point and optim objFun value # r.ff should be something like 132.05
p.h=[pos_sum,neg_sum] p.args.h=h_args p.args.c=h_args p.dh=[pos_sum_grad,neg_sum_grad] p.dc=chisq_grad #p.dh=[pos_sum_grad,neg_sum_grad,neg_sum_grad] p.df = S_grad if 0: print 'checking' p.checkdf() #p.checkdc() print 'check equality constraints' p.checkdh() print 'checking inequality' p.checkdc() sys.exit() print 'solving' if 1: #r=p.solve('scipy_cobyla') #r=p.solve('scipy_lbfgsb') r = p.solve('algencan') #r = p.solve('ralg') print 'done' pout=r.xf if 0: scipy.optimize.optimize.fmin_l_bfgs_b(max_wrap, p0, fprime = None, args=h_args, approx_grad = 0, \ bounds = None, m = 10, factr = 10000000.0, \
def minimize(self, **kwargs): """ solve the nonlinear problem using OpenOpt Returns: obj_value, solution obj_value -- value of the objective function at the discovered solution solution -- the solution flux vector (indexed like matrix columns) """ if self.iterator is None: nlp = NLP(self.obj, self.x0, df=self.d_obj, c=self.nlc, dc=self.d_nlc, A=self.Aineq, Aeq=self.Aeq, b=self.bineq, beq=self.beq, lb=self.lb, ub=self.ub, **kwargs) nlp.debug = 1 nlp.plot = False nlp.checkdf() if self.nlc is not None: nlp.checkdc() r = nlp.solve(self.solver) else: self.rlist = [] for x0 in self.iterator: nlp = NLP(self.obj, x0, df=self.d_obj, c=self.nlc, dc=self.d_nlc, A=self.Aineq, Aeq=self.Aeq, b=self.bineq, beq=self.beq, lb=self.lb, ub=self.ub, **kwargs) r = nlp.solve(self.solver) if r.istop > 0 and r.ff == r.ff: self.rlist.append(r) if self.rlist != []: r = min(self.rlist, key=lambda x: x.ff) if r.istop <= 0 or r.ff != r.ff: # check halting condition self.obj_value = 0. self.solution = [] self.istop = r.istop else: self.obj_value = r.ff self.solution = r.xf self.istop = r.istop return self.obj_value, self.solution