def oodraw(self, p): #note that self is same as p.graphics try: import pylab except: p.pWarn( 'to use OpenOpt graphics you need pylab (Python module) installed. Turning graphics off...' ) p.plot = 0 return #isNewFigure = (not isempty(lastTask) and not isequal(lastTask, {p.name p.primal.fName})) ... #or (~self.drawingInOneWindow and globstat.iter ==1)... #or ~isfield(self, 'figureHandler')... #or (isequal(p.env, 'matlab') and ~ishandle(self.figureHandler)) #todo: fix me later! needNewFigure = not p.iter #lastTask={p.name p.primal.fName} % x0 can be absent colors = ['b', 'k', 'c', 'r', 'g'] specifiers = ['-', ':', '-.', '--'] pylab.ion() if needNewFigure: #self.figureHandler = pylab.figure self.colorCount = -1 self.specifierCount = 0 self.nTrajectories = 0 self.ghandlers = [] #self.solverNames = [] #TODO: the condition should be handled somewhere other place, not here. if p.probType in ('NLSP', 'SNLE'): Y_LABELS = ['maxResidual'] #pylab.plot([0], [log10(p.contol)-1.5]) elif p.probType == 'NLLSP': Y_LABELS = ['sum(residuals^2)'] else: Y_LABELS = ['objective function'] isIterPointAlwaysFeasible = p.solver.__isIterPointAlwaysFeasible__ if type(p.solver.__isIterPointAlwaysFeasible__) == bool \ else p.solver.__isIterPointAlwaysFeasible__(p) if not (p._isUnconstrained() or isIterPointAlwaysFeasible): self.isMaxConstraintSubplotRequired = True if p.useScaledResidualOutput: Y_LABELS.append('MaxConstraint/ConTol') else: Y_LABELS.append('maxConstraint') else: self.isMaxConstraintSubplotRequired = False if self.isMaxConstraintSubplotRequired: self.nSubPlots = 2 else: self.nSubPlots = 1 #creating new trajectory, if needed isNewTrajectory = not p.iter # FIXME if isNewTrajectory: self.colorCount += 1 if self.drawingInOneWindow: if self.colorCount > len(colors) - 1: self.colorCount = 0 self.specifierCount += 1 if self.specifierCount > len(specifiers) - 1: p.warn('line types number exeeded') self.specifierCount = 0 #setting color & specifier #color = colors[self.colorCount] #specifier = specifiers[self.specifierCount] color = p.color specifier = p.specifier #setting xlabel, ylabel, title etc tx = p.xlabel.lower() if isNewTrajectory: self.nTrajectories += 1 #win = gtk.Window() #win.set_name("OpenOpt " + str(oover) + ", license: BSD 2.0") pTitle = 'problem: ' + p.name if p.showGoal: pTitle += ' goal: ' + p.goal if self.nSubPlots > 1: pylab.subplot(self.nSubPlots, 1, 1) p.figure = pylab.gcf() pylab.title(pTitle) p.figure.canvas.set_window_title('OpenOpt ' + ooversion) if tx == 'cputime': xlabel = 'CPU Time elapsed (without graphic output), sec' d_x = 0.01 elif tx == 'time': xlabel = 'Time elapsed (without graphic output), sec' d_x = 0.01 elif tx in ['niter', 'iter']: xlabel = 'iter' d_x = 4 elif tx == 'nf': xlabel = 'Number of objective function evaluations' d_x = 4 else: p.err('unknown graphic output xlabel: "' + tx + '", should be in "time", "cputime", "iter", "nf"') self.nPointsPlotted = 0 for ind in range(self.nSubPlots): if (self.nSubPlots > 1 and ind != 0) or p.probType in ('NLSP', 'SNLE'): ax = pylab.subplot(self.nSubPlots, 1, ind + 1) ax.set_yscale('log') # if self.nSubPlots > 1: # ax = pylab.subplot(self.nSubPlots, 1, ind+1) # if p.probType in ('NLSP', 'SNLE') or ind != 0: # ax.set_yscale('log') pylab.hold(1) pylab.grid(1) pylab.ylabel(Y_LABELS[ind]) pylab.xlabel(xlabel) ################ getting data to plot ############### if p.iter > 0: IND_start, IND_end = self.nPointsPlotted - 1, p.iter + 1 #note: indexing from zero assumed #if p.isFinished: IND_end = p.iter else: IND_start, IND_end = 0, 1 if p.plotOnlyCurrentMinimum: yy = array(p.iterValues.f[IND_start:]) if isNewTrajectory: self.currMin = yy[0] k = 0 for j in range( IND_start, IND_start + len(yy) ): #todo: min is slow in 1x1 comparison vs if-then-else yy[k] = min(self.currMin, p.iterValues.f[j]) self.currMin = yy[k] k += 1 if IND_start <= IND_end: if len(p.iterValues.f) >= 1: yySave = [p.iterValues.f[-1]] # FIXME! (for constraints) else: yySave = [p.f(p.x0)] else: yy = array(p.iterValues.f[IND_start:IND_end]) if IND_start <= IND_end: if len(p.iterValues.f) >= 1: yySave = [p.iterValues.f[-1]] else: yySave = [p.f(p.x0)] if tx == 'iter': xx = range(IND_start, IND_end) elif tx == 'cputime': if len(p.iterTime) != len(p.cpuTimeElapsedForPlotting): p.iterTime.append(p.iterTime[-1]) xx = asfarray(p.iterCPUTime[IND_start:IND_end]) - asfarray( p.cpuTimeElapsedForPlotting[IND_start:IND_end]) elif tx == 'time': if len(p.iterTime) != len(p.timeElapsedForPlotting): p.iterTime.append(p.iterTime[-1]) xx = asfarray(p.iterTime[IND_start:IND_end]) - asfarray( p.timeElapsedForPlotting[IND_start:IND_end]) elif tx == 'nf': xx = asfarray(p.iterValues.nf[IND_start:IND_end]) else: p.err('unknown labelX case') if len(xx) > len(yy): if p.isFinished: xx = xx[:-1] else: p.err('OpenOpt graphics ERROR - FIXME!') if p.probType in ('NLSP', 'SNLE'): yy = yy + p.ftol / self.REDUCE YY = [yy] #if len(YY) == 0: YY = yySave if self.isMaxConstraintSubplotRequired: yy22 = p.contol / self.REDUCE + asfarray( p.iterValues.r[IND_start:IND_end]) if p.useScaledResidualOutput: yy22 /= p.contol YY.append(yy22) if IND_start <= IND_end: if len(p.iterValues.r) == 0: return rr = p.iterValues.r[-1] if p.useScaledResidualOutput: rr /= p.contol if len(p.iterValues.r) >= 1: yySave.append(p.contol / self.REDUCE + asfarray(rr)) else: yySave.append(p.contol / self.REDUCE + asfarray(p.getMaxResidual(p.x0))) if needNewFigure: if self.nSubPlots > 1: pylab.subplot(2, 1, 2) tmp = 1 if p.useScaledResidualOutput else p.contol pylab.plot([xx[0]], [tmp / 10**1.5]) pylab.plot([xx[0] + d_x], [tmp / 10**1.5]) pylab.plot([xx[0]], [YY[1][0] * 10]) pylab.plot([xx[0] + d_x], [YY[1][0] * 10]) pylab.subplot(2, 1, 1) pylab.plot([xx[0]], [YY[0][0]]) pylab.plot([xx[0] + d_x], [YY[0][0]]) ########################################## if self.plotIterMarkers: usualMarker = 'o' else: usualMarker = '' for ind in range(self.nSubPlots): if self.nSubPlots > 1: pylab.subplot(self.nSubPlots, 1, ind + 1) yy2 = ravel(YY[ind]) if len(yy2) < len(xx): if IND_start > IND_end: yy2 = ravel(yySave[ind]) elif yy2.size == 0: yy2 = ravel(yySave[ind]) else: yy2 = hstack((yy2, yy2[-1])) # if len(yy2)<len(xx): # if p.debug: p.warn('FIXME! - different len of xx and yy in graphics') # yy2 = yy2.tolist()+[yy2[-1]] if isNewTrajectory: if isfinite(p.xlim[0]): pylab.plot([p.xlim[0]], [yy2[0]], color='w') if isfinite(p.xlim[1]): pylab.plot([p.xlim[1]], [yy2[0]], color='w') if ind == 0: if isfinite(p.ylim[0]): pylab.plot([xx[0]], [p.ylim[0]], color='w') if isfinite(p.ylim[1]): pylab.plot([xx[0]], [p.ylim[1]], color='w') if p.probType in ('NLSP', 'SNLE'): pylab.plot([xx[0]], [p.ftol / self.REDUCE], color='w') if ind == 1: horz_line_value = 1 if p.useScaledResidualOutput else p.primalConTol pylab.plot([xx[0], xx[-1]], [horz_line_value, horz_line_value], ls = self.axLineStyle, linewidth = self.axLineWidth, color='g',\ marker = self.axMarker, ms = self.axMarkerSize, mew = self.markerEdgeWidth, mec = self.axMarkerEdgeColor, mfc = self.axFaceColor) elif p.probType in ('NLSP', 'SNLE'): pylab.plot([xx[0], xx[-1]], [p.ftol, p.ftol], ls = self.axLineStyle, linewidth = self.axLineWidth, color='g',\ marker = self.axMarker, ms = self.axMarkerSize, mew = self.markerEdgeWidth, mec = self.axMarkerEdgeColor, mfc = self.axFaceColor) if isNewTrajectory: p2 = pylab.plot([xx[0]], [yy2[0]], color=color, marker=self.specifierStart, markersize=self.markerSize) p3 = pylab.plot([xx[0], xx[0] + 1e-50], [yy2[0], yy2[0]], color=color, markersize=self.markerSize) p._p3 = p3 if p.legend == '': pylab.legend([p3[0]], [p.solver.__name__], shadow=True) elif type(p.legend) in (tuple, list): pylab.legend([p3[0]], p.legend, shadow=True) else: pylab.legend([p3[0]], [p.legend], shadow=True) pylab.plot(xx[1:], yy2[1:], color, marker=usualMarker, markersize=self.markerSize / 3) else: pylab.plot(xx, ravel(yy2), color + specifier, marker=usualMarker, markersize=self.iterMarkerSize) if p.isFinished: pylab.legend([p._p3[0]], [p.solver.__name__], shadow=True, loc=0) #xMin, xMax = [], [] if p.istop < 0: if stopcase( p.istop ) == 0: # maxTime, maxIter, maxCPUTime, maxFunEvals etc exeeded if p.isFeas(p.xf): s = self.specifierContinueFeasible else: s = self.specifierContinueInfeasible else: s = self.specifierFailed else: #if not hasattr(p, 'isFeasible'): p.isFeas() if p.isFeasible: #if p.isFeas(p.xk) if p.istop > 0: s = self.specifierOK else: # p.istop = 0 s = self.specifierUndefined else: s = self.specifierError if s == self.specifierOK: marker = (5, 1, 0) else: marker = s if isnan(yy2[-1]): yy2[-1] = 0 pylab.scatter(ravel(xx[-1]), [yy2[-1]], c=color, marker=marker, s=[150]) #pylab.axis('auto') [xmin, xmax, ymin, ymax] = pylab.axis() if ymax - ymin > 25 * (yy2[-1] - ymin): delta = 0.04 * (ymax - ymin) pylab.scatter([(xmin + xmax) / 2, (xmin + xmax) / 2], [ymin - delta, ymax + delta], s=1, c='w', edgecolors='none', marker='o') pylab.draw() if ind == 0 and p.probType in ('NLSP', 'SNLE'): pylab.plot([xmin, xmax], [log10(p.ftol), log10(p.ftol)],\ linewidth = self.axLineWidth, ls = self.axLineStyle, color='g',\ marker = self.axMarker, ms = self.axMarkerSize, \ mew = self.markerEdgeWidth, mec = self.axMarkerEdgeColor, mfc = self.axFaceColor) if ind == 1: horz_line_value = 0 if p.useScaledResidualOutput else log10( p.primalConTol) pylab.plot([xmin, xmax], [horz_line_value, horz_line_value],\ linewidth = self.axLineWidth, ls = self.axLineStyle, color='g',\ marker = self.axMarker, ms = self.axMarkerSize, \ mew = self.markerEdgeWidth, mec = self.axMarkerEdgeColor, mfc = self.axFaceColor) pylab.subplot(self.nSubPlots, 1, 1) pylab.plot([xmax], [yy2[-1]], color='w') # if p.probType == 'NLSP': # pylab.axhline(y=log10(p.primalConTol), xmin=xmin, xmax=xmax,\ # linewidth = self.axLineWidth, ls = self.axLineStyle, color='g',\ # marker = self.axMarker, ms = self.axMarkerSize, \ # mew = self.markerEdgeWidth, mec = self.axMarkerEdgeColor, mfc = self.axFaceColor) # if p.isFinished: # for ind in range(self.nSubPlots): # if self.nSubPlots>1: pylab.subplot(self.nSubPlots,1,ind+1) #pylab.xlim(min(xMin), max(xMax)) self.nPointsPlotted = p.iter + 1 pylab.draw()
def runProbSolver(p_, solver_str_or_instance=None, *args, **kwargs): #p = copy.deepcopy(p_, memo=None, _nil=[]) p = p_ if len(args) != 0: p.err('unexpected args for p.solve()') if hasattr(p, 'was_involved'): p.err("""You can't run same prob instance for twice. Please reassign prob struct. You can avoid it via using FuncDesigner oosystem.""") else: p.was_involved = True if solver_str_or_instance is None: if hasattr(p, 'solver'): solver_str_or_instance = p.solver elif 'solver' in kwargs.keys(): solver_str_or_instance = kwargs['solver'] if type(solver_str_or_instance) is str and ':' in solver_str_or_instance: isConverter = True probTypeToConvert, solverName = solver_str_or_instance.split(':', 1) p.solver = getSolverFromStringName(p, solverName) solver_params = {} #return converter(solverName, *args, **kwargs) else: isConverter = False if solver_str_or_instance is None: p.err('you should provide name of solver') elif type(solver_str_or_instance) is str: p.solver = getSolverFromStringName(p, solver_str_or_instance) else: # solver_str_or_instance is oosolver if not solver_str_or_instance.isInstalled: p.err(''' solver %s seems to be uninstalled yet, check http://openopt.org/%s for install instructions''' % (solver_str_or_instance.__name__, p.probType)) p.solver = solver_str_or_instance for key, value in solver_str_or_instance.fieldsForProbInstance.items(): setattr(p, key, value) p.isConverterInvolved = isConverter old_err = seterr(all= 'ignore') if 'debug' in kwargs.keys(): p.debug = kwargs['debug'] probAttributes = set(p.__dict__) solverAttributes = set(p.solver.__dict__) intersection = list(probAttributes.intersection(solverAttributes)) if len(intersection) != 0: if p.debug: p.warn(''' attribute %s is present in both solver and prob (probably you assigned solver parameter in prob constructor), the attribute will be assigned to solver''' % intersection[0]) for elem in intersection: setattr(p.solver, elem, getattr(p, elem)) solver = p.solver.__solver__ for key, value in kwargs.items(): if hasattr(p.solver, key): if isConverter: solver_params[key] = value else: setattr(p.solver, key, value) elif hasattr(p, key): setattr(p, key, value) else: p.warn('incorrect parameter for prob.solve(): "' + str(key) + '" - will be ignored (this one has been not found in neither prob nor ' + p.solver.__name__ + ' solver parameters)') if p.probType == 'EIG' and 'goal' in kwargs: p.err('for EIG parameter "goal" should be used only in class instance definition, not in "solve" method') p.iterValues = EmptyClass() p.iterCPUTime = [] p.iterTime = [] p.iterValues.x = [] # iter points p.iterValues.f = [] # iter ObjFunc Values p.iterValues.r = [] # iter MaxResidual p.iterValues.rt = [] # iter MaxResidual Type: 'c', 'h', 'lb' etc p.iterValues.ri = [] # iter MaxResidual Index p.solutions = [] # list of solutions, may contain several elements for interalg and mb other solvers if p._baseClassName == 'NonLin':p.iterValues.nNaNs = [] # number of constraints equal to numpy.nan if p.goal in ['max','maximum']: p.invertObjFunc = True #TODO: remove it! p.advanced = EmptyClass() p.istop = 0 p.iter = 0 p.graphics.nPointsPlotted = 0 p.finalIterFcnFinished = False #for fn in p.nEvals.keys(): p.nEvals[fn] = 0 # NB! f num is used in LP/QP/MILP/etc stop criteria check p.msg = '' if not type(p.callback) in (list, tuple): p.callback = [p.callback] if hasattr(p, 'xlabel'): p.graphics.xlabel = p.xlabel if p.graphics.xlabel == 'nf': p.iterValues.nf = [] # iter ObjFunc evaluation number T = time() C = clock() p._Prepare() T = time() - T C = clock() - C if T > 1 or C > 1: p.disp('Initialization: Time = %0.1f CPUTime = %0.1f' % (T, C)) for fn in ['FunEvals', 'Iter', 'Time', 'CPUTime']: if hasattr(p,'min'+fn) and hasattr(p,'max'+fn) and getattr(p,'max'+fn) < getattr(p,'min'+fn): p.warn('min' + fn + ' (' + str(getattr(p,'min'+fn)) +') exceeds ' + 'max' + fn + '(' + str(getattr(p,'max'+fn)) +'), setting latter to former') setattr(p,'max'+fn, getattr(p,'min'+fn)) for fn in ['maxFunEvals', 'maxIter']: setattr(p, fn, int(getattr(p, fn)))# to prevent warnings from numbers like 1e7 if hasattr(p, 'x0'): try: p.x0 = atleast_1d(asfarray(p.x0).copy()) except NotImplementedError: p.x0 = asfarray(p.x0.tolist()) for fn in ['lb', 'ub', 'b', 'beq']: if hasattr(p, fn): fv = getattr(p, fn) if fv is not None:# and fv != []: if str(type(fv)) == "<class 'map'>": p.err("Python3 incompatibility with previous versions: you can't use 'map' here, use rendered value instead") setattr(p, fn, asfarray(fv).flatten()) else: setattr(p, fn, asfarray([])) if p.solver._requiresFiniteBoxBounds: ind1, ind2 = isinf(p.lb), isinf(p.ub) if isscalar(p.implicitBounds): p.implicitBounds = (-p.implicitBounds, p.implicitBounds) # may be from lp2nlp converter, thus omit nlp init code p.lb[ind1] = p.implicitBounds[0] if asarray(p.implicitBounds[0]).size == 1 else p.implicitBounds[0][ind1] p.ub[ind2] = p.implicitBounds[1] if asarray(p.implicitBounds[1]).size == 1 else p.implicitBounds[0][ind2] # if p.lb.size == 0: # p.lb = -inf * ones(p.n) # if p.ub.size == 0: # p.ub = inf * ones(p.n) p.stopdict = {} for s in ['b','beq']: if hasattr(p, s): setattr(p, 'n'+s, len(getattr(p, s))) #if p.probType not in ['LP', 'QP', 'MILP', 'LLSP']: p.objFunc(p.x0) p.isUC = p._isUnconstrained() isIterPointAlwaysFeasible = p.solver.__isIterPointAlwaysFeasible__ if type(p.solver.__isIterPointAlwaysFeasible__) == bool \ else p.solver.__isIterPointAlwaysFeasible__(p) if isIterPointAlwaysFeasible: #assert p.data4TextOutput[-1] == 'log10(maxResidual)' if p.data4TextOutput[-1] == 'log10(maxResidual)': p.data4TextOutput = p.data4TextOutput[:-1] # else: # p.err('bug in runProbSolver.py') elif p.useScaledResidualOutput: p.data4TextOutput[-1] = 'log10(MaxResidual/ConTol)' if p.showFeas and p.data4TextOutput[-1] != 'isFeasible': p.data4TextOutput.append('isFeasible') if p.maxSolutions != 1: p._nObtainedSolutions = 0 p.data4TextOutput.append('nSolutions') if not p.solver.iterfcnConnected: if SMALL_DELTA_X in p.kernelIterFuncs: p.kernelIterFuncs.pop(SMALL_DELTA_X) if SMALL_DELTA_F in p.kernelIterFuncs: p.kernelIterFuncs.pop(SMALL_DELTA_F) if not p.solver._canHandleScipySparse: if hasattr(p.A, 'toarray'): p.A = p.A.toarray() if hasattr(p.Aeq, 'toarray'): p.Aeq = p.Aeq.toarray() if isinstance(p.A, ndarray) and type(p.A) != ndarray: # numpy matrix p.A = p.A.A if isinstance(p.Aeq, ndarray) and type(p.Aeq) != ndarray: # numpy matrix p.Aeq = p.Aeq.A if hasattr(p, 'optVars'): p.err('"optVars" is deprecated, use "freeVars" instead ("optVars" is not appropriate for some prob types, e.g. systems of (non)linear equations)') # p.xf = nan * ones([p.n, 1]) # p.ff = nan #todo : add scaling, etc p.primalConTol = p.contol if not p.solver.__name__.startswith('interalg'): p.contol *= ConTolMultiplier p.timeStart = time() p.cpuTimeStart = clock() # TODO: move it into solver parameters if p.probType not in ('MINLP', 'IP'): p.plotOnlyCurrentMinimum = p.__isNoMoreThanBoxBounded__() ############################ # Start solving problem: if p.iprint >= 0: p.disp('\n' + '-'*25 + ' OpenOpt %s ' % version + '-'*25) pt = p.probType if p.probType != 'NLSP' else 'SNLE' s = 'solver: ' + p.solver.__name__ + ' problem: ' + p.name + ' type: %s' % pt if p.showGoal: s += ' goal: ' + p.goal p.disp(s) p.extras = {} try: if isConverter: pass # TODO: will R be somewhere used? #R = converter(solverName, **solver_params) else: nErr = check(p) if nErr: p.err("prob check results: " +str(nErr) + "ERRORS!")#however, I guess this line will be never reached. if p.probType not in ('IP', 'EIG'): p.iterfcn(p.x0) if hasSetproctitleModule: try: originalName = setproctitle.getproctitle() if originalName.startswith('OpenOpt-'): originalName = None else: s = 'OpenOpt-' + p.solver.__name__ # if p.name != 'unnamed': s += '-' + p.name setproctitle.setproctitle(s) except: originalName = None else: p.pWarn(''' please install setproctitle module (it's available via easy_install and Linux soft channels like apt-get)''') solver(p) if hasSetproctitleModule and originalName is not None: setproctitle.setproctitle(originalName) # except killThread: # if p.plot: # print 'exiting pylab' # import pylab # if hasattr(p, 'figure'): # print 'closing figure' # #p.figure.canvas.draw_drawable = lambda: None # pylab.ioff() # pylab.close() # #pylab.draw() # #pylab.close() # print 'pylab exited' # return None except isSolved: # p.fk = p.f(p.xk) # p.xf = p.xk # p.ff = p.objFuncMultiple2Single(p.fk) if p.istop == 0: p.istop = 1000 finally: seterr(**old_err) ############################ p.contol = p.primalConTol # Solving finished if hasattr(p, '_bestPoint') and not any(isnan(p._bestPoint.x)) and p.probType != 'ODE': p.iterfcn(p._bestPoint) if p.probType != 'EIG': if not hasattr(p, 'xf') and not hasattr(p, 'xk'): p.xf = p.xk = ones(p.n)*nan if hasattr(p, 'xf') and (not hasattr(p, 'xk') or array_equal(p.xk, p.x0)): p.xk = p.xf if not hasattr(p, 'xf') or all(isnan(p.xf)): p.xf = p.xk if p.xf is nan: p.xf = p.xk = ones(p.n)*nan if p.isFeas(p.xf) and (not p.probType=='MINLP' or p.discreteConstraintsAreSatisfied(p.xf)): p.isFeasible = True else: p.isFeasible = False else: p.isFeasible = True # check it! p.isFinished = True # After the feasibility check above! if p.probType == 'MOP': p.isFeasible = True elif p.probType == 'IP': p.isFeasible = p.rk < p.ftol else: p.ff = p.fk = p.objFunc(p.xk) # walkaround for PyPy: if type(p.ff) == ndarray and p.ff.size == 1: p.ff = p.fk = asscalar(p.ff) if not hasattr(p, 'ff') or any(p.ff==nan): p.iterfcn, tmp_iterfcn = lambda *args: None, p.iterfcn p.ff = p.fk p.iterfcn = tmp_iterfcn if p.invertObjFunc: p.fk, p.ff = -p.fk, -p.ff if asfarray(p.ff).size > 1: p.ff = p.objFuncMultiple2Single(p.fk) #p.ff = p.objFuncMultiple2Single(p.ff) #if not hasattr(p, 'xf'): p.xf = p.xk if type(p.xf) in (list, tuple) or isscalar(p.xf): p.xf = asarray(p.xf) p.xf = p.xf.flatten() p.rf = p.getMaxResidual(p.xf) if not p.probType == 'IP' else p.rk if not p.isFeasible and p.istop > 0: p.istop = -100-p.istop/1000.0 if p.istop == 0 and p.iter >= p.maxIter: p.istop, p.msg = IS_MAX_ITER_REACHED, 'Max Iter has been reached' p.stopcase = stopcase(p) p.xk, p.rk = p.xf, p.rf if p.invertObjFunc: p.fk = -p.ff p.iterfcn(p.xf, -p.ff, p.rf) else: p.fk = p.ff p.iterfcn(p.xf, p.ff, p.rf) p.__finalize__() if not p.storeIterPoints: delattr(p.iterValues, 'x') r = OpenOptResult(p) #TODO: add scaling handling!!!!!!! # for fn in ('df', 'dc', 'dh', 'd2f', 'd2c', 'd2h'): # if hasattr(p, '_' + fn): setattr(r, fn, getattr(p, '_'+fn)) p.invertObjFunc = False if p.isFDmodel: p.x0 = p._x0 finalTextOutput(p, r) if not hasattr(p, 'isManagerUsed') or p.isManagerUsed == False: finalShow(p) return r
def oodraw(self, p): #note that self is same as p.graphics try: import pylab except: p.pWarn('to use OpenOpt graphics you need pylab (Python module) installed. Turning graphics off...') p.plot = 0 return #isNewFigure = (not isempty(lastTask) and not isequal(lastTask, {p.name p.primal.fName})) ... #or (~self.drawingInOneWindow and globstat.iter ==1)... #or ~isfield(self, 'figureHandler')... #or (isequal(p.env, 'matlab') and ~ishandle(self.figureHandler)) #todo: fix me later! needNewFigure = not p.iter #lastTask={p.name p.primal.fName} % x0 can be absent colors = ['b', 'k', 'c', 'r', 'g'] specifiers = ['-', ':', '-.', '--'] pylab.ion() if needNewFigure: #self.figureHandler = pylab.figure self.colorCount = -1 self.specifierCount = 0 self.nTrajectories = 0 self.ghandlers = [] #self.solverNames = [] #TODO: the condition should be handled somewhere other place, not here. if p.probType in ('NLSP', 'SNLE'): Y_LABELS = ['maxResidual'] #pylab.plot([0], [log10(p.contol)-1.5]) elif p.probType == 'NLLSP': Y_LABELS = ['sum(residuals^2)'] else: Y_LABELS = ['objective function'] isIterPointAlwaysFeasible = p.solver.__isIterPointAlwaysFeasible__ if type(p.solver.__isIterPointAlwaysFeasible__) == bool \ else p.solver.__isIterPointAlwaysFeasible__(p) if not (p._isUnconstrained() or isIterPointAlwaysFeasible): self.isMaxConstraintSubplotRequired = True if p.useScaledResidualOutput: Y_LABELS.append('MaxConstraint/ConTol') else: Y_LABELS.append('maxConstraint') else: self.isMaxConstraintSubplotRequired = False if self.isMaxConstraintSubplotRequired: self.nSubPlots = 2 else: self.nSubPlots = 1 #creating new trajectory, if needed isNewTrajectory = not p.iter # FIXME if isNewTrajectory: self.colorCount += 1 if self.drawingInOneWindow: if self.colorCount > len(colors) - 1 : self.colorCount = 0 self.specifierCount += 1 if self.specifierCount > len(specifiers) - 1 : p.warn('line types number exeeded') self.specifierCount = 0 #setting color & specifier #color = colors[self.colorCount] #specifier = specifiers[self.specifierCount] color = p.color specifier = p.specifier #setting xlabel, ylabel, title etc tx = p.xlabel.lower() if isNewTrajectory: self.nTrajectories += 1 #win = gtk.Window() #win.set_name("OpenOpt " + str(oover) + ", license: BSD 2.0") pTitle = 'problem: ' + p.name if p.showGoal: pTitle += ' goal: ' + p.goal if self.nSubPlots>1: pylab.subplot(self.nSubPlots, 1, 1) p.figure = pylab.gcf() pylab.title(pTitle) p.figure.canvas.set_window_title('OpenOpt ' + ooversion) if tx == 'cputime': xlabel = 'CPU Time elapsed (without graphic output), sec' d_x = 0.01 elif tx == 'time': xlabel = 'Time elapsed (without graphic output), sec' d_x = 0.01 elif tx in ['niter', 'iter']: xlabel = 'iter' d_x = 4 elif tx == 'nf': xlabel = 'Number of objective function evaluations' d_x = 4 else: p.err('unknown graphic output xlabel: "' + tx + '", should be in "time", "cputime", "iter", "nf"') self.nPointsPlotted = 0 for ind in range(self.nSubPlots): if (self.nSubPlots > 1 and ind != 0) or p.probType in ('NLSP', 'SNLE'): ax = pylab.subplot(self.nSubPlots, 1, ind+1) ax.set_yscale('log') # if self.nSubPlots > 1: # ax = pylab.subplot(self.nSubPlots, 1, ind+1) # if p.probType in ('NLSP', 'SNLE') or ind != 0: # ax.set_yscale('log') pylab.hold(1) pylab.grid(1) pylab.ylabel(Y_LABELS[ind]) pylab.xlabel(xlabel) ################ getting data to plot ############### if p.iter>0: IND_start, IND_end = self.nPointsPlotted-1, p.iter+1#note: indexing from zero assumed #if p.isFinished: IND_end = p.iter else: IND_start, IND_end = 0, 1 if p.plotOnlyCurrentMinimum: yy = array(p.iterValues.f[IND_start:]) if isNewTrajectory: self.currMin = yy[0] k = 0 for j in range(IND_start, IND_start + len(yy)): #todo: min is slow in 1x1 comparison vs if-then-else yy[k] = min(self.currMin, p.iterValues.f[j]) self.currMin = yy[k] k += 1 if IND_start<=IND_end: if len(p.iterValues.f) >= 1: yySave = [p.iterValues.f[-1]] # FIXME! (for constraints) else: yySave = [p.f(p.x0)] else: yy = array(p.iterValues.f[IND_start:IND_end]) if IND_start<=IND_end: if len(p.iterValues.f) >= 1: yySave = [p.iterValues.f[-1]] else: yySave = [p.f(p.x0)] if tx == 'iter': xx = range(IND_start, IND_end) elif tx == 'cputime': if len(p.iterTime) != len(p.cpuTimeElapsedForPlotting): p.iterTime.append(p.iterTime[-1]) xx = asfarray(p.iterCPUTime[IND_start:IND_end]) - asfarray(p.cpuTimeElapsedForPlotting[IND_start:IND_end]) elif tx == 'time': if len(p.iterTime) != len(p.timeElapsedForPlotting): p.iterTime.append(p.iterTime[-1]) xx = asfarray(p.iterTime[IND_start:IND_end]) - asfarray(p.timeElapsedForPlotting[IND_start:IND_end]) elif tx == 'nf': xx = asfarray(p.iterValues.nf[IND_start:IND_end]) else: p.err('unknown labelX case') if len(xx)>len(yy): if p.isFinished: xx = xx[:-1] else: p.err('OpenOpt graphics ERROR - FIXME!') if p.probType in ('NLSP', 'SNLE'): yy = yy+p.ftol/self.REDUCE YY = [yy] #if len(YY) == 0: YY = yySave if self.isMaxConstraintSubplotRequired: yy22 = p.contol/self.REDUCE+asfarray(p.iterValues.r[IND_start:IND_end]) if p.useScaledResidualOutput: yy22 /= p.contol YY.append(yy22) if IND_start<=IND_end: if len(p.iterValues.r) == 0: return rr = p.iterValues.r[-1] if p.useScaledResidualOutput: rr /= p.contol if len(p.iterValues.r) >= 1: yySave.append(p.contol/self.REDUCE+asfarray(rr)) else: yySave.append(p.contol/self.REDUCE+asfarray(p.getMaxResidual(p.x0))) if needNewFigure: if self.nSubPlots > 1: pylab.subplot(2, 1, 2) tmp = 1 if p.useScaledResidualOutput else p.contol pylab.plot([xx[0]],[tmp / 10**1.5]) pylab.plot([xx[0]+d_x],[tmp / 10**1.5]) pylab.plot([xx[0]], [YY[1][0] * 10]) pylab.plot([xx[0]+d_x], [YY[1][0] * 10]) pylab.subplot(2, 1, 1) pylab.plot([xx[0]],[YY[0][0]]) pylab.plot([xx[0]+d_x],[YY[0][0]]) ########################################## if self.plotIterMarkers: usualMarker = 'o' else: usualMarker = '' for ind in range(self.nSubPlots): if self.nSubPlots > 1: pylab.subplot(self.nSubPlots,1,ind+1) yy2 = ravel(YY[ind]) if len(yy2) < len(xx): if IND_start > IND_end: yy2 = ravel(yySave[ind]) elif yy2.size == 0: yy2 = ravel(yySave[ind]) else: yy2 = hstack((yy2, yy2[-1])) # if len(yy2)<len(xx): # if p.debug: p.warn('FIXME! - different len of xx and yy in graphics') # yy2 = yy2.tolist()+[yy2[-1]] if isNewTrajectory: if isfinite(p.xlim[0]): pylab.plot([p.xlim[0]], [yy2[0]], color='w') if isfinite(p.xlim[1]): pylab.plot([p.xlim[1]], [yy2[0]], color='w') if ind==0: if isfinite(p.ylim[0]): pylab.plot([xx[0]], [p.ylim[0]], color='w') if isfinite(p.ylim[1]): pylab.plot([xx[0]], [p.ylim[1]], color='w') if p.probType in ('NLSP', 'SNLE'): pylab.plot([xx[0]], [p.ftol / self.REDUCE], color='w') if ind == 1: horz_line_value = 1 if p.useScaledResidualOutput else p.primalConTol pylab.plot([xx[0], xx[-1]], [horz_line_value, horz_line_value], ls = self.axLineStyle, linewidth = self.axLineWidth, color='g',\ marker = self.axMarker, ms = self.axMarkerSize, mew = self.markerEdgeWidth, mec = self.axMarkerEdgeColor, mfc = self.axFaceColor) elif p.probType in ('NLSP', 'SNLE'): pylab.plot([xx[0], xx[-1]], [p.ftol, p.ftol], ls = self.axLineStyle, linewidth = self.axLineWidth, color='g',\ marker = self.axMarker, ms = self.axMarkerSize, mew = self.markerEdgeWidth, mec = self.axMarkerEdgeColor, mfc = self.axFaceColor) if isNewTrajectory: p2 = pylab.plot([xx[0]], [yy2[0]], color = color, marker = self.specifierStart, markersize = self.markerSize) p3 = pylab.plot([xx[0], xx[0]+1e-50], [yy2[0], yy2[0]], color = color, markersize = self.markerSize) p._p3 = p3 if p.legend == '': pylab.legend([p3[0]], [p.solver.__name__], shadow = True) elif type(p.legend) in (tuple, list): pylab.legend([p3[0]], p.legend, shadow = True) else: pylab.legend([p3[0]], [p.legend], shadow = True) pylab.plot(xx[1:], yy2[1:], color, marker = usualMarker, markersize = self.markerSize/3) else: pylab.plot(xx, ravel(yy2), color + specifier, marker = usualMarker, markersize = self.iterMarkerSize) if p.isFinished: pylab.legend([p._p3[0]], [p.solver.__name__], shadow = True, loc=0) #xMin, xMax = [], [] if p.istop<0: if stopcase(p.istop) == 0: # maxTime, maxIter, maxCPUTime, maxFunEvals etc exeeded if p.isFeas(p.xf): s = self.specifierContinueFeasible else: s = self.specifierContinueInfeasible else: s = self.specifierFailed else: #if not hasattr(p, 'isFeasible'): p.isFeas() if p.isFeasible: #if p.isFeas(p.xk) if p.istop > 0: s = self.specifierOK else:# p.istop = 0 s = self.specifierUndefined else: s = self.specifierError if s == self.specifierOK: marker = (5, 1, 0) else: marker = s if isnan(yy2[-1]): yy2[-1] = 0 pylab.scatter(ravel(xx[-1]), [yy2[-1]], c=color, marker = marker, s=[150]) #pylab.axis('auto') [xmin, xmax, ymin, ymax] = pylab.axis() if ymax - ymin > 25 * (yy2[-1] -ymin): delta = 0.04 * (ymax - ymin) pylab.scatter([(xmin+xmax)/2, (xmin+xmax)/2], [ymin-delta, ymax+delta], s=1, c='w', edgecolors='none', marker='o') pylab.draw() if ind == 0 and p.probType in ('NLSP', 'SNLE'): pylab.plot([xmin, xmax], [log10(p.ftol), log10(p.ftol)],\ linewidth = self.axLineWidth, ls = self.axLineStyle, color='g',\ marker = self.axMarker, ms = self.axMarkerSize, \ mew = self.markerEdgeWidth, mec = self.axMarkerEdgeColor, mfc = self.axFaceColor) if ind == 1: horz_line_value = 0 if p.useScaledResidualOutput else log10(p.primalConTol) pylab.plot([xmin, xmax], [horz_line_value, horz_line_value],\ linewidth = self.axLineWidth, ls = self.axLineStyle, color='g',\ marker = self.axMarker, ms = self.axMarkerSize, \ mew = self.markerEdgeWidth, mec = self.axMarkerEdgeColor, mfc = self.axFaceColor) pylab.subplot(self.nSubPlots, 1, 1) pylab.plot([xmax], [yy2[-1]], color='w') # if p.probType == 'NLSP': # pylab.axhline(y=log10(p.primalConTol), xmin=xmin, xmax=xmax,\ # linewidth = self.axLineWidth, ls = self.axLineStyle, color='g',\ # marker = self.axMarker, ms = self.axMarkerSize, \ # mew = self.markerEdgeWidth, mec = self.axMarkerEdgeColor, mfc = self.axFaceColor) # if p.isFinished: # for ind in range(self.nSubPlots): # if self.nSubPlots>1: pylab.subplot(self.nSubPlots,1,ind+1) #pylab.xlim(min(xMin), max(xMax)) self.nPointsPlotted = p.iter+1 pylab.draw()
def runProbSolver(p_, solver_str_or_instance=None, *args, **kwargs): #p = copy.deepcopy(p_, memo=None, _nil=[]) p = p_ if len(args) != 0: p.err('unexpected args for p.solve()') if hasattr(p, 'was_involved'): p.err("""You can't run same prob instance for twice. Please reassign prob struct. You can avoid it via using FuncDesigner oosystem.""") else: p.was_involved = True if solver_str_or_instance is None: if hasattr(p, 'solver'): solver_str_or_instance = p.solver elif 'solver' in kwargs.keys(): solver_str_or_instance = kwargs['solver'] if type(solver_str_or_instance) is str and ':' in solver_str_or_instance: isConverter = True probTypeToConvert, solverName = solver_str_or_instance.split(':', 1) p.solver = getSolverFromStringName(p, solverName) solver_params = {} #return converter(solverName, *args, **kwargs) else: isConverter = False if solver_str_or_instance is None: p.err('you should provide name of solver') elif type(solver_str_or_instance) is str: p.solver = getSolverFromStringName(p, solver_str_or_instance) else: # solver_str_or_instance is oosolver if not solver_str_or_instance.isInstalled: p.err(''' solver %s seems to be uninstalled yet, check http://openopt.org/%s for install instructions''' % (solver_str_or_instance.__name__, p.probType)) p.solver = solver_str_or_instance for key, value in solver_str_or_instance.fieldsForProbInstance.items( ): setattr(p, key, value) p.isConverterInvolved = isConverter old_err = seterr(all='ignore') if 'debug' in kwargs.keys(): p.debug = kwargs['debug'] probAttributes = set(p.__dict__) solverAttributes = set(p.solver.__dict__) intersection = list(probAttributes.intersection(solverAttributes)) if len(intersection) != 0: if p.debug: p.warn(''' attribute %s is present in both solver and prob (probably you assigned solver parameter in prob constructor), the attribute will be assigned to solver''' % intersection[0]) for elem in intersection: setattr(p.solver, elem, getattr(p, elem)) solver = p.solver.__solver__ for key, value in kwargs.items(): if hasattr(p.solver, key): if isConverter: solver_params[key] = value else: setattr(p.solver, key, value) elif hasattr(p, key): setattr(p, key, value) else: p.warn( 'incorrect parameter for prob.solve(): "' + str(key) + '" - will be ignored (this one has been not found in neither prob nor ' + p.solver.__name__ + ' solver parameters)') if p.probType == 'EIG' and 'goal' in kwargs: p.err( 'for EIG parameter "goal" should be used only in class instance definition, not in "solve" method' ) p.iterValues = EmptyClass() p.iterCPUTime = [] p.iterTime = [] p.iterValues.x = [] # iter points p.iterValues.f = [] # iter ObjFunc Values p.iterValues.r = [] # iter MaxResidual p.iterValues.rt = [] # iter MaxResidual Type: 'c', 'h', 'lb' etc p.iterValues.ri = [] # iter MaxResidual Index p.solutions = [ ] # list of solutions, may contain several elements for interalg and mb other solvers if p._baseClassName == 'NonLin': p.iterValues.nNaNs = [] # number of constraints equal to numpy.nan if p.goal in ['max', 'maximum']: p.invertObjFunc = True #TODO: remove it! p.advanced = EmptyClass() p.istop = 0 p.iter = 0 p.graphics.nPointsPlotted = 0 p.finalIterFcnFinished = False #for fn in p.nEvals.keys(): p.nEvals[fn] = 0 # NB! f num is used in LP/QP/MILP/etc stop criteria check p.msg = '' if not type(p.callback) in (list, tuple): p.callback = [p.callback] if hasattr(p, 'xlabel'): p.graphics.xlabel = p.xlabel if p.graphics.xlabel == 'nf': p.iterValues.nf = [] # iter ObjFunc evaluation number T = time() C = clock() p._Prepare() p.initTime = time() - T p.initCPUTime = clock() - C if p.initTime > 1 or p.initCPUTime > 1: p.disp('Initialization: Time = %0.1f CPUTime = %0.1f' % (p.initTime, p.initCPUTime)) for fn in ['FunEvals', 'Iter', 'Time', 'CPUTime']: if hasattr(p, 'min' + fn) and hasattr(p, 'max' + fn) and getattr( p, 'max' + fn) < getattr(p, 'min' + fn): p.warn('min' + fn + ' (' + str(getattr(p, 'min' + fn)) + ') exceeds ' + 'max' + fn + '(' + str(getattr(p, 'max' + fn)) + '), setting latter to former') setattr(p, 'max' + fn, getattr(p, 'min' + fn)) for fn in ['maxFunEvals', 'maxIter']: setattr(p, fn, int(getattr(p, fn))) # to prevent warnings from numbers like 1e7 if hasattr(p, 'x0'): try: p.x0 = atleast_1d(asfarray(p.x0).copy()) except NotImplementedError: p.x0 = asfarray(p.x0.tolist()) for fn in ['lb', 'ub', 'b', 'beq']: if hasattr(p, fn): fv = getattr(p, fn) if fv is not None: # and fv != []: if str(type(fv)) == "<class 'map'>": p.err( "Python3 incompatibility with previous versions: you can't use 'map' here, use rendered value instead" ) setattr(p, fn, asfarray(fv).flatten()) else: setattr(p, fn, asfarray([])) if p.solver._requiresFiniteBoxBounds: ind1, ind2 = isinf(p.lb), isinf(p.ub) if isscalar(p.implicitBounds): p.implicitBounds = ( -p.implicitBounds, p.implicitBounds ) # may be from lp2nlp converter, thus omit nlp init code p.lb[ind1] = p.implicitBounds[0] if asarray( p.implicitBounds[0]).size == 1 else p.implicitBounds[0][ind1] p.ub[ind2] = p.implicitBounds[1] if asarray( p.implicitBounds[1]).size == 1 else p.implicitBounds[0][ind2] # if p.lb.size == 0: # p.lb = -inf * ones(p.n) # if p.ub.size == 0: # p.ub = inf * ones(p.n) p.stopdict = {} for s in ['b', 'beq']: if hasattr(p, s): setattr(p, 'n' + s, len(getattr(p, s))) #if p.probType not in ['LP', 'QP', 'MILP', 'LLSP']: p.objFunc(p.x0) p.isUC = p._isUnconstrained() isIterPointAlwaysFeasible = p.solver.__isIterPointAlwaysFeasible__ if type(p.solver.__isIterPointAlwaysFeasible__) == bool \ else p.solver.__isIterPointAlwaysFeasible__(p) if isIterPointAlwaysFeasible: #assert p.data4TextOutput[-1] == 'log10(maxResidual)' if p.data4TextOutput[-1] == 'log10(maxResidual)': p.data4TextOutput = p.data4TextOutput[:-1] # else: # p.err('bug in runProbSolver.py') elif p.useScaledResidualOutput: p.data4TextOutput[-1] = 'log10(MaxResidual/ConTol)' if p.showFeas and p.data4TextOutput[-1] != 'isFeasible': p.data4TextOutput.append('isFeasible') if p.maxSolutions != 1: p._nObtainedSolutions = 0 p.data4TextOutput.append('nSolutions') if not p.solver.iterfcnConnected: if SMALL_DELTA_X in p.kernelIterFuncs: p.kernelIterFuncs.pop(SMALL_DELTA_X) if SMALL_DELTA_F in p.kernelIterFuncs: p.kernelIterFuncs.pop(SMALL_DELTA_F) if not p.solver._canHandleScipySparse: if hasattr(p.A, 'toarray'): p.A = p.A.toarray() if hasattr(p.Aeq, 'toarray'): p.Aeq = p.Aeq.toarray() if isinstance(p.A, ndarray) and type(p.A) != ndarray: # numpy matrix p.A = p.A.A if isinstance(p.Aeq, ndarray) and type(p.Aeq) != ndarray: # numpy matrix p.Aeq = p.Aeq.A if hasattr(p, 'optVars'): p.err( '"optVars" is deprecated, use "freeVars" instead ("optVars" is not appropriate for some prob types, e.g. systems of (non)linear equations)' ) # p.xf = nan * ones([p.n, 1]) # p.ff = nan #todo : add scaling, etc p.primalConTol = p.contol if not p.solver.__name__.startswith('interalg'): p.contol *= ConTolMultiplier p.timeStart = time() p.cpuTimeStart = clock() # TODO: move it into solver parameters if p.probType not in ('MINLP', 'IP'): p.plotOnlyCurrentMinimum = p.__isNoMoreThanBoxBounded__() ############################ # Start solving problem: if p.iprint >= 0: p.disp('\n' + '-' * 25 + ' OpenOpt %s ' % version + '-' * 25) pt = p.probType if p.probType != 'NLSP' else 'SNLE' s = 'solver: ' + p.solver.__name__ + ' problem: ' + p.name + ' type: %s' % pt if p.showGoal: s += ' goal: ' + p.goal p.disp(s) p.extras = {} try: # if 0 and isConverter: # pass # # TODO: will R be somewhere used? # #R = converter(solverName, **solver_params) # else: nErr = check(p) if nErr: p.err( "prob check results: " + str(nErr) + "ERRORS!") #however, I guess this line will be never reached. if p.probType not in ('IP', 'EIG'): p.iterfcn(p.x0) if hasSetproctitleModule: try: originalName = setproctitle.getproctitle() if originalName.startswith('OpenOpt-'): originalName = None else: s = 'OpenOpt-' + p.solver.__name__ # if p.name != 'unnamed': s += '-' + p.name setproctitle.setproctitle(s) except: originalName = None else: p.pWarn(''' please install setproctitle module (it's available via easy_install and Linux soft channels like apt-get)''' ) # import os, cProfile # print '----' # print 'p.n, p.nb, p.nbeq, p.nc, p.nh:', p.n, p.nb, p.nbeq, p.nc, p.nh # if p.solver.__name__ == 'interalg': # cProfile.runctx( 'solver(p)', globals(), locals(), filename="t.profile" ) # #os.system('runsnake t.profile') # else: # solver(p) solver(p) # if p.solver.__name__ == 'interalg': # # print p.extras # os.system('pyprof2calltree -i t.profile -o myfile.prof.grind') # os.system('kcachegrind myfile.prof.grind') # print p.xk # print '====' # except killThread: # if p.plot: # print 'exiting pylab' # import pylab # if hasattr(p, 'figure'): # print 'closing figure' # #p.figure.canvas.draw_drawable = lambda: None # pylab.ioff() # pylab.close() # #pylab.draw() # #pylab.close() # print 'pylab exited' # return None except isSolved: # p.fk = p.f(p.xk) # p.xf = p.xk # p.ff = p.objFuncMultiple2Single(p.fk) if p.istop == 0: p.istop = 1000 finally: if p.isFDmodel: for v in p.freeVarsSet | p.fixedVarsSet: if v.fields != (): v.domain, v.aux_domain = v.aux_domain, v.domain seterr(**old_err) if hasSetproctitleModule and originalName is not None: setproctitle.setproctitle(originalName) ############################ p.contol = p.primalConTol # Solving finished if hasattr(p, '_bestPoint') and not any(isnan( p._bestPoint.x)) and p.probType != 'ODE': try: p.iterfcn(p._bestPoint) except isSolved: pass if p.probType != 'EIG': if not hasattr(p, 'xf') and not hasattr(p, 'xk'): p.xf = p.xk = ones(p.n) * nan if hasattr(p, 'xf') and (not hasattr(p, 'xk') or array_equal(p.xk, p.x0)): p.xk = p.xf if not hasattr(p, 'xf') or all(isnan(p.xf)): p.xf = p.xk if p.xf is nan: p.xf = p.xk = ones(p.n) * nan if p.isFeas(p.xf) and (not p.probType == 'MINLP' or p.discreteConstraintsAreSatisfied(p.xf)): p.isFeasible = True else: p.isFeasible = False else: p.isFeasible = True # check it! p.isFinished = True # After the feasibility check above! if p.probType == 'MOP': p.isFeasible = True elif p.probType == 'IP': p.isFeasible = p.rk < p.ftol else: p.ff = p.fk = p.objFunc(p.xk) # walkaround for PyPy: if type(p.ff) == ndarray and p.ff.size == 1: p.ff = p.fk = asscalar(p.ff) if not hasattr(p, 'ff') or any(p.ff == nan): p.iterfcn, tmp_iterfcn = lambda *args: None, p.iterfcn p.ff = p.fk p.iterfcn = tmp_iterfcn if p.invertObjFunc: p.fk, p.ff = -p.fk, -p.ff if asfarray(p.ff).size > 1: p.ff = p.objFuncMultiple2Single(p.fk) #p.ff = p.objFuncMultiple2Single(p.ff) #if not hasattr(p, 'xf'): p.xf = p.xk if type(p.xf) in (list, tuple) or isscalar(p.xf): p.xf = asarray(p.xf) p.xf = p.xf.flatten() p.rf = p.getMaxResidual(p.xf) if p.probType not in ('IP', 'ODE') else p.rk if not p.isFeasible and p.istop > 0: p.istop = -100 - p.istop / 1000.0 if p.istop == 0 and p.iter >= p.maxIter: p.istop, p.msg = IS_MAX_ITER_REACHED, 'Max Iter has been reached' p.stopcase = stopcase(p) p.xk, p.rk = p.xf, p.rf if p.invertObjFunc: p.fk = -p.ff p.iterfcn(p.xf, -p.ff, p.rf) else: p.fk = p.ff p.iterfcn(p.xf, p.ff, p.rf) p.__finalize__() if not p.storeIterPoints: delattr(p.iterValues, 'x') r = OpenOptResult(p) #TODO: add scaling handling!!!!!!! # for fn in ('df', 'dc', 'dh', 'd2f', 'd2c', 'd2h'): # if hasattr(p, '_' + fn): setattr(r, fn, getattr(p, '_'+fn)) p.invertObjFunc = False if p.isFDmodel: p.x0 = p._x0 finalTextOutput(p, r) if not hasattr(p, 'isManagerUsed') or p.isManagerUsed == False: finalShow(p) return r