Пример #1
0
    def _Prepare(self):
        if self._isPrepared: return
        self._isPrepared = True
        if isinstance(self.d, dict): # FuncDesigner startPoint 
            self.x0 = self.d
        MatrixProblem._Prepare(self)
        if self.isFDmodel:
            equations = self.C
            AsSparse = bool(self.useSparse) if type(self.useSparse) != str else self._useSparse()
            C, d = [], []
            if len(self._fixedVars) < len(self._freeVars):
                Z = dict([(v, zeros_like(self._x0[v]) if v not in self._fixedVars else self._x0[v]) for v in self._x0.keys()])
            else:
                Z = dict([(v, zeros_like(self._x0[v]) if v in self._freeVars else self._x0[v]) for v in self._x0.keys()])
            #Z = self.x0#self._vector2point(zeros(self.n))

            for lin_oofun in equations:
                if lin_oofun.getOrder(self.freeVars, self.fixedVars) > 1:
                    raise OpenOptException('SLE constructor requires all equations to be linear')
                C.append(self._pointDerivative2array(lin_oofun.D(Z, **self._D_kwargs), useSparse = AsSparse))
                d.append(-lin_oofun(Z))
                
            self.d = hstack(d).flatten()
            self.C  = Vstack(C)
            
            if hasattr(self.C, 'tocsc'): self.C_as_csc = self.C.tocsc()
            if isinstance(self.C,ndarray) and self.n > 100 and len(flatnonzero(self.C))/self.C.size < 0.3:
                s = "Probably you'd better solve this SLE as sparse"
                if not scipyInstalled: s += ' (requires scipy installed)'
                self.pWarn(s)


        self.x0 = zeros(self.C.shape[1])
Пример #2
0
    def _Prepare(self):
        if self._isPrepared: return
        self._isPrepared = True
        if isinstance(self.d, dict): # FuncDesigner startPoint 
            self.x0 = self.d
        MatrixProblem._Prepare(self)
        if self.isFDmodel:
            equations = self.C
            AsSparse = bool(self.useSparse) if type(self.useSparse) != str else self._useSparse()
            C, d = [], []
            if len(self._fixedVars) < len(self._freeVars):
                Z = dict([(v, zeros_like(self._x0[v]) if v not in self._fixedVars else self._x0[v]) for v in self._x0.keys()])
            else:
                Z = dict([(v, zeros_like(self._x0[v]) if v in self._freeVars else self._x0[v]) for v in self._x0.keys()])
            #Z = self.x0#self._vector2point(zeros(self.n))

            for lin_oofun in equations:
                if lin_oofun.getOrder(self.freeVars, self.fixedVars) > 1:
                    raise OpenOptException('SLE constructor requires all equations to be linear')
                C.append(self._pointDerivative2array(lin_oofun.D(Z, **self._D_kwargs), useSparse = AsSparse))
                d.append(-lin_oofun(Z))
                
            self.d = hstack(d).flatten()
            self.C  = Vstack(C)
            
            if hasattr(self.C, 'tocsc'): self.C_as_csc = self.C.tocsc()
            if isinstance(self.C,ndarray) and self.n > 100 and len(flatnonzero(self.C))/self.C.size < 0.3:
                s = "Probably you'd better solve this SLE as sparse"
                if not scipyInstalled: s += ' (requires scipy installed)'
                self.pWarn(s)


        self.x0 = zeros(self.C.shape[1])
Пример #3
0
    def _Prepare(self):
        if self._lp_prepared: return
        self._lp_prepared = True
        MatrixProblem._Prepare(self)
        if self.x0 is None: self.x0 = zeros(self.n)
        if hasattr(self.f, 'is_oovar'): # hence is oofun or oovar
            # freeVars and fixedVars are already rendered to both not-None here (if it's the case)
            # but current .D() implementation doesn't allow to use both arguments
            # BTW translator ignores fixed vars in derivatives, so passing fixedVars here can be omitted 
            # but is performed for more safety wrt future changes
            _f = self._point2vector(self.f.D(self._x0, fixedVars = self.fixedVars)) 
            self.f, self._f = _f, self.f
            self.user.f = (self._f, )
            self._init_f_vector = _f # we don't take p.goal into account here
            _c = self._f(self._x0) - dot(self.f, self.x0)
            self._c = _c
        else:
            self._init_f_vector = self.f # we don't take p.goal into account here
            self._c = 0
        self.f = atleast_1d(self.f)
        if not hasattr(self, 'n'): self.n = len(self.f)
        #print 'lb:', self.lb, 'ub:', self.ub
        if not hasattr(self, 'lb'): self.lb = -inf * ones(self.n)
        if not hasattr(self, 'ub'): self.ub = inf * ones(self.n)
#        if any(isnan(self.lb)): 
#            if self.lb.size != 1: self.err('NaN in lower bound for a variable from the problem')
#            self.lb = -inf * ones(self.n)
#        if any(isnan(self.ub)): 
#            if self.ub.size != 1: self.err('NaN in upper bound for a variable from the problem')
#            self.ub = inf * ones(self.n)
        
        if self.goal in ['max', 'maximum']:
            self.f = -asfarray(self.f)
Пример #4
0
    def _Prepare(self):
        if self._lp_prepared: return
        self._lp_prepared = True
        MatrixProblem._Prepare(self)
        if self.x0 is None: self.x0 = zeros(self.n)
        if hasattr(self.f, 'is_oovar'):  # hence is oofun or oovar
            # freeVars and fixedVars are already rendered to both not-None here (if it's the case)
            # but current .D() implementation doesn't allow to use both arguments
            # BTW translator ignores fixed vars in derivatives, so passing fixedVars here can be omitted
            # but is performed for more safety wrt future changes
            _f = self._point2vector(
                self.f.D(self._x0, fixedVars=self.fixedVars))
            self.f, self._f = _f, self.f
            self.user.f = (self._f, )
            self._init_f_vector = _f  # we don't take p.goal into account here
            _c = self._f(self._x0) - dot(self.f, self.x0)
            self._c = _c
        else:
            self._init_f_vector = self.f  # we don't take p.goal into account here
            self._c = 0
        self.f = atleast_1d(self.f)
        if not hasattr(self, 'n'): self.n = len(self.f)
        #print 'lb:', self.lb, 'ub:', self.ub
        if not hasattr(self, 'lb'): self.lb = -inf * ones(self.n)
        if not hasattr(self, 'ub'): self.ub = inf * ones(self.n)
        #        if any(isnan(self.lb)):
        #            if self.lb.size != 1: self.err('NaN in lower bound for a variable from the problem')
        #            self.lb = -inf * ones(self.n)
        #        if any(isnan(self.ub)):
        #            if self.ub.size != 1: self.err('NaN in upper bound for a variable from the problem')
        #            self.ub = inf * ones(self.n)

        if self.goal in ['max', 'maximum']:
            self.f = -asfarray(self.f)
Пример #5
0
 def _Prepare(self):
     MatrixProblem._Prepare(self)
     if self._isFDmodel():
         C, d, q, s = renderFDmodel(self)        
         self.C, self.d, self.q, self.s = C, d, q, s
         D = self.f.D(self._x0, fixedVars = self.fixedVars)
         _f = self._point2vector(D).flatten()
         self.f, self._f = _f, self.f
Пример #6
0
 def _Prepare(self):
     if isinstance(self.d, dict): # FuncDesigner startPoint 
         self.x0 = self.d
     MatrixProblem._Prepare(self)
     if self.isFDmodel:
         self.C, self.d = self._linearOOFunsToMatrices(self.C)
     if not self.damp is None and (not hasattr(self, 'X') or not any(isfinite(self.X))):
         self.X = zeros(self.n)
Пример #7
0
 def _Prepare(self):
     if isinstance(self.d, dict):  # FuncDesigner startPoint
         self.x0 = self.d
     MatrixProblem._Prepare(self)
     if self.isFDmodel:
         self.C, self.d = self._linearOOFunsToMatrices(self.C)
     if not self.damp is None and (not hasattr(self, 'X')
                                   or not any(isfinite(self.X))):
         self.X = zeros(self.n)
Пример #8
0
 def _Prepare(self):
     MatrixProblem._Prepare(self)
     if self.solver.__name__ in ['cvxopt_sdp', 'dsdp']:
         try:
             from cvxopt.base import matrix
             matrixConverter = lambda x: matrix(x, tc='d')
         except:
             self.err('cvxopt must be installed')
     else:
         matrixConverter = asfarray
     for i in self.S.keys(): self.S[i] = matrixConverter(self.S[i])
     for i in self.d.keys(): self.d[i] = matrixConverter(self.d[i])
Пример #9
0
    def _Prepare(self):
        if not self._isFDmodel():
            if not isspmatrix(self.H):
                self.H = asfarray(
                    self.H)  # TODO: handle the case in runProbSolver()
            self.n = self.H.shape[0]
            if not hasattr(self, 'x0') or self.x0 is None or self.x0[0] is nan:
                self.x0 = zeros(self.n)
        MatrixProblem._Prepare(self)
        if self._isFDmodel():
            H, f, C = quad_render(self.H, self)
            self.user.f = (self.H, )
            self.H, self.f, self.C = H, f, C
            #            print(H, f, C)

            if self.fixedVars is None or (self.freeVars is not None and len(
                    self.freeVars) < len(self.fixedVars)):
                order_kw = {'Vars': self.freeVarsSet}
            else:
                order_kw = {'fixedVars': self.fixedVarsSet}
            order_kw['fixedVarsScheduleID'] = self._FDVarsID
            from FuncDesigner import ooarray
            for c in self.constraints:
                if isinstance(c, ooarray):
                    for elem in c:
                        processConstraint(elem, order_kw, self)
                else:
                    processConstraint(c, order_kw, self)
            r = []
            for v in self._freeVarsList:
                if isinstance(v.domain, (tuple, list, ndarray, set)):
                    self.err(
                        'for FuncDesigner MILP models only variables with domains int, bool or None (real) are implemented for now'
                    )
                if v.domain is int or v.domain is 'int' or v.domain is bool or v.domain is 'bool':
                    r1, r2 = self._oovarsIndDict[v]
                    r += arange(r1, r2).tolist()

            self.intVars, self._intVars = r, self.intVars
        self._intVars_vector = self.intVars
Пример #10
0
 def _Prepare(self):
     MatrixProblem._Prepare(self)
     if not self.damp is None and not any(isfinite(self.X)):
         self.X = zeros(self.n)
Пример #11
0
 def _Prepare(self):
     # TODO: handle cvxopt sparse matrix case here
     self.n = self.H.shape[0]
     if not hasattr(self, 'x0') or self.x0 is None or self.x0[0] == nan:
         self.x0 = zeros(self.n)
     MatrixProblem._Prepare(self)
Пример #12
0
 def _Prepare(self):
     MatrixProblem._Prepare(self)
     if not self.damp is None and not any(isfinite(self.X)):
         self.X = zeros(self.n)
Пример #13
0
 def _Prepare(self):
     # TODO: handle cvxopt sparse matrix case here
     self.n = self.H.shape[0]
     if not hasattr(self, 'x0') or self.x0 is None or self.x0[0] == nan:
         self.x0 = zeros(self.n)
     MatrixProblem._Prepare(self)