示例#1
0
 def _Prepare(self):
     if hasattr(self, 'prepared') and self.prepared == True:
         return
     NonLinProblem._Prepare(self)    
     if self.isFDmodel:
         r = {}
         for iv in self.freeVars:
             if iv.domain is None: continue
             ind1, ind2 = self._oovarsIndDict[iv]
             assert ind2-ind1 == 1 
             r[ind1] = iv.domain
         self.discreteVars = r
     # TODO: use something else instead of dict.keys()
     for key in self.discreteVars.keys():
         fv = self.discreteVars[key]
         if type(fv) not in [list, tuple, ndarray] and fv not in ('bool', bool):
             self.err('each element from discreteVars dictionary should be list or tuple of allowed values')
         if fv is not bool and fv is not 'bool': fv = sort(fv)
         lowest = 0 if fv is bool or fv is 'bool' else fv[0] 
         biggest = 1 if fv is bool or fv is 'bool' else fv[-1] 
         if lowest > self.ub[key]:
             self.err('variable '+ str(key)+ ': smallest allowed discrete value ' + str(fv[0]) + ' exeeds imposed upper bound '+ str(self.ub[key]))
         if biggest < self.lb[key]:
             self.err('variable '+ str(key)+ ': biggest allowed discrete value ' + str(fv[-1]) + ' is less than imposed lower bound '+ str(self.lb[key]))
         self.discreteVars[key] = fv
示例#2
0
    def __init__(self, *args, **kwargs):
        #if len(args) > 1: self.err('incorrect args number for GLP constructor, must be 0..1 + (optionaly) some kwargs')

        NonLinProblem.__init__(self, *args, **kwargs)
        
        def maxNonSuccess(p):
            newPoint = p.point(p.xk)
            if self._currentBestPoint is None:
                self._currentBestPoint = newPoint
                return False
            elif newPoint.betterThan(self._currentBestPoint):
                self._currentBestPoint = newPoint
                self._nonSuccessCounter = 0
                return False
            self._nonSuccessCounter += 1
            if self._nonSuccessCounter > self.maxNonSuccess:
                return (True, 'Non-Success Number > maxNonSuccess = ' + str(self.maxNonSuccess))
            else:
                return False
        
        self.kernelIterFuncs[MAX_NON_SUCCESS] = maxNonSuccess
        if 'lb' in kwargs.keys():
            self.n = len(kwargs['lb'])
        elif 'ub' in kwargs.keys():
            self.n = len(kwargs['ub'])
        if hasattr(self, 'n'):
            if not hasattr(self, 'lb'):
                self.lb = -inf * ones(self.n)
            if not hasattr(self, 'ub'):
                self.ub =  inf * ones(self.n)
            if 'x0' not in kwargs and len(args) < 2: 
                self.x0 = (asarray(self.lb) + asarray(self.ub)) / 2.0
示例#3
0
    def _Prepare(self):
        self.X = atleast_2d(self.X)
        self.Y = array(self.Y, float)
        if self.X.shape[0] != self.Y.shape[0]:
            if self.X.shape[1] != self.Y.shape[0]:
                self.err('incorrect shape of input data')
            else:
                self.X = self.X.T
        NonLinProblem._Prepare(self)
        #if self.Y.ndim

        if self.userProvided.df:
            assert len(self.user.df) == 1
            self.dfff = self.user.df[0]

            def dff(x):
                r = zeros(self.n)
                for i in range(self.Y.shape[0]):
                    #print asfarray(self.fff(x, self.X[i])-self.Y[i]), asfarray(self.dfff(x, self.X[i]))
                    r += dot(
                        2.0 * asfarray(self.fff(x, self.X[i]) - self.Y[i]),
                        asfarray(self.dfff(x, self.X[i])))
                return r

            self.df = self.user.df = dff
示例#4
0
 def __init__(self, *args, **kwargs):
     NonLinProblem.__init__(self, *args, **kwargs)
     self.nSolutions = 'all'
     self.kernelIterFuncs.pop(SMALL_DELTA_X, None)
     self.kernelIterFuncs.pop(SMALL_DELTA_F, None)
     self.data4TextOutput = ['front length', 'income', 'outcome', 'log10(maxResidual)']
     f = self.f
     i = 0
     targets = []
     while True:
         if len(f[i:]) == 0: break
         func = f[i]
         if type(func) in (list, tuple):
             F, tol, val = func
             i += 1
         else:
             F, tol, val = f[i], f[i+1], f[i+2]
             i += 3
         t = target()
         t.func, t.tol = F, tol
         t.val = val if type(val) != str \
         else inf if val in ('max', 'maximum') \
         else -inf if val in ('min', 'minimum') \
         else self.err('incorrect MOP func target')
         targets.append(t)
     self.targets = targets
     self.f = [t.func for t in targets]
     self.user.f = self.f
示例#5
0
文件: IP.py 项目: javafx2010/OOSuite
 def __init__(self, *args, **kwargs):
     NonLinProblem.__init__(self, *args, **kwargs)
     domain = args[1]
     self.x0 = dict([(v, 0.5*(val[0]+val[1])) for v, val in domain.items()])
     self.constraints = [v>bounds[0] for v,  bounds in domain.items()] + [v<bounds[1] for v,  bounds in domain.items()]
     #self.data4TextOutput = ['objFunVal', 'residual']
     self._Residual = inf
示例#6
0
 def _Prepare(self):
     if hasattr(self, 'prepared') and self.prepared == True:
         return
     NonLinProblem._Prepare(self)
     self.prepared = True
     if self.isFDmodel:
         r = {}
         for iv in self.freeVars:
             if iv.domain is None: continue
             ind1, ind2 = self._oovarsIndDict[iv]
             assert ind2 - ind1 == 1
             r[ind1] = iv.domain
         self.discreteVars = r
     # TODO: use something else instead of dict.keys()
     new_discr = {}
     for key, fv in self.discreteVars.items():
         if type(fv) not in [list, tuple, ndarray
                             ] and fv not in ('bool', bool):
             self.err(
                 'each element from discreteVars dictionary should be list or tuple of allowed values'
             )
         if fv is not bool and fv is not 'bool':
             new_discr[key] = sort(fv)
         lowest = 0 if fv is bool or fv is 'bool' else fv[0]
         biggest = 1 if fv is bool or fv is 'bool' else fv[-1]
         if lowest > self.ub[key]:
             self.err('variable ' + str(key) +
                      ': smallest allowed discrete value ' + str(fv[0]) +
                      ' exeeds imposed upper bound ' + str(self.ub[key]))
         if biggest < self.lb[key]:
             self.err('variable ' + str(key) +
                      ': biggest allowed discrete value ' + str(fv[-1]) +
                      ' is less than imposed lower bound ' +
                      str(self.lb[key]))
     self.discreteVars.update(new_discr)
示例#7
0
    def __init__(self, *args, **kwargs):
        #if len(args) > 1: self.err('incorrect args number for GLP constructor, must be 0..1 + (optionaly) some kwargs')

        NonLinProblem.__init__(self, *args, **kwargs)

        def maxNonSuccess(p):
            newPoint = p.point(p.xk)
            if self._currentBestPoint is None:
                self._currentBestPoint = newPoint
                return False
            elif newPoint.betterThan(self._currentBestPoint):
                self._currentBestPoint = newPoint
                self._nonSuccessCounter = 0
                return False
            self._nonSuccessCounter += 1
            if self._nonSuccessCounter > self.maxNonSuccess:
                return (True, 'Non-Success Number > maxNonSuccess = ' +
                        str(self.maxNonSuccess))
            else:
                return False

        self.kernelIterFuncs[MAX_NON_SUCCESS] = maxNonSuccess
        if 'lb' in kwargs.keys():
            self.n = len(kwargs['lb'])
        elif 'ub' in kwargs.keys():
            self.n = len(kwargs['ub'])
        if hasattr(self, 'n'):
            if not hasattr(self, 'lb'):
                self.lb = -inf * ones(self.n)
            if not hasattr(self, 'ub'):
                self.ub = inf * ones(self.n)
            if 'x0' not in kwargs and len(args) < 2:
                self.x0 = (asarray(self.lb) + asarray(self.ub)) / 2.0
示例#8
0
 def __init__(self, *args, **kwargs):
     NonLinProblem.__init__(self, *args, **kwargs)
     domain = args[1]
     self.x0 = dict([(v, 0.5 * (val[0] + val[1])) for v, val in domain.items()])
     self.constraints = [v > bounds[0] for v, bounds in domain.items()] + [
         v < bounds[1] for v, bounds in domain.items()
     ]
     # self.data4TextOutput = ['objFunVal', 'residual']
     self._Residual = inf
示例#9
0
文件: DFP.py 项目: javafx2010/OOSuite
 def __init__(self, *args, **kwargs):
     NonLinProblem.__init__(self, *args, **kwargs)
     assignScript(self, kwargs)
     self.fff = self.f
     def ff(x):
         r = []
         for i in range(self.Y.shape[0]):
             r.append(asfarray(self.fff(x, self.X[i])-self.Y[i])**2)
         return r
     self.f = ff
示例#10
0
 def solve(self, *args, **kw):
     #        if self.plot or kw.get('plot', False):
     #            self.warn('\ninteractive graphic output for MOP is unimplemented yet and will be turned off')
     #            kw['plot'] = False
     self.graphics.drawFuncs = [mop_iterdraw]
     r = NonLinProblem.solve(self, *args, **kw)
     r.plot = lambda *args, **kw: self._plot(**kw)
     r.__call__ = lambda *args, **kw: self.err(
         'evaluation of MOP result on arguments is unimplemented yet, use r.solutions'
     )
     r.export = lambda *args, **Kw: _export_to_xls(self, r, *args, **kw)
     T0 = self.targets[0]
     if T0.val == -inf:
         keyfunc = lambda elem: elem[T0.func]
     elif T0.val == inf:
         keyfunc = lambda elem: -elem[T0.func]
     else:
         keyfunc = lambda elem: abs(T0.val - elem[T0.func])
     r.solutions.sort(key=keyfunc)
     for v in self._categoricalVars:
         for elem in r.solutions:
             elem.useAsMutable = True
             elem[v] = v.aux_domain[elem[v]]
             elem.useAsMutable = False
     return r
示例#11
0
    def _Prepare(self):
        self.X = atleast_2d(self.X)
        self.Y = array(self.Y, float)
        if self.X.shape[0] != self.Y.shape[0]:
            if self.X.shape[1] != self.Y.shape[0]:
                self.err('incorrect shape of input data')
            else:
                self.X = self.X.T
        NonLinProblem._Prepare(self)
        #if self.Y.ndim

        if self.userProvided.df:
            assert len(self.user.df) == 1
            self.dfff = self.user.df[0]
            def dff(x):
                r = zeros(self.n)
                for i in range(self.Y.shape[0]):
                    #print asfarray(self.fff(x, self.X[i])-self.Y[i]), asfarray(self.dfff(x, self.X[i]))
                    r += dot(2.0 * asfarray(self.fff(x, self.X[i])-self.Y[i]), asfarray(self.dfff(x, self.X[i])))
                return r        
            self.df = self.user.df = dff
示例#12
0
    def solve(self, *args, **kw):
#        if self.plot or kw.get('plot', False):
#            self.warn('\ninteractive graphic output for MOP is unimplemented yet and will be turned off')
#            kw['plot'] = False
        self.graphics.drawFuncs = [mop_iterdraw]
        r = NonLinProblem.solve(self, *args, **kw)
        r.plot = lambda *args, **kw: self._plot(**kw)
        r.__call__ = lambda *args, **kw: self.err('evaluation of MOP result on arguments is unimplemented yet, use r.solutions')
        r.export = lambda *args, **Kw: _export_to_xls(self, r, *args, **kw)
        T0 = self.targets[0]
        if T0.val == -inf:
            keyfunc = lambda elem: elem[T0.func]
        elif T0.val == inf:
            keyfunc = lambda elem: -elem[T0.func]
        else:
            keyfunc = lambda elem: abs(T0.val - elem[T0.func])
        r.solutions.sort(key=keyfunc)
        for v in self._stringVars:
            for elem in r.solutions:
                elem.useAsMutable = True
                elem[v] = v.reverse_aux_domain[int(elem[v])]
                elem.useAsMutable = False
        return r
示例#13
0
文件: DFP.py 项目: javafx2010/OOSuite
 def __finalize__(self):
     NonLinProblem.__finalize__(self)
     if self.userProvided.df:
         self.df = self.dfff
         self.f = self.fff
示例#14
0
 def __init__(self, *args, **kwargs):
     NonLinProblem.__init__(self, *args, **kwargs)
     domain= args[1]
     self.x0 = domain
示例#15
0
 def __init__(self, *args, **kwargs):
     NonLinProblem.__init__(self, *args, **kwargs)
     domain = args[1]
     self.x0 = domain
示例#16
0
 def __init__(self, *args, **kwargs):
     NonLinProblem.__init__(self, *args, **kwargs)
     if 'is_oovar' in dir(self.f):
         self.f = [self.f]
示例#17
0
 def __init__(self, *args, **kwargs):
     self.goal = 'minimum'
     NonLinProblem.__init__(self, *args, **kwargs)
示例#18
0
 def __init__(self, *args, **kwargs):
     self.goal = 'minimum'
     self.discreteVars = {}
     NonLinProblem.__init__(self, *args, **kwargs)
     self.iprint = 1
示例#19
0
 def __init__(self, *args, **kwargs):
     self.goal = 'minimum'
     NonLinProblem.__init__(self, *args, **kwargs)
示例#20
0
 def __init__(self, *args, **kwargs):
     NonLinProblem.__init__(self, *args, **kwargs)
示例#21
0
 def __init__(self, *args, **kwargs):
     self.goal = 'minimum'
     self.discreteVars = {}
     NonLinProblem.__init__(self, *args, **kwargs)
     self.iprint=1
示例#22
0
 def __init__(self, *args, **kwargs):
     NonLinProblem.__init__(self, *args, **kwargs)
     if 'is_oovar' in dir(self.f):
         self.f = [self.f]
示例#23
0
 def __init__(self, *args, **kwargs):
     NonLinProblem.__init__(self, *args, **kwargs)