示例#1
0
  def __init__(self,name,imagefile,context,skills=[],factionid=0):
    """
    """
    model = [ (0,0), (0,181), (100,181), (100,0) ]
    bounding_rect = [ (0,0), (0,181), (100,181), (100,0) ]
    #super( Character, self ).__init__( model, imagefile, offset=(0,50) )
    _model = Vertel()
    _model.set( [ ( 0.0, 0.0, 0.0 ), 
                    ( 0.0, 171.0/600.0, 0.0 ), 
                    ( 101.0/800.0, 171.0/600.0,0.0 ),
                    ( 101.0/800.0 , 0.0, 0.0 ) ] )
    _bound = Vertel()
    _bound.set( [ ( 0.0, 0.0, 0.0 ),
                    ( 0.0, 100.0/600, 0.0 ),
                    ( 100.0/800.0, 100.0/600, 0.0 ),
                    ( 100.0/800, 0.0, 0.0 ) ] )
    super( Character, self ).__init__( imagefile, _model, _bound )
    #self.name = kwargs.get('name',"Unnamed")
    self.name = name
    self.mind = Mind( ego=self, factionid=factionid ) 

    self._controller = None

    self._context = context

    self._target = None

    #body stats
    self.strength  =  10  #strength of character, in kg*m*s^-2
    self.agility   =  20  #agility of character,  in kg*m*s^-2
    self.endurance =  10  #endurance: MAX Energy Reserves, in 
                          #endurance 
                          #endurance: MAX energy output in watts in kg*m^2*s^-3
    self._reach = 1.0     #arm length
    self._mass   = 50   #mass of character, in kg
    self.health = 100  #

    self.energy = Energy()

    self._alive  = True #alive bit, used to drop a character object references

    #skills
    self.skillbook  = SkillBook(skills=skills) 
    self.deck       = Deck(maxsize=4, slots=['default']*6 )

    #equipment
    self._equipment = Equipment()     #equipment is currently held by the character
    self.inventory = Inventory(40.0) #items in storage
    self.weapon = None

    #modifiers
    self.buff = Buffable(self) #allows the character to be buffed (stat and status effects)
    self.status_effects = []
    self._wounds = []
示例#2
0
    def __init__(self, envName, numThreads=3):
        self.lock_queue = threading.Lock()
        self.enviromentName = envName
        self.env = Enviroment(envName)
        self.mind = Mind(self.env.envDim(),
                         self.env.actDim(),
                         policyOptimizer=Adam(lr=0.001),
                         criticOptimizer=Adam(lr=0.001))
        self.optimizer = A3C.OptimizerThread(self.mind)
        self.numThreads = numThreads
        self.results = History()

        commons.tensorFlowGraph = K.get_session().graph
示例#3
0
文件: main.py 项目: knighton/rehack
def main():
    m = Mind()
    clauses = [
        DeepClause('see', {
            'agent': 'Tim',
            'target': 'Tom',
        }),
        DeepClause('see', {
            'agent': UNK,
            'target': 'Tom',
        }),
    ]

    for c in clauses:
        print '-' * 80
        print c.to_d()
        for clause in m.receive(c):
            print '*', json.dumps(clause.to_d(), indent=4)
示例#4
0
class Agent:
    def __init__(self):
        self.mind = Mind(self)
        self.init_agent()

    def init_agent(self):
        self.drawer = Drawer(self)
        self.goal = 0
        self.goal_integral = 0
        self.foods_eaten = 0
        self.food_pos = [random.uniform(0, 1) for _ in range(2)]
        self.agent_pos = [random.uniform(0, 1) for _ in range(2)]
        self.agent_vel = [random.uniform(0, 1) for _ in range(2)]

    def live(self, generation):
        for i in range(iterations):
            self.mind.one_iteration(generation)

        return self.goal_integral

    def percept(self):
        distance_to_food = [(self.food_pos[i] - self.agent_pos[i])
                            for i in range(2)]
        self.goal = (self.agent_pos[0] - self.agent_pos[1]
                     ) / 2  #sum([pos for pos in self.agent_pos]) / 2
        #return [self.goal] + distance_to_food + self.agent_vel
        return [self.goal] + self.agent_vel

    def update_sensors(self):
        #self.goal -= goal_decay if self.goal == 0 else 0
        self.goal_integral += self.goal
        for i in range(2):
            self.agent_pos[i] += self.agent_vel[i] / 20
            if not (0 < self.agent_pos[i] < 1):
                self.agent_pos[i] = int(
                    self.agent_pos[i])  #.5 + random.uniform(-.1, .1)#
        '''
示例#5
0
class Prelude(object):

	def __init__(self):
		self.timer = None
		self.isSpeaking = False
		self.proactiveMode = False
		self.brainLocation = "mind.mdu"
		self.quantumRandomness = False
		self.avoidLearnByRepeating = False
		self.associater = MatchingAlgorithm.CosineTFIDF
		self.saveTracking = time.time()

	def initializeEngine(self):
		self.mindInstance = Mind()
		self.mindInstance.associater = self.associater
		self.mindInstance.brainLocation = self.brainLocation
		self.chatInitiated = time.strftime("%H:%M:%S")
		self.mindInstance.analyzeShortTermMemory()


	def chatWithPrelude(self, question):
		if self.mindInstance is None:
			return "Error: Mind not initialized";
		if self.proactiveMode:
			self.idleTime = 0
			if self.timer is not None:
				timer.Stop()
		
		if self.quantumRandomness:
			self.mindInstance.quantumRandomness = True

		if self.avoidLearnByRepeating:
			self.mindInstance.avoidLearnByRepeating = True

		answer = ""
		answer = self.mindInstance.listenToInput(question)
		
		if self.isSpeaking:
			self.speak(answer)
		
		if self.proactiveMode:
			self.setTimer()
			self.autoSpeakInput = answer
			self.timer.start()

		elapsed_time = time.time() - self.saveTracking
		if elapsed_time > 10:
			print "soft save"
			self.forceSaveMindFile()
			self.saveTracking = time.time()
		
		return answer	



	def setTimer(self):
		pass

	def monoLog(self):
		pass

	def speak(self, a):
		pass

	def stopEngine(self):
		if self.mindInstance:
			self.mindInstance.prepareCurrentMemoryForDisc()
		if self.isContributable:
			self.mindInstance.contributeClientMind()

	def forceUpload(self):
		pass

	def forceSaveMindFile(self):
		if self.mindInstance:
			self.mindInstance.prepareCurrentMemoryForDisc()

	def countMindMemory(self):
		if self.mindInstance:
			print "My memory contains " + str(len(self.mindInstance.botsMemory)) + " neurons"
		return str(len(self.mindInstance.botsMemory))

	def setTimer(self):
		self.timer = Timer(20 * 60, autoAnswer)



	def autoAnswer(self):
		try:
			t = self.mindInstance.listenToInput(self.autoSpeakInput)
			print "You (away):\t" 
			print "Prelude (bored):\t" + t
		except:
			print "Error: autoAnswer"

	def setAssociationAlgorithm(self, assocType):
		# if self.mindInstance:
		# 	self.mindInstance.associater = assocType
		self.associater = assocType

	def setMindFileLocation(self, fileName):
		self.brainLocation = fileName

	def getVersionInfo(self):
		return "Prelude@# Engine, version 1.2.7, 2004-2015(c) by Lennart Lopin ";

	def setBotClient(self):
		pass

	def setProactiveMode(self, flag):
		pass
示例#6
0
	def initializeEngine(self):
		self.mindInstance = Mind()
		self.mindInstance.associater = self.associater
		self.mindInstance.brainLocation = self.brainLocation
		self.chatInitiated = time.strftime("%H:%M:%S")
		self.mindInstance.analyzeShortTermMemory()
示例#7
0
class Prelude(object):
    def __init__(self):
        self.timer = None
        self.isSpeaking = False
        self.proactiveMode = False
        self.brainLocation = "mind.mdu"
        self.quantumRandomness = False
        self.avoidLearnByRepeating = False
        self.associater = MatchingAlgorithm.CosineTFIDF
        self.saveTracking = time.time()

    def initializeEngine(self):
        self.mindInstance = Mind()
        self.mindInstance.associater = self.associater
        self.mindInstance.brainLocation = self.brainLocation
        self.chatInitiated = time.strftime("%H:%M:%S")
        self.mindInstance.analyzeShortTermMemory()

    def chatWithPrelude(self, question):
        if self.mindInstance is None:
            return "Error: Mind not initialized"
        if self.proactiveMode:
            self.idleTime = 0
            if self.timer is not None:
                timer.Stop()

        if self.quantumRandomness:
            self.mindInstance.quantumRandomness = True

        if self.avoidLearnByRepeating:
            self.mindInstance.avoidLearnByRepeating = True

        answer = ""
        answer = self.mindInstance.listenToInput(question)

        if self.isSpeaking:
            self.speak(answer)

        if self.proactiveMode:
            self.setTimer()
            self.autoSpeakInput = answer
            self.timer.start()

        elapsed_time = time.time() - self.saveTracking
        if elapsed_time > 10:
            print "soft save"
            self.forceSaveMindFile()
            self.saveTracking = time.time()

        return answer

    def setTimer(self):
        pass

    def monoLog(self):
        pass

    def speak(self, a):
        pass

    def stopEngine(self):
        if self.mindInstance:
            self.mindInstance.prepareCurrentMemoryForDisc()
        if self.isContributable:
            self.mindInstance.contributeClientMind()

    def forceUpload(self):
        pass

    def forceSaveMindFile(self):
        if self.mindInstance:
            self.mindInstance.prepareCurrentMemoryForDisc()

    def countMindMemory(self):
        if self.mindInstance:
            print "My memory contains " + str(len(
                self.mindInstance.botsMemory)) + " neurons"

    def setTimer(self):
        self.timer = Timer(20 * 60, autoAnswer)

    def autoAnswer(self):
        try:
            t = self.mindInstance.listenToInput(self.autoSpeakInput)
            print "You (away):\t"
            print "Prelude (bored):\t" + t
        except:
            print "Error: autoAnswer"

    def setAssociationAlgorithm(self, assocType):
        # if self.mindInstance:
        # 	self.mindInstance.associater = assocType
        self.associater = assocType

    def setMindFileLocation(self, fileName):
        self.brainLocation = fileName

    def getVersionInfo(self):
        return "Prelude@# Engine, version 1.2.7, 2004-2015(c) by Lennart Lopin "

    def setBotClient(self):
        pass

    def setProactiveMode(self, flag):
        pass
示例#8
0
 def initializeEngine(self):
     self.mindInstance = Mind()
     self.mindInstance.associater = self.associater
     self.mindInstance.brainLocation = self.brainLocation
     self.chatInitiated = time.strftime("%H:%M:%S")
     self.mindInstance.analyzeShortTermMemory()
示例#9
0
class Character( Actor ):
  """
  """
  #===============================================================================================
  def __init__(self,name,imagefile,context,skills=[],factionid=0):
    """
    """
    model = [ (0,0), (0,181), (100,181), (100,0) ]
    bounding_rect = [ (0,0), (0,181), (100,181), (100,0) ]
    #super( Character, self ).__init__( model, imagefile, offset=(0,50) )
    _model = Vertel()
    _model.set( [ ( 0.0, 0.0, 0.0 ), 
                    ( 0.0, 171.0/600.0, 0.0 ), 
                    ( 101.0/800.0, 171.0/600.0,0.0 ),
                    ( 101.0/800.0 , 0.0, 0.0 ) ] )
    _bound = Vertel()
    _bound.set( [ ( 0.0, 0.0, 0.0 ),
                    ( 0.0, 100.0/600, 0.0 ),
                    ( 100.0/800.0, 100.0/600, 0.0 ),
                    ( 100.0/800, 0.0, 0.0 ) ] )
    super( Character, self ).__init__( imagefile, _model, _bound )
    #self.name = kwargs.get('name',"Unnamed")
    self.name = name
    self.mind = Mind( ego=self, factionid=factionid ) 

    self._controller = None

    self._context = context

    self._target = None

    #body stats
    self.strength  =  10  #strength of character, in kg*m*s^-2
    self.agility   =  20  #agility of character,  in kg*m*s^-2
    self.endurance =  10  #endurance: MAX Energy Reserves, in 
                          #endurance 
                          #endurance: MAX energy output in watts in kg*m^2*s^-3
    self._reach = 1.0     #arm length
    self._mass   = 50   #mass of character, in kg
    self.health = 100  #

    self.energy = Energy()

    self._alive  = True #alive bit, used to drop a character object references

    #skills
    self.skillbook  = SkillBook(skills=skills) 
    self.deck       = Deck(maxsize=4, slots=['default']*6 )

    #equipment
    self._equipment = Equipment()     #equipment is currently held by the character
    self.inventory = Inventory(40.0) #items in storage
    self.weapon = None

    #modifiers
    self.buff = Buffable(self) #allows the character to be buffed (stat and status effects)
    self.status_effects = []
    self._wounds = []
  #===============================================================================================
  # Helper functions which link related members of a Character object, such as the skll book and
  # skill deck
  #===============================================================================================
  def setslot(self,**kwargs):
    """
      helper function, sets deck slot type
    """
    self.deck.setslot(**kwargs)
  def setskill(self,**kwargs):
    """
      helper function, sets deck slot with skill from skillbook
    """
    self.deck.setskill(slot=kwargs['slot'],skill=self.skillbook[kwargs['skill']])
  def queueskill(self,**kwargs):
    """
      helper function, queues a skill as the next action from the deck
      
    """
    print "Character queue skill"
    try:
      skill = self.deck[kwargs['slot']]
      if skill:
        self.mind.appendaction( Action( actor  = self,
                                        atype  = skill.type,
                                        target = self.mind._target,
                                        skill  = skill,
                                        stage  = self._context,
                                        energy = 3 ) )
        
      else:
        print "No Skill set for deck slot:", kwargs['slot']
    except KeyError:
      print "KeyError in Character:",self.name," queueskill"
      print "  Either 'slot':'val' not present in arguments, or 'slot' not in deck"
      print "  Keys in deck", self.deck.keys()
      print "  kwargs:", kwargs
  #===============================================================================================
  def reach(self):
    """
      return the characters effective reach, 
      taking into account
       - base reach
       - weapon reach
    """
    return ( self._reach + self._equipment["rhand"]._reach )
  #===============================================================================================
  def __str__(self):
    """
    """
    return 'Character: %(name)s' %{'name':self.name}
  #===============================================================================================
  def recharge(self,step=1):
    """
      recharge is the turn update function, called recharge to avoid ambiguity with the animated
      actor's update function. Recharge is called at the beginning of a character's turn.
    """
    self.energy.rechargehand() #draw a new hand of energy from the pool
    #todo decrement cooldown on abilities by 1
  #===============================================================================================
  def use(self, energy, attempt=False):
    """
      remove the specified amount of energy from this characters' hand
    """
    return self.energy.subhand(energy,attempt)
  #===============================================================================================
  def hand(self):
    """return the current energy left in this characters' hand"""
    return self.energy._hand
  #===============================================================================================
  def pool(self):
    """return the current energy left in this characters' pool"""
    return self._energy_pool
  #===============================================================================================
  def iscapable(self, action):
    """
      return True if this character is capable of performing the action, False otherwise

      whether a character is capable of performing a given action or not is dependent on the
      energy cost of the action, and any relevant status effects on the character
    """
    if self.energy._hand >= action.getenergy():
      return True
  #===============================================================================================
  def update(self, step=1):
    """
    """
    #call super class update
    self.recharge(step)
    self.health -= len(self._wounds)*25
    #mind.plan
    return self.health
  #===============================================================================================
  def settarget(self, item ):
    """
    """
    self._target = item
    self.mind._target = item
  def gettarget(self):
    """
    """
    return self._target
  #===============================================================================================
  def listen(self):
    """
      TODO: Make character an event listener
      TODO: this should be an event handler
            currently just updates wound and energy
    """
    #call super class update
    self.recharge(1)
    self.health -= len(self._wounds)*25
    #mind.plan
    if not self.mind.targetalive(): self.mind._target = None
    return self.health
示例#10
0
class A3C:
    class OptimizerThread(threading.Thread):
        stop_signal = False

        def __init__(self, mind):
            self.mind = mind
            threading.Thread.__init__(self)

        def run(self):
            while not self.stop_signal:
                self.mind.train()

        def stop(self):
            self.stop_signal = True

    class AgentThread(threading.Thread):
        stop_signal = False

        def __init__(self, agent, resultHistory):
            threading.Thread.__init__(self)
            self.agent = agent
            self.history = resultHistory

        def run(self):
            while not self.stop_signal:
                result = self.agent.playSingleEpisode()
                if result != None:
                    self.history.registerResult(result)

        def stop(self):
            self.stop_signal = True

    def __init__(self, envName, numThreads=3):
        self.lock_queue = threading.Lock()
        self.enviromentName = envName
        self.env = Enviroment(envName)
        self.mind = Mind(self.env.envDim(),
                         self.env.actDim(),
                         policyOptimizer=Adam(lr=0.001),
                         criticOptimizer=Adam(lr=0.001))
        self.optimizer = A3C.OptimizerThread(self.mind)
        self.numThreads = numThreads
        self.results = History()

        commons.tensorFlowGraph = K.get_session().graph

    def train(self, stopFunction, remapRewardFunction=None, epsilon=1.0):
        self.optimizer.start()
        agents = list()
        for idEnv in range(self.numThreads):
            agent = Agent(Enviroment(self.enviromentName, seed=idEnv),
                          self.mind,
                          rewardMapper=remapRewardFunction,
                          epsilon=epsilon,
                          name="Agent-" + str(idEnv))
            agThread = A3C.AgentThread(agent, self.results)
            agThread.start()
            agents.append(agThread)
        done = False
        while not done:
            time.sleep(1)
            done = stopFunction(list(self.results.results))
        self.optimizer.stop()
        for agent in agents:
            agent.stop()
        self.mind.save(self.enviromentName)

    def play(self):
        self.mind.trained = True
        self.mind.load_weights(self.enviromentName)
        agent = Agent(Enviroment(self.enviromentName), self.mind, epsilon=0.0)
        return agent.playSingleEpisode(render=True)
示例#11
0
 def __init__(self):
     self.mind = Mind(self)
     self.init_agent()
示例#12
0
    def __init__(self, size, p_hunter, p_prey,
            prey_reward = 1, stuck_penalty = 1,
            death_penalty = 1, p_resurrection = 0.2,
            agent_max_age = 10, agent_range = 2, num_actions = 5,
            same = True, lock = None, name=None,
            max_iteration = 5000, boundary=False):
        (H, W) = self.size = size
        input_size = (2*agent_range + 1) ** 2
        self.boundary_exists = boundary
        if lock:
            self.lock = lock
        else:
            self.lock = Lock()

        self.A_mind = Mind(input_size, num_actions, self.lock, Queue())
        self.B_mind = Mind(input_size, num_actions, self.lock, Queue())

        self.max_iteration = max_iteration
        self.lock = lock
        if same:
            weights = self.A_mind.network.state_dict()
            self.B_mind.network.load_state_dict(weights)


        self.p_prey = p_prey
        self.p_resurrection = p_resurrection
        assert 1 - 2*p_hunter - p_prey > 0, 'Free space probability is less than one'
        self.probs = np.array([p_hunter, 1 - 2*p_hunter - p_prey, p_hunter, p_prey])

        self.prey_reward = prey_reward
        self.stuck_penalty = stuck_penalty
        self.death_penalty = death_penalty
        self.agent_max_age = agent_max_age
        self.hzn = agent_range
        self.vals = [-1, 0, 1, 2]
        self.names_to_vals = {"void": -2, "A": -1, "free": 0, "B": 1, "prey": 2}
        self.vals_to_names = {v: k for k, v in self.names_to_vals.items()}
        self.vals_to_index = {-1: 0, 0: 1, 1: 2, 2: 3}

        self.num_grids = size[0] * size[1]

        self.id_to_type = {}
        self.id_to_lives = {}

        self.crystal = np.zeros((max_iteration, H, W, 3)) # type, age, id
        self.history = []
        self.id_track = []
        self.records = []

        self.args = [self.prey_reward,
                self.stuck_penalty,
                self.death_penalty,
                self.agent_max_age,
                self.hzn]
        if name:
            self.name = name
        else:
            self.name = abs(hash(tuple([self] + self.args)))

        if not os.path.isdir(str(self.name)):
            os.mkdir(str(self.name))
            os.mkdir(str(self.name)+'/episodes')
            self.map, self.agents, self.loc_to_agent, self.id_to_agent = self._generate_map()
            self._set_initial_states()
            self.mask = self._get_mask()
            self.crystal = np.zeros((max_iteration, H, W, 4)) # type, age, tr,  id
            self.iteration = 0
        else:
            assert False, "There exists an experiment with this name."
示例#13
0
class Environment:
    def __init__(self, size, p_hunter, p_prey,
            prey_reward = 1, stuck_penalty = 1,
            death_penalty = 1, p_resurrection = 0.2,
            agent_max_age = 10, agent_range = 2, num_actions = 5,
            same = True, lock = None, name=None,
            max_iteration = 5000, boundary=False):
        (H, W) = self.size = size
        input_size = (2*agent_range + 1) ** 2
        self.boundary_exists = boundary
        if lock:
            self.lock = lock
        else:
            self.lock = Lock()

        self.A_mind = Mind(input_size, num_actions, self.lock, Queue())
        self.B_mind = Mind(input_size, num_actions, self.lock, Queue())

        self.max_iteration = max_iteration
        self.lock = lock
        if same:
            weights = self.A_mind.network.state_dict()
            self.B_mind.network.load_state_dict(weights)


        self.p_prey = p_prey
        self.p_resurrection = p_resurrection
        assert 1 - 2*p_hunter - p_prey > 0, 'Free space probability is less than one'
        self.probs = np.array([p_hunter, 1 - 2*p_hunter - p_prey, p_hunter, p_prey])

        self.prey_reward = prey_reward
        self.stuck_penalty = stuck_penalty
        self.death_penalty = death_penalty
        self.agent_max_age = agent_max_age
        self.hzn = agent_range
        self.vals = [-1, 0, 1, 2]
        self.names_to_vals = {"void": -2, "A": -1, "free": 0, "B": 1, "prey": 2}
        self.vals_to_names = {v: k for k, v in self.names_to_vals.items()}
        self.vals_to_index = {-1: 0, 0: 1, 1: 2, 2: 3}

        self.num_grids = size[0] * size[1]

        self.id_to_type = {}
        self.id_to_lives = {}

        self.crystal = np.zeros((max_iteration, H, W, 3)) # type, age, id
        self.history = []
        self.id_track = []
        self.records = []

        self.args = [self.prey_reward,
                self.stuck_penalty,
                self.death_penalty,
                self.agent_max_age,
                self.hzn]
        if name:
            self.name = name
        else:
            self.name = abs(hash(tuple([self] + self.args)))

        if not os.path.isdir(str(self.name)):
            os.mkdir(str(self.name))
            os.mkdir(str(self.name)+'/episodes')
            self.map, self.agents, self.loc_to_agent, self.id_to_agent = self._generate_map()
            self._set_initial_states()
            self.mask = self._get_mask()
            self.crystal = np.zeros((max_iteration, H, W, 4)) # type, age, tr,  id
            self.iteration = 0
        else:
            assert False, "There exists an experiment with this name."

    def configure(self, prey, penalty, age):
        self.prey_reward = prey
        self.stuck_penalty = penalty
        self.agent_max_age = age

    def get_agents(self):
        return self.agents

    def get_map(self):
        return self.map.copy()

    def move(self, agent):
        (i, j) = loc = agent.get_loc()
        (i_n, j_n) = to = agent.get_decision()
        self.map[i, j] = 0
        self.map[i_n, j_n] = agent.get_type()
        agent.set_loc(to)
        self.loc_to_agent[to] = agent
        del self.loc_to_agent[loc]

    def step(self, agent, by):
        if agent.is_alive() and agent.get_time_remaining() == 0:
            rew = self.kill(agent)
        elif agent.is_alive():
            (i, j) = agent.get_loc() # current location
            assert self.loc_to_agent[(i, j)]
            (di, dj) = by
            (i_n, j_n) = self._add((i, j), (di, dj)) #new location
            agent.set_decision((i_n, j_n))
            if self.map[i_n, j_n] == self.names_to_vals["free"]:
                rew = self.on_free(agent)
                assert rew != None
            elif self.map[i_n, j_n] == self.names_to_vals["prey"]:
                prey = self.loc_to_agent[(i_n, j_n)]
                if agent.get_type() in [-1, 1]:
                    rew = self.on_prey(agent, prey)
                else:
                    rew = self.on_same(agent, prey)
                assert rew != None
            elif self.map[i_n, j_n] * agent.get_type() == -1:
                opponent = self.loc_to_agent[(i_n, j_n)]
                rew = self.on_opponent(agent, opponent)
                assert rew != None
            elif di == 0 and dj == 0:
                rew = self.on_still(agent)
            else:
                other_agent = self.loc_to_agent[(i_n, j_n)]
                rew = self.on_same(agent, other_agent)
                assert rew != None
            done = False
            self.update_agent(agent, rew, done)
            agent.clear_decision()
        else:
            resurrected = self.resurrect(agent)
            rew = 0
            if resurrected and agent.get_type() in [-1, 1]:
                assert agent.get_time_remaining() == self.agent_max_age, agent.get_time_remaining()
                assert agent.get_loc() != None, "wtf?"
        return rew

    def on_free(self, agent):
        raise NotImplementedError('Please implement what to'\
         'do when agent steps on a free grid.')

    def on_still(self, agent):
        raise NotImplementedError('Please implement what to'\
         'do when agent stands still.')

    def on_prey(self, agent, prey):
        raise NotImplementedError('Please implement what to'\
         'do when agent encounters a prey.')

    def on_obstacle(self, agent):
        raise NotImplementedError('Please implement what to'\
         'do when agent encounters an obstacle.')

    def on_same(self, agent, other):
        raise NotImplementedError('Please implement what to'\
         'do when agent encounters its same kind.')

    def on_opponent(self, agent, opponent):
        raise NotImplementedError('Please implement what to'\
         'do when agent encounters a different kind.')

    def kill(self, victim, killer=None):
        raise NotImplementedError('Please implement what to'\
         'do when victim dies (with or wo killer).')


    def resurrect(self, agent):
        resurrected = False
        if np.random.random() < self.p_resurrection:
            locs = [(a, b)  for a in range(len(self.map))
                for b in range(len(self.map[0]))
                if self.map[a,b] == 0]
            idx = np.random.choice(range(len(locs)))
            (i, j) = loc = locs[idx]
            self.map[i, j] = agent.get_type()
            self.loc_to_agent[loc] = agent
            agent.respawn(loc)
            agent.set_current_state(self.get_agent_state(agent))
            resurrected = True
        return resurrected

    def update_agent(self, agent, rew, done):
        state = self.get_agent_state(agent)
        agent.set_next_state(state)
        name = self.vals_to_names[agent.get_type()]
        agent.update(rew, done)
        return rew

    def update(self):
        self.iteration += 1
        self.history.append(self.map.copy())

        self.A_mind.train(self.names_to_vals["A"])
        self.B_mind.train(self.names_to_vals["B"])
        a_ages = []
        a_ids = []
        b_ages = []
        b_ids = []
        id_track = np.zeros(self.map.shape)
        self.deads = []
        for agent in self.agents:
            typ = agent.get_type()
            age = agent.get_age()
            idx = agent.get_id()

            if agent.is_alive():
                i, j = agent.get_loc()
                tr = agent.get_time_remaining()
                id_track[i, j] = idx
                self.crystal[self.iteration - 1, i, j] = [typ, age, tr, idx]
            else:
                self.deads.append([agent.get_type(), agent.get_id()])

            type = agent.get_type()
            if type == self.names_to_vals["A"]:
                a_ages.append(str(age))
                a_ids.append(str(idx))
            else:
                b_ages.append(str(age))
                b_ids.append(str(idx))

        self.id_track.append(id_track)
        a_ages = " ".join(a_ages)
        b_ages = " ".join(b_ages)

        a_ids = " ".join(a_ids)
        b_ids = " ".join(b_ids)

        with open("%s/episodes/a_age.csv" % self.name, "a") as f:
            f.write("%s, %s, %s\n" % (self.iteration, a_ages, a_ids))

        with open("%s/episodes/b_age.csv" % self.name, "a") as f:
            f.write("%s, %s, %s\n" % (self.iteration, b_ages, b_ids))

        if self.iteration == self.max_iteration - 1:
            A_losses = self.A_mind.get_losses()
            B_losses = self.B_mind.get_losses()
            np.save("%s/episodes/a_loss.npy" % self.name, np.array(A_losses))
            np.save("%s/episodes/b_loss.npy" % self.name, np.array(B_losses))

    def shuffle(self):
        map = np.zeros(self.size)
        loc_to_agent = {}

        locs = [(i, j) for i in range(self.map.shape[0]) for j in range(self.map.shape[1]) if self.map[i, j] == 0]
        random.shuffle(locs)
        id_track = np.zeros(self.map.shape)
        for i, agent in enumerate(self.agents):
            loc = locs[i]
            agent.respawn(loc)
            loc_to_agent[loc] = agent
            map[loc] = agent.get_type()
            id_track[loc] = agent.get_id()


        self.map, self.loc_to_agent = map, loc_to_agent
        self._set_initial_states()
        self.history = [map.copy()]
        self.id_track = [id_track]
        self.records = []
        self.iteration = 0

    def record(self, rews):
        self.records.append(rews)

    def save(self, episode):
        f = gzip.GzipFile('%s/crystal.npy.gz' % self.name, "w")
        np.save(f, self.crystal)
        f.close()

    def save_agents(self):
        self.lock.acquire()
        pickle.dump(self.agents, open("agents/agent_%s.p" % (self.name), "wb" ))
        self.lock.release()

    def get_agent_state(self, agent):
        hzn = self.hzn
        i, j = agent.get_loc()
        fov = np.zeros((2 * hzn + 1, 2 *  hzn + 1)) - 2
        if self.boundary_exists:
            start_i, end_i, start_j, end_j = 0, 2 * hzn + 1, 0, 2 * hzn + 1
            if i < hzn:
                start_i = hzn - i
            elif i + hzn - self.size[0] + 1 > 0:
                end_i = (2 * hzn + 1) - (i + hzn - self.size[0] + 1)
            if j < hzn:
                start_j = hzn - j
            elif j + hzn - self.size[1] + 1 > 0:
                end_j = (2 * hzn + 1) - (j + hzn - self.size[1] + 1)
            i_upper = min(i + hzn + 1, self.size[0])
            i_lower = max(i - hzn, 0)

            j_upper = min(j + hzn + 1, self.size[1])
            j_lower = max(j - hzn, 0)

            fov[start_i: end_i, start_j: end_j] = self.map[i_lower: i_upper, j_lower: j_upper].copy()
        else:
            for di in range(-hzn, hzn+1):
                for dj in range(-hzn, hzn+1):
                    fov[hzn + di, hzn + dj] = self.map[(i+di) % self.size[0], (j+dj) % self.size[1]]

        fov[hzn, hzn] = agent.get_type()
        return fov

    def _to_csv(self, episode):
        with open("episodes/%s_%s.csv" % (episode, self.name), 'w') as f:
            f.write(', '.join(self.records[0].keys()) + '\n')
            proto = ", ".join(['%.3f' for _ in range(len(self.records[0]))]) + '\n'
            for rec in self.records:
                f.write(proto % tuple(rec.values()))

    def _add(self, fr, by):
        i, j = fr
        di, dj = by
        if self.boundary_exists:
            i_n = min(max(i + di, 0), self.size[0] - 1)
            j_n = min(max(j + dj, 0), self.size[1] - 1)
        else:
            i_n = (i + di) % self.size[0]
            j_n = (j + dj) % self.size[1]
        return (i_n, j_n)

    def _get_mask(self):
        mask = []
        for i, row in enumerate(self.map):
            foo = []
            for j, col in enumerate(row):
                foo.append((-1) ** (i + j))
            mask.append(foo)
        return np.array(mask)

    def _generate_map(self):
        map = np.zeros(self.size)
        loc_to_agent = {}
        id_to_agent = {}
        agents = []
        idx = 0
        for i, row in enumerate(map):
            for j, col in enumerate(row):
                val = np.random.choice(self.vals, p=self.probs)
                if not val == self.names_to_vals["free"]:
                    if val == self.names_to_vals["A"]:
                        mind = self.A_mind
                    elif val == self.names_to_vals["B"]:
                        mind = self.B_mind
                    else:
                        assert False, 'Error'
                    agent = Agent(idx, (i, j), val, mind,  self.probs[1], self.agent_max_age)
                    loc_to_agent[(i, j)] = agent
                    id_to_agent[idx] = agent
                    agents.append(agent)
                    idx += 1
                map[i, j] = val
        return map, agents, loc_to_agent, id_to_agent

    def predefined_initialization(self, file):
        with open(file) as f:
            for i, line in enumerate(f):
                if not i:
                    keys = [key.strip() for key in line.rstrip().split(',')]
                line.rstrip().split(',')

    def _set_initial_states(self):
        for agent in self.agents:
            state = self.get_agent_state(agent)
            agent.set_current_state(state)

    def _count(self, arr):
        cnt = np.zeros(len(self.vals))
        arr = arr.reshape(-1)
        for elem in arr:
            if elem in self.vals_to_index:
                cnt[self.vals_to_index[elem]] += 1
        return cnt
示例#14
0
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from nltk.corpus import stopwords
import numpy as np
import numpy.linalg as LA
from mind import Mind

m = Mind()
m.analyzeShortTermMemory()


#train_set = ["The sky is blue.", "The sun is bright."] #Documents
train_set = list(m.botsMemory.keys())
test_set = ["hello how are you"] #Query

vectorizer = CountVectorizer(stop_words = None)
#print vectorizer
transformer = TfidfTransformer()
#print transformer

trainVectorizerArray = vectorizer.fit_transform(train_set).toarray()
testVectorizerArray = vectorizer.transform(test_set).toarray()
print 'Fit Vectorizer to train set', trainVectorizerArray
print 'Transform Vectorizer to test set', testVectorizerArray
cx = lambda a, b : round(np.inner(a, b)/(LA.norm(a)*LA.norm(b)), 6)

for vector in trainVectorizerArray:
    #print vector
    for testV in testVectorizerArray:
        #print testV
        cosine = cx(vector, testV)
示例#15
0
import torch

data = get_data('../data/samples.txt')
mid, seq, avid, label, lens = collect_data(batch_data=data)
dataset = MindDataset(mid, seq, avid, label, lens)

dataloader = DataLoader(dataset, batch_size=32)
dim = 8
max_len = 20
input_units = 20
output_units = 20
iteration = 1
max_K = 3
p = 2

model = Mind(dim, max_len, input_units, output_units, iteration, max_K, p)

optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
model.train()

train_iteration = 3

for i in range(train_iteration):
    losses = []
    for data in dataloader:
        mid, seq, label_avid, label, lens = data
        output = model(mid, seq, label_avid, lens)
        output = output.squeeze(dim=-1).float()
        label = label.float()
        loss = F.binary_cross_entropy_with_logits(output, label)
        losses.append(loss.data.item())
示例#16
0
def main():
    from mind import Mind
    minecraft_mind = Mind()
    minecraft_mind.learning_loop()