Exemplo n.º 1
0
 def __init__(self,
              xdim=None,
              xopt=None,
              xbound=5,
              feasible=True,
              constrained=False,
              violation=False,
              **args):
     self.feasible = feasible
     self.constrained = constrained
     self.violation = violation
     self.xbound = xbound
     if xdim is None:
         xdim = self.xdim
     if xdim is None:
         xdim = self.xdimMin
     assert xdim >= self.xdimMin and not (self.xdimMax is not None
                                          and xdim > self.xdimMax)
     self.xdim = xdim
     if xopt is None:
         self.xopt = zeros(self.xdim)
     else:
         self.xopt = xopt
     setAllArgs(self, args)
     self.reset()
Exemplo n.º 2
0
    def __init__(self, game, **kwargs):
        setAllArgs(self, kwargs)
        self._game = game
        self._avatar_types = []
        self._abs_avatar_types = []
        self._other_types = []
        self._mortal_types = []
        for skey in sorted(game.sprite_constr):
            sclass, _, stypes = game.sprite_constr[skey]
            if issubclass(sclass, Avatar):
                self._abs_avatar_types += stypes[:-1]
                self._avatar_types += [stypes[-1]]
                if issubclass(sclass, RotatingAvatar) or issubclass(
                        sclass, ShootAvatar):
                    self.orientedAvatar = True
            if skey not in game.sprite_groups:
                continue
            ss = game.sprite_groups[skey]
            if len(ss) == 0:
                continue
            if isinstance(ss[0], Avatar):
                assert issubclass(ss[0].physicstype, GridPhysics), \
                        'Not supported: Game must have grid physics, has %s'\
                        % (self._avatar.physicstype.__name__)
            else:
                self._other_types += [skey]
                if not ss[0].is_static:
                    self.staticOther = False
        assert self.staticOther, "not yet supported: all non-avatar sprites must be static. "

        self._avatar_types = sorted(
            set(self._avatar_types).difference(self._abs_avatar_types))
        self.uniqueAvatar = (len(self._avatar_types) == 1)
        #assert self.uniqueAvatar, 'not yet supported: can only have one avatar class'

        # determine mortality
        for skey, _, effect, _ in game.collision_eff:
            if effect in kill_effects:
                if skey in self._avatar_types + self._abs_avatar_types:
                    self.mortalAvatar = True
                if skey in self._other_types:
                    self.mortalOther = True
                    self._mortal_types += [skey]

        # retain observable features, and their colors
        self._obstypes = {}
        self._obscols = {}
        for skey in self._other_types:
            ss = game.sprite_groups[skey]
            self._obstypes[skey] = [
                self._sprite2state(sprite, oriented=False) for sprite in ss
                if sprite.is_static
            ]
            self._obscols[skey] = ss[0].color

        if self.mortalOther:
            self._gravepoints = {}
            for skey in self._mortal_types:
                for s in self._game.sprite_groups[skey]:
                    self._gravepoints[(skey, self._rect2pos(s.rect))] = True
Exemplo n.º 3
0
 def __init__(self, game, **kwargs):
     setAllArgs(self, kwargs)
     self._game = game
     self._avatar_types = []
     self._abs_avatar_types = []
     self._other_types = []
     self._mortal_types = []
     for skey in sorted(game.sprite_constr): 
         sclass, _, stypes = game.sprite_constr[skey]
         if issubclass(sclass, Avatar):
             self._abs_avatar_types += stypes[:-1]
             self._avatar_types += [stypes[-1]]
             if issubclass(sclass, RotatingAvatar) or issubclass(sclass, ShootAvatar):
                 self.orientedAvatar = True
         if skey not in game.sprite_groups:
             continue 
         ss = game.sprite_groups[skey]
         if len(ss) == 0:
             continue
         if isinstance(ss[0], Avatar):
             assert issubclass(ss[0].physicstype, GridPhysics), \
                     'Not supported: Game must have grid physics, has %s'\
                     % (self._avatar.physicstype.__name__)                       
         else:
             self._other_types += [skey]
             if not ss[0].is_static:
                 self.staticOther = False
     assert self.staticOther, "not yet supported: all non-avatar sprites must be static. "
     
     self._avatar_types = sorted(set(self._avatar_types).difference(self._abs_avatar_types))
     self.uniqueAvatar = (len(self._avatar_types) == 1)
     #assert self.uniqueAvatar, 'not yet supported: can only have one avatar class'
     
     # determine mortality
     for skey, _, effect, _ in game.collision_eff:
         if effect in kill_effects:
             if skey in self._avatar_types+self._abs_avatar_types:
                 self.mortalAvatar = True
             if skey in self._other_types:
                 self.mortalOther = True
                 self._mortal_types += [skey]
     
              
     # retain observable features, and their colors
     self._obstypes = {}
     self._obscols = {}
     for skey in self._other_types:
         ss = game.sprite_groups[skey]
         self._obstypes[skey] = [self._sprite2state(sprite, oriented=False) 
                                 for sprite in ss if sprite.is_static]
         self._obscols[skey] = ss[0].color            
     
     if self.mortalOther:
         self._gravepoints = {}
         for skey in self._mortal_types:
             for s in self._game.sprite_groups[skey]:
                 self._gravepoints[(skey, self._rect2pos(s.rect))] = True
Exemplo n.º 4
0
 def __init__(self, agentName = "UnnamedClient", host = 'localhost', port = 4242, **otherargs):
     """General TCP Environment"""
     setAllArgs(self, otherargs)
     self.host = host
     self.port = port
     if self.verbose:
         print "TCPENV: agentName ", agentName
     self.client = Client(host, port, agentName)
     self.connected = True
Exemplo n.º 5
0
 def __init__(self, provider, init_params, **kwargs):
     self.provider = provider
     self.parameters = init_params.copy()
     self.paramdim = len(init_params)
     self._num_updates = 0
     setAllArgs(self, kwargs)
     self._additionalInit()
     if self.verbose:
         self._printStuff()
Exemplo n.º 6
0
 def __init__(self, provider, init_params, **kwargs):
     self.provider = provider
     self.parameters = init_params.copy()
     self.paramdim = len(init_params)
     self._num_updates = 0
     setAllArgs(self, kwargs)
     self._additionalInit()
     self.callback(self)
     if self.verbose:
         self._printStuff()
Exemplo n.º 7
0
 def __init__(self, xdim = None, xopt = None, **args):
     if xdim is None:
         xdim = self.xdim
     if xdim is None:
         xdim = self.xdimMin
     assert xdim >= self.xdimMin and not (self.xdimMax is not None and xdim > self.xdimMax)
     self.xdim = xdim
     if xopt is None:
         self.xopt = zeros(self.xdim)
     else:
         self.xopt = xopt
     setAllArgs(self, args)
     self.reset()
Exemplo n.º 8
0
 def __init__(self, num_actions, num_features, **kwargs):
     ValueBasedLearner.__init__(self)
     setAllArgs(self, kwargs)
     self.explorer = None
     self.num_actions = num_actions
     self.num_features = num_features
     if self.randomInit:
         self._theta = randn(self.num_actions, self.num_features) / 10.
     else:
         self._theta = zeros((self.num_actions, self.num_features))
     self._additionalInit()
     self._behaviorPolicy = self._boltzmannPolicy
     self.reset()
Exemplo n.º 9
0
 def __init__(self, num_actions, num_features, **kwargs):
     ValueBasedLearner.__init__(self)
     setAllArgs(self, kwargs)
     self.explorer = None        
     self.num_actions = num_actions
     self.num_features = num_features
     if self.randomInit:
         self._theta = randn(self.num_actions, self.num_features) / 10.
     else:
         self._theta = zeros((self.num_actions, self.num_features))
     self._additionalInit()
     self._behaviorPolicy = self._boltzmannPolicy
     self.reset()
Exemplo n.º 10
0
 def __init__(self, xdim = None, xopt = None, **args):
     if xdim is None:
         xdim = self.xdim
     if xdim is None:
         xdim = self.xdimMin
     assert xdim >= self.xdimMin and not (self.xdimMax is not None and xdim > self.xdimMax)
     self.xdim = xdim
     if xopt is None:
         self.xopt = zeros(self.xdim)
     else:
         self.xopt = xopt
     setAllArgs(self, args)
     self.reset()
Exemplo n.º 11
0
 def __init__(self, xdim = None, xopt = None, xbound=5, feasible=True, constrained=False, violation=False, **args):
     self.feasible=feasible
     self.constrained=constrained
     self.violation=violation
     self.xbound=xbound
     if xdim is None:
         xdim = self.xdim
     if xdim is None:
         xdim = self.xdimMin
     assert xdim >= self.xdimMin and not (self.xdimMax is not None and xdim > self.xdimMax)
     self.xdim = xdim
     if xopt is None:
         self.xopt = zeros(self.xdim)
     else:
         self.xopt = xopt
     setAllArgs(self, args)
     self.reset()
Exemplo n.º 12
0
 def __init__(self, DS, **kwargs):
     """ Initialize with the training data set DS. All keywords given are set as member variables. 
     The following are particularly important:
     @param hidden: number of hidden units
     @param TDS: test data set for checking convergence
     @param VDS: validation data set for final performance evaluation
     @param epoinc: number of epochs to train for, before checking convergence (default: 5)
     """
     self.DS = DS
     self.hidden=10 
     self.maxepochs=1000
     self.Graph=None
     self.TDS=None
     self.VDS=None
     self.epoinc = 5  
     setAllArgs( self, kwargs)
     self.trainCurve = None
Exemplo n.º 13
0
 def __init__(self, DS, **kwargs):
     """ Initialize with the training data set DS. All keywords given are set as member variables. 
     The following are particularly important:
     @param hidden: number of hidden units
     @param TDS: test data set for checking convergence
     @param VDS: validation data set for final performance evaluation
     @param epoinc: number of epochs to train for, before checking convergence (default: 5)
     """
     self.DS = DS
     self.hidden=10 
     self.maxepochs=1000
     self.Graph=None
     self.TDS=None
     self.VDS=None
     self.epoinc = 5  
     setAllArgs( self, kwargs)
     self.trainCurve = None
 def __init__(self, num_features, num_actions, indexOfAgent=None, **kwargs):
     IndexableValueBasedLearner.__init__(self, indexOfAgent)
     setAllArgs(self, kwargs)
     self.explorer = None
     self.indexOfAgent = indexOfAgent
     self.num_actions = num_actions
     self.num_features = num_features
     if self.randomInit:
         self._theta = randn(self.num_actions, self.num_features) / 10.
     else:
         self._theta = zeros((self.num_actions, self.num_features))
     self._additionalInit()
     self._behaviorPolicy = self._boltzmannPolicy
     self.reset()
     #
     self.ownerAgentProperties["requireOtherAgentsState"] = False
     self.ownerAgentProperties["requireJointAction"] = False
     self.ownerAgentProperties["requireJointReward"] = False
 def __init__(self, num_features, num_actions, indexOfAgent=None, **kwargs):
     IndexableValueBasedLearner.__init__(self, indexOfAgent)
     setAllArgs(self, kwargs)
     self.explorer = None        
     self.indexOfAgent=indexOfAgent
     self.num_actions = num_actions
     self.num_features = num_features
     if self.randomInit:
         self._theta = randn(self.num_actions, self.num_features) / 10.
     else:
         self._theta = zeros((self.num_actions, self.num_features))
     self._additionalInit()
     self._behaviorPolicy = self._boltzmannPolicy
     self.reset()
     #
     self.ownerAgentProperties["requireOtherAgentsState"]=False
     self.ownerAgentProperties["requireJointAction"]=False
     self.ownerAgentProperties["requireJointReward"]=False
Exemplo n.º 16
0
 def __init__(self, evaluator = None, initEvaluable = None, **kwargs):
     """ The evaluator is any callable object (e.g. a lambda function). 
     Algorithm parameters can be set here if provided as keyword arguments. """
     # set all algorithm-specific parameters in one go:
     self.__minimize = None
     self.__evaluator = None
     setAllArgs(self, kwargs)
     # bookkeeping
     self.numEvaluations = 0      
     self.numLearningSteps = 0
     if self.storeAllEvaluated:
         self._allEvaluated = []
         self._allEvaluations = []
     elif self.storeAllEvaluations:
         self._allEvaluations = []
     
     if evaluator is not None:
         self.setEvaluator(evaluator, initEvaluable)        
Exemplo n.º 17
0
 def __init__(self, evaluator = None, initEvaluable = None, **kwargs):
     """ The evaluator is any callable object (e.g. a lambda function). 
     Algorithm parameters can be set here if provided as keyword arguments. """
     # set all algorithm-specific parameters in one go:
     self.__minimize = None
     self.__evaluator = None
     setAllArgs(self, kwargs)
     # bookkeeping
     self.numEvaluations = 0      
     self.numLearningSteps = 0
     if self.storeAllEvaluated:
         self._allEvaluated = []
         self._allEvaluations = []
     elif self.storeAllEvaluations:
         self._allEvaluations = []
     
     if evaluator is not None:
         self.setEvaluator(evaluator, initEvaluable)        
Exemplo n.º 18
0
 def __init__(self, basefun, **kwargs):
     """ Provide a stationary 1D base function. """
     setAllArgs(self, kwargs)
     self._basefun = basefun
     self._optimum = 0
     if not self.startatzero:
         self._jump()
     else:
         self._timer = self.jumptime
         
     # monkey-patching wrapper
     tmp = self._basefun._newSample
     def _oneStep(*args, **kwargs):
         """ the timer is decremented only when _newSamples() is called """
         #print self, self._timer, self._optimum
         if 'override' not in kwargs or kwargs['override'] == False:
             self._timer -= 1
             if self._timer <= 0:
                 self._jump()
         return tmp(*args, **kwargs)                    
     self._basefun._newSample = _oneStep
Exemplo n.º 19
0
    def __init__(self, basefun, **kwargs):
        """ Provide a stationary 1D base function. """
        setAllArgs(self, kwargs)
        self._basefun = basefun
        self._optimum = 0
        if not self.startatzero:
            self._jump()
        else:
            self._timer = self.jumptime

        # monkey-patching wrapper
        tmp = self._basefun._newSample

        def _oneStep(*args, **kwargs):
            """ the timer is decremented only when _newSamples() is called """
            #print self, self._timer, self._optimum
            if 'override' not in kwargs or kwargs['override'] == False:
                self._timer -= 1
                if self._timer <= 0:
                    self._jump()
            return tmp(*args, **kwargs)

        self._basefun._newSample = _oneStep
Exemplo n.º 20
0
 def __init__(self, dataset, module, **kwargs):
     setAllArgs(self, kwargs)
     self.module = module
     self.paramdim = module.paramdim
     self._ready = False
     self.reset(dataset)
Exemplo n.º 21
0
 def __init__(self, **args):
     setAllArgs(self, args)
     self._lastseen = None
Exemplo n.º 22
0
 def set(self, **kwargs):
     """ convenience method to set several member variables at once """
     setAllArgs(self, kwargs)
Exemplo n.º 23
0
 def set(self, **kwargs):
     """ convenience method to set several member variables at once """
     setAllArgs(self, kwargs)
Exemplo n.º 24
0
 def __init__(self, paramdim, loss_fun, gradient_fun, **kwargs):
     self.paramdim = paramdim
     self.loss_fun = loss_fun
     self.gradient_fun = gradient_fun        
     setAllArgs(self, kwargs)
Exemplo n.º 25
0
 def __init__(self, dataset, module, **kwargs):
     setAllArgs(self, kwargs)
     self.module = module
     self.paramdim = module.paramdim
     self._ready = False
     self.reset(dataset)
Exemplo n.º 26
0
 def __init__(self, **args):
     setAllArgs(self, args)
     self._lastseen = None
Exemplo n.º 27
0
 def __init__(self, paramdim, loss_fun, gradient_fun, **kwargs):
     self.paramdim = paramdim
     self.loss_fun = loss_fun
     self.gradient_fun = gradient_fun
     setAllArgs(self, kwargs)