Changeset 738

Show
Ignore:
Timestamp:
09/05/10 19:11:13 (1 year ago)
Author:
dmitrey
Message:

some changes toward oosystem solve

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • PythonPackages/FuncDesigner/FuncDesigner/ooSystem.py

    r733 r738  
    8484    def minimize(self, *args, **kwargs): 
    8585        kwargs['goal'] = 'minimum' 
    86         return self._optimize(*args, **kwargs) 
     86        return self._solve(*args, **kwargs) 
    8787         
    8888    def maximize(self, *args, **kwargs): 
    8989        kwargs['goal'] = 'maximum' 
    90         return self._optimize(*args, **kwargs) 
    91  
    92     def _optimize(self, objective, *args, **kwargs): 
    93         if isinstance(objective, BaseFDConstraint): 
    94             raise FuncDesignerException("1st argument can't be of type 'FuncDesigner constraint', it should be 'FuncDesigner oofun'") 
    95         elif not isinstance(objective, oofun): 
    96             raise FuncDesignerException('1st argument should be objective function of type "FuncDesigner oofun"') 
    97          
    98         constraints = self._getAllConstraints() 
     90        return self._solve(*args, **kwargs) 
     91 
     92    def solve(self, *args, **kwargs): 
     93        kwargs['goal'] = 'solution' 
     94        assert len(args) == 0 or all([not isinstance(arg, oofun) for arg in args]) 
     95        return self._solve(*args, **kwargs) 
     96 
     97    def _solve(self, *args, **kwargs): 
    9998         
    10099        #!!!!! TODO: determing LP, MILP, MINLP if possible 
    101         
    102100        try: 
    103101            import openopt 
    104102        except: 
    105             raise FuncDesignerException('to perform optimization you should have openopt installed') 
    106              
    107         if 'constraints' in kwargs: 
    108             # TODO: mention it in doc 
    109             kwargs['constraints'] = set(kwargs['constraints']).update(set(constraints)) 
    110         else: 
    111             kwargs['constraints'] = constraints 
    112          
    113         optVars = kwargs.get('optVars', None) 
    114         fixedVars = kwargs.get('fixedVars', None) 
    115         isLinear = objective.getOrder(optVars, fixedVars) < 2 and all([(c.oofun.getOrder(optVars, fixedVars) < 2) for c in constraints]) 
    116         if isLinear: 
    117             p = openopt.LP(objective, *args, **kwargs) 
    118             if 'solver' not in kwargs: 
    119                 for solver in self.lpSolvers: 
    120                     if (':' not in solver and not openopt.oosolver(solver).isInstalled )or (solver == 'glpk' and not openopt.oosolver('cvxopt_lp').isInstalled): 
    121                         continue 
    122                     if solver == 'glpk' : 
    123                         p2 = openopt.LP([1, -1], lb = [1, 1], ub=[10, 10]) 
    124                         try: 
    125                             r = p2.solve('glpk', iprint=-5) 
    126                         except: 
     103            raise FuncDesignerException('to perform the operation you should have openopt installed') 
     104             
     105        constraints = self._getAllConstraints() 
     106             
     107        # TODO: mention it in doc 
     108        kwargs['constraints'] = set(kwargs['constraints']).update(set(constraints)) if 'constraints' in kwargs else constraints 
     109         
     110        optVars, fixedVars = kwargs.get('optVars', None), kwargs.get('fixedVars', None) 
     111        isSystemOfEquations = kwargs['goal'] == 'solution' 
     112         
     113        isLinear = all([(c.oofun.getOrder(optVars, fixedVars) < 2) for c in constraints]) 
     114        #if not isSystemOfEquations:  
     115         
     116         
     117        if isSystemOfEquations: 
     118            if isLinear: # Linear equations system 
     119                p = openopt.SLE(*args, **kwargs) 
     120            else: # Nonlinear equations system 
     121                p = openopt.NLSP(*args, **kwargs) 
     122                 
     123        else: # an optimization problem 
     124            assert len(args) > 0,  'you should have objective function as 1st argument' 
     125            objective = args[0] 
     126            if isinstance(objective, BaseFDConstraint): 
     127                raise FuncDesignerException("1st argument can't be of type 'FuncDesigner constraint', it should be 'FuncDesigner oofun'") 
     128            elif not isinstance(objective, oofun): 
     129                raise FuncDesignerException('1st argument should be objective function of type "FuncDesigner oofun"') 
     130             
     131            isLinear &= objective.getOrder(optVars, fixedVars) < 2 
     132             
     133            if isLinear: 
     134                p = openopt.LP(*args, **kwargs) 
     135                if 'solver' not in kwargs: 
     136                    for solver in self.lpSolvers: 
     137                        if (':' not in solver and not openopt.oosolver(solver).isInstalled )or (solver == 'glpk' and not openopt.oosolver('cvxopt_lp').isInstalled): 
    127138                            continue 
    128                         if r.istop < 0: 
    129                             continue 
    130                         else: 
    131                             break 
    132                 if ':' in solver: 
    133                     pWarn('You have linear problem but no linear solver (lpSolve, glpk, cvxopt_lp) is installed; converter to NLP will be used.') 
    134                 p.solver = solver 
    135         else: 
    136             p = openopt.NLP(objective, *args, **kwargs) 
    137             if 'solver' not in kwargs: 
    138                 p.solver = 'ralg' 
     139                        if solver == 'glpk' : 
     140                            p2 = openopt.LP([1, -1], lb = [1, 1], ub=[10, 10]) 
     141                            try: 
     142                                r = p2.solve('glpk', iprint=-5) 
     143                            except: 
     144                                continue 
     145                            if r.istop < 0: 
     146                                continue 
     147                            else: 
     148                                break 
     149                    if ':' in solver: 
     150                        pWarn('You have linear problem but no linear solver (lpSolve, glpk, cvxopt_lp) is installed; converter to NLP will be used.') 
     151                    p.solver = solver 
     152            else: 
     153                p = openopt.NLP(*args, **kwargs) 
     154                if 'solver' not in kwargs: 
     155                    p.solver = 'ralg' 
    139156        # TODO: solver autoselect 
    140157        #if p.iprint >= 0: p.disp('The optimization problem is  ' + p.probType) 
  • PythonPackages/OpenOpt/openopt/kernel/NLSP.py

    r405 r738  
    1010    _optionalData = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 
    1111    probType = 'NLSP' 
    12     goal = 'minimize residual
    13     allowedGoals = ['minimize residual'] 
     12    goal = 'solution
     13    allowedGoals = ['solution'] 
    1414    isObjFunValueASingleNumber = False 
    1515    showGoal = False