コード例 #1
0
ファイル: Game.py プロジェクト: gpanneau/PBIM
 def New_Generation(
     self,
     Methode=0,
     Indiv=50,
     Mute=10
 ):  #crée une nouvelle génération d'individus à partir d'une ancienne population triée par fitness en fonction de la methode choisie
     if Methode == 0:
         Father = self.Pop[0]
         self.Pop = []
         self.AddAgent(Father)
         for i in range(0, Indiv):
             G = Genome.Genome(25, 3)
             G.Set_Map(self.Pop[0].Genome_.Map_[:, :])
             A = Agent.Agent(self.Pop[0].posX_, self.Pop[0].posY_, G,
                             self.Grid)
             A.Mutate(Mute, 0.95)
             self.AddAgent(A)
     if Methode == 1:
         j = 0
         PopBis = []
         for agent in self.Pop:  #chaque agent peut se reproduire dans la limite des places disponible. Les plus performent se reproduiront en premier
             if j < Indiv:
                 PopBis.append(agent)
                 j += 1
                 if j < Indiv:
                     G = Genome.Genome(25, 3)
                     G.Set_Map(agent.Genome_.Map_[:, :])
                     A = Agent.Agent(agent.posX_, agent.posY_, G, self.Grid)
                     A.Mutate(Mute, 0.95)
                     PopBis.append(A)
                     j += 1
             else:
                 break
         self.Pop = PopBis
コード例 #2
0
def createTheAgent(self, line, num, agType):
    # explicitly pass self, here we use a function
    if len(line.split()) == 5 and line.split()[1] != 'brown':
        anAgent = Agent(num,
                        self.worldState,
                        int(line.split()[2]),
                        int(line.split()[3]),
                        self.leftX,
                        self.rightX,
                        self.bottomY,
                        self.topY,
                        agType=agType)
        self.agentList.append(anAgent)
        anAgent.setColorAndGender(line.split()[1], line.split()[4])

    elif len(line.split()) == 4 and line.split()[1] == 'brown':
        anAgent = Agent(num,
                        self.worldState,
                        int(line.split()[2]),
                        int(line.split()[3]),
                        self.leftX,
                        self.rightX,
                        self.bottomY,
                        self.topY,
                        agType=agType)
        # not added to agentList
        anAgent.setColorAndGender(line.split()[1], "")  # brown, no gender,
        # it is a desk
    else:
        print("Error in file " + agType + ".txt")
        os.sys.exit(1)
コード例 #3
0
    def __init__(self, map, valDim=1):
        self.map = map
        self.x_num = map.x_num
        self.y_num = map.y_num

        self.defaultEdgeColor = QtGui.QColor(0, 0, 0)
        self.defaultEdgeWidth = 1

        self.defaultActiveCellColor = QtGui.QColor(0, 255, 255)

        self.defaultObstacleColor = QtGui.QColor(255, 165, 0)
        self.defaultHumanColor = QtGui.QColor(0, 204, 0)
        self.defaultRobotColor = QtGui.QColor(0, 0, 204)

        self.refStartHexIdx = None
        self.refEndHexIdx = None
        self.refStartColor = QtGui.QColor(102, 102, 255)
        self.refEndColor = QtGui.QColor(153, 153, 0)

        self.activeCells = []
        self.currentHexValDim = 0
        self.hexValDim = valDim
        self.hexVals = []
        for d in range(self.hexValDim):
            hexVal = np.ones((self.x_num, self.y_num))
            self.hexVals.append(hexVal)

        self.accessible = np.ones((self.x_num, self.y_num))

        self.humanPath = []
        self.robotPath = []
        self.human = Agent()
        self.robot = Agent()
コード例 #4
0
 def __init__(self):
     self.max_length = 100
     self.towards_ref = None
     self.mean_ref = None
     self.away_ref = None
     self.me = Agent(200, 200)
     self.agents = [
         Agent(random.randint(10, 990), random.randint(10, 590))
         for i in range(50)
     ]
     self.me.velocity = Vector2(0.1, 0)
コード例 #5
0
    def populate(self):
        '''
        Initiate random population placement onto spatial grid
        :param agents: List of agents in simulation
        :return:
        '''

        # To keep track of coalitions

        # Criminals are in their own coalition at first
        for coalition_id in range(self.config['num_criminals']):
            # Create a coalition
            new_coalition = Coalition_Crime(uid=coalition_id)
            new_agent = Agent(
                of_type=0,
                uid=coalition_id,
                network=new_coalition,
                crime_propensity=random.uniform(
                    0, self.config['crime_propensity_init_max']),
                location=[
                    random.uniform(0, self.config['grid_width']),
                    random.uniform(0, self.config['grid_height'])
                ],
                resources=random.uniform(
                    0, self.config['resouces_init_max_for_criminals']))
            self.coalitions.append(new_coalition)

        # Populate Civilians
        for civilian_id in range(self.config['num_criminals'],
                                 self.config['num_civilians']):
            self.civilians.append(
                Agent(
                    of_type=1,
                    uid=civilian_id,
                    location=list(
                        random.uniform(0, self.config['grid_width']),
                        random.uniform(0, self.config['grid_height'])),
                    resources=list(
                        random.uniform(
                            0,
                            self.config['resources_init_max_for_civilians']))))

        for police_id in range(
                len(self.coalitions) + len(self.civilians),
                self.config['num_police']):
            self.police.append(
                Agent(of_type=2,
                      uid=police_id,
                      location=[
                          random.uniform(0, self.config['grid_width']),
                          random.uniform(0, self.config['grid_height'])
                      ]))
コード例 #6
0
  def New_Generation(self,Methode=0,Indiv=50,Mute=10):
    """
    Create a new generation of individual from the descending sort population.
    Two methode are possible :
      - Methode 0 : only the best individual can have children, it is him genome who is mutate
      - Methode 1 : the 50% best individuals have children by mutation of them genome
    By default the choosen methode is the methode 0

    Parameters
    ----------
    self : class
      Object Game

    Methode : int
      The methode choose for the generation of indivual, can take the values 0 or 1, by default methode select is 0

    Indiv : int
      Number of individual at the new generation, by default 50

    Mute : int
      Number of mutation by genome desire, by default 10
    """    
    if Methode==0:
      Father=self.Pop[0]
      self.Pop=[]
      self.AddAgent(Father)
      for i in range(0,Indiv):
        G=Genome.Genome(25,3)
        G.Set_Map(self.Pop[0].Genome_.Map_[:,:])
        A=Agent.Agent(self.Pop[0].posX_,self.Pop[0].posY_,G,self.Grid)
        A.Mutate(Mute,0.95)
        self.AddAgent(A)
    if Methode==1:
      j=0
      PopBis=[]
      for agent in self.Pop:#chaque agent peut se reproduire dans la limite des places disponible. Les plus performent se reproduiront en premier
        if j<Indiv:
          PopBis.append(agent)
          j+=1
          if j<Indiv:
            G=Genome.Genome(25,3)
            G.Set_Map(agent.Genome_.Map_[:,:])
            A=Agent.Agent(agent.posX_,agent.posY_,G,self.Grid)
            A.Mutate(Mute,0.95)
            PopBis.append(A)
            j+=1
        else:
          break
      self.Pop=PopBis
コード例 #7
0
def main():
    env = GridWorld()
    agent = Agent()
    data = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]  # 테이블 초기화
    gamma = 1.0
    alpha = 0.001

    for k in range(50000):  # 총 5만 번의 에피소드 진행
        done = False
        history = []
        while not done:
            action = agent.select_action()
            (y, x), reward, done = env.step(action)
            history.append((y, x, reward))
        env.reset()

        # 매 에피소드가 끝나고 바로 해당 데이터를 이용해 테이블을 업데이트
        cum_reward = 0  # 리턴 G_t
        for transition in history[::-1]:
            # 방문했던 상태들을 뒤에서부터 보며 차례차례 리턴을 계산
            y, x, reward = transition
            data[y][x] = data[y][x] + alpha * (cum_reward - data[y][x])
            cum_reward = reward + gamma * cum_reward

    # 학습이 끝나고 난 후 데이터를 출력해보기 위한 코드
    for row in data:
        print(row)
コード例 #8
0
def agent_update():
    global agent_array
    temp_agent_array = []

    if freeze:
        return

    for i in xrange(0, len(agent_array)):
        agent = agent_array[i]
        temp_vel = (0, 0)
        cohesion_v = computeCohesion(agent, i % 2)
        alignment_v = computeAlignment(agent, i % 2)
        seperation_v = computeSeperation(agent, i % 2)
        obstacle_dodge_v = computeObscatleDodge(agent)

        v_array = [
            agent.vel,
            utils.v_mul(cohesion_v, COHESION_WEIGHT[i % 2]),
            utils.v_mul(alignment_v, ALIGNMENT_WEIGHT[i % 2]),
            utils.v_mul(seperation_v, SEPERATION_WEIGHT[i % 2]),
            utils.v_mul(obstacle_dodge_v, OBSTACLE_DOGDGE_WEIGHT)
        ]

        temp_vel = utils.v_array_sum(v_array)
        a = Agent(agent.pos, temp_vel)
        if i % 2:
            a.vel = utils.limit(temp_vel, DEFAULT_SPEED + 6 + speed_adjustment)
        else:
            a.vel = utils.limit(temp_vel, DEFAULT_SPEED + speed_adjustment)
        # utils.change_vel_if_zero(a)
        a.updatePos()
        temp_agent_array.append(a)

    agent_array = temp_agent_array
コード例 #9
0
def run():
    while True:
        for event in pyg.event.get():
            if event.type == pyg.QUIT:
                pyg.quit()
                sys.exit()
            elif pyg.key.get_pressed()[pyg.K_c]:
                clear_all_item()
            elif pyg.key.get_pressed()[pyg.K_r]:
                randomize_position()
            elif pyg.key.get_pressed()[pyg.K_f]:
                toggle_freeze_agent()
            elif pyg.key.get_pressed()[pyg.K_UP]:
                adjust_speed(1)
            elif pyg.key.get_pressed()[pyg.K_DOWN]:
                adjust_speed(0)
            elif pyg.mouse.get_pressed()[0]:
                agent_array.append(Agent(pyg.mouse.get_pos()))
            elif pyg.mouse.get_pressed()[2]:
                obstacle_array.append(Obstacle(pyg.mouse.get_pos()))

        screen.fill(BACKGROUND)
        draw_agent()
        draw_obstacle()
        draw_text()
        agent_update()
        pyg.display.update()
        clock.tick(FPS)
コード例 #10
0
 def __init__(self, input_stream, size_of_domain_set):
     self.input_stream = input_stream
     self.n = size_of_domain_set
     self.trail_zeros = 0
     self.flajolet_martin_fn = self.flajolet_martin(self.n)
     self.agent = Agent([self.input_stream], [], self.flajolet_martin_fn,
                        self.trail_zeros)
コード例 #11
0
   def __init__(self, parent):
      GGrule.__init__(self, 30)
      self.TimeDelay = ATOM3Integer(2)
      self.exactMatch = 1
      self.LHS = ASG_omacs(parent)

      self.obj2423=Agent(parent)
      self.obj2423.preAction( self.LHS.CREATE )
      self.obj2423.isGraphObjectVisual = True

      if(hasattr(self.obj2423, '_setHierarchicalLink')):
        self.obj2423._setHierarchicalLink(False)

      # price
      self.obj2423.price.setNone()

      # name
      self.obj2423.name.setValue('')
      self.obj2423.name.setNone()

      self.obj2423.GGLabel.setValue(1)
      self.obj2423.graphClass_= graph_Agent
      if parent.genGraphics:
         new_obj = graph_Agent(160.0,60.0,self.obj2423)
         new_obj.layConstraints = dict() # Graphical Layout Constraints 
         new_obj.layConstraints['scale'] = [1.0, 1.0]
      else: new_obj = None
      self.obj2423.graphObject_ = new_obj

      # Add node to the root: self.LHS
      self.LHS.addNode(self.obj2423)
      self.obj2423.postAction( self.LHS.CREATE )


      self.RHS = ASG_omacs(parent)
コード例 #12
0
def addAFactory(address):

    if random.random() < 0.5:
        # with a given probability, create a new factory cloning
        # an existing one, randomly chosen (can also be a clone)

        factoryTmpList = []
        # agentList contains also recipes
        for i in range(len(address.agentList)):
            if address.agentList[i].agType == 'factories':
                factoryTmpList.append(address.agentList[i])
        # print factoryTmpList
        random.shuffle(factoryTmpList)
        toBeCloned = factoryTmpList[0]
        # print toBeCloned.number

        # cloning (the agent constructor interacts with the graph)

        common.clonedN += 1
        anAgent = Agent(toBeCloned.number * 100 + common.clonedN,
                        address.worldState,
                        toBeCloned.xPos + modPosition(),
                        toBeCloned.yPos + modPosition(),
                        agType=toBeCloned.agType,
                        sector=toBeCloned.sector)
        address.agentList.append(anAgent)
        anAgent.setAgentList(address.agentList)
        if common.verbose:
            print("Created factory #", anAgent.number, "in sector",
                  anAgent.sector)
コード例 #13
0
ファイル: ListOperators.py プロジェクト: zatricion/Streams
def many_to_many(f,
                 in_streams,
                 num_out_streams,
                 state=None,
                 call_streams=None):
    def transition(in_lists, state=None):
        smallest_list_length = min(v.stop - v.start for v in in_lists)
        input_lists = [
            v.list[v.start:v.start + smallest_list_length] for v in in_lists
        ]
        # Carry out the state-transition operation which
        # (1) updates state and (2) computes the output list for
        # the single output stream of this agent.
        # func returns a list and has the side effect of modifying
        # state
        if state is None:
            output_lists = f(input_lists)
        else:
            output_lists, state = f(input_lists, state)
        in_lists_start_values = [
            v.start + smallest_list_length for v in in_lists
        ]
        return (output_lists, state, in_lists_start_values)

    # Create agent
    out_streams = [Stream() for v in range(num_out_streams)]
    Agent(in_streams, out_streams, transition, state, call_streams)

    return out_streams
コード例 #14
0
    def initial(self, Form):
        self.clear(Form, self.tableWidget)
        self.clear(Form, self.tableWidget_2)
        self.textEdit_5.setHtml(_translate("Form", '', None))
        self.textEdit_6.setHtml(_translate("Form", "0", None))

        player1 = self.textEdit.toPlainText()
        player2 = self.textEdit_2.toPlainText()

        agent = []
        for i in range(2):
            if i == 0: x = player1
            else: x = player2
            if x == 's1':
                a = Agent.StrategicAgent1(
                    unichr(65 + i) + " (Strate method 1) ")
            elif x == 's2':
                a = Agent.StrategicAgent2(
                    unichr(65 + i) + " (Strate method 2) ")
            elif x == 'mc':
                a = Agent.MonteCarloAgent(unichr(65 + i) + " (Monte Carlo) ")
            elif x == 'dp':
                a = Agent.DynamicProgrammingAgent(
                    unichr(65 + i) + " (Dynamic Programming) ")
            elif x == 'rd':
                a = Agent.Agent(unichr(65 + i) + " (Random) ")
            else:
                print "Illegal command!!"
            agent.append(a)

        global agentA
        global agentB

        agentA = agent[0]
        agentB = agent[1]
コード例 #15
0
def play(model_file_name, config):
    print('load pretrained model file: ' + model_file_name)

    agent = Agent(config)
    load_checkpoint(model_file_name, agent.model)
    bird_game = game.GameState()

    total_reward = 0.
    time_count = 0.

    # 1.init S
    action = [1, 0]  # do nothing
    state = init_state()
    obs, reward, terminal = bird_game.frame_step(action)
    obs = preprocess(obs)
    state = np.append(state[1:, :, :], obs.reshape((1, ) + obs.shape), axis=0)

    while not terminal:
        action = agent.optimal_action(state)

        next_obs, reward, terminal = bird_game.frame_step(action)
        next_obs = preprocess(next_obs)
        next_state = np.append(state[1:, :, :],
                               next_obs.reshape((1, ) + next_obs.shape),
                               axis=0)

        state = next_state

        total_reward += reward
        time_count += 1

    print('total time step is {}'.format(time_count))
コード例 #16
0
ファイル: mActions.py プロジェクト: vishalbelsare/SLAPP3
def createTheAgent(self, line, num, agType):
    # explictly pass self, here we use a function
    # added z axis for position in 3D

    # third axis dimension copied form y dimension
    bottomZ=self.bottomY
    topZ=self.topY
    if len(line.split()) == 1:  # weak control, can be improved
        anAgent = Agent(
            num,
            self.worldState,
            random.randint(
                self.leftX,
                self.rightX),
            random.randint(
                self.bottomY,
                self.topY),
            random.randint(
                bottomZ,
                topZ),
            self.leftX,
            self.rightX,
            self.bottomY,
            self.topY,
            bottomZ,
            topZ,
            agType=agType)
        self.agentList.append(anAgent)

    else:
        print("Error in file " + agType + ".txt")
        os.sys.exit(1)
コード例 #17
0
def list_agent(f, inputs, outputs, state, call_streams, window_size,
               step_size):
    # f: list of lists, state -> list of lists, state
    assert_is_list_of_streams_or_None(call_streams)
    num_outputs = len(outputs)

    def transition(in_lists, state):
        smallest_list_length = min(v.stop - v.start for v in in_lists)
        if smallest_list_length == 0:
            return ([[]] * num_outputs, state, [v.start for v in in_lists])
        input_lists = [
            v.list[v.start:v.start + smallest_list_length] for v in in_lists
        ]
        if state is None:
            output_lists = f(input_lists)
        else:
            output_lists, state = f(input_lists, state)

        ## if num_outputs:
        ##     assert_is_list_of_lists(output_lists, num_outputs)
        in_lists_start_values = [
            v.start + smallest_list_length for v in in_lists
        ]
        return (output_lists, state, in_lists_start_values)

    # Create agent
    return Agent(inputs, outputs, transition, state, call_streams)
コード例 #18
0
def main():
    conversation_id = str(uuid.uuid4())
    bot_id = botVersion()
    user_id = getpass.getuser()

    file_logger = IOAdapter.FileLogger("../data/output/log.jsonl")
    sql_logger = IOAdapter.SQLiteLogger("../data/output/log.db")
    io_adapter = sql_logger.makeIO(
        conversation_id, user_id, bot_id,
        file_logger.makeIO(conversation_id, user_id, bot_id,
                           IOAdapter.CommandLineIO()))

    agent = Agent(
        io_adapter,
        [
            TFIDFDiscussAction(
                ml.load_tfidf_model_from_image("../models/vectoriser_img.pk")),
            #EmbeddingDiscussAction(ml.load_embeddings_model_from_image("../data/qa_pairs_embedded.pk",
            #                                                           "https://storage.googleapis.com/tfhub-modules/google/universal-sentence-encoder/4.tar.gz")),
            # LdaDiscussAction("../models/lda_n20.pickle", "../models/tf_n20.pickle","../data/docs_preprocessed_topicDistribution_match_20.csv"),
            FarewellAction(),
            StaticQuestionAction(),
            GreetAction(),
            TopicSuggestionAction("../data/suggestions.json", 3),
            FallbackAction(),
            RecapAction(),
            RegexStaticQuestionAction()
        ])

    try:
        asyncio.run(agent.run())
    finally:
        sql_logger.close()
        file_logger.close()
コード例 #19
0
 def target_update_analysis(self):
     target_updates = [(4, 0.001), (30, 1), (8, 0.01), (15, 0.1)]
     target_updates_str = [
         "t" + str(a[0]) + "_tau" + str(a[1]) for a in target_updates
     ]
     filename = "results/target_update_tau.png"
     fig = plt.figure(figsize=(12, 8), tight_layout=True)
     for idx, target_update_tau in enumerate(target_updates):
         target_update, tau = target_update_tau
         agent = Agent(self.gamma, self.epsilon_start, self.epsilon_end,
                       self.epsilon_decay, self.alpha, target_update,
                       self.max_iter, tau, self.batch_size,
                       self.dropout_ratio)
         if agent.reward_exist():
             rewards = agent.load_rewards()
             print("load value for {}".format(agent.tag))
         else:
             rewards = agent.train()
         rewards = self.moving_average(rewards)
         epochs = [(i + 1) for i in range(len(rewards))]
         plt.plot(epochs, rewards, color=self.colors[idx], linestyle='-')
     plt.xlabel("Epochs")
     plt.ylabel("Rewards")
     plt.xlim(0, self.max_iter)
     plt.ylim(-1200, 300)
     plt.legend(target_updates_str, loc='best')
     fig.savefig(filename, dpi=fig.dpi)
     return
コード例 #20
0
    async def connect(websocket, path):
        conversation_id = str(uuid.uuid4())
        remote_ip, remote_port = websocket.remote_address
        user_id = remote_ip + ":" + str(remote_port)

        io_adapter = sql_logger.makeIO(
            conversation_id, user_id, bot_id,
            file_logger.makeIO(conversation_id, user_id, bot_id,
                               IOAdapter.WSIO(websocket)))

        agent = Agent(
            io_adapter,
            [
                TFIDFDiscussAction(
                    ml.load_tfidf_model_from_image(
                        "../models/vectoriser_img.pk")),
                # EmbeddingDiscussAction(ml.load_embeddings_model_from_image("../data/qa_pairs_embedded.pk",
                #                                                           "https://storage.googleapis.com/tfhub-modules/google/universal-sentence-encoder/4.tar.gz")),
                # LdaDiscussAction("../models/lda_n20.pickle", "../models/tf_n20.pickle","../data/docs_preprocessed_topicDistribution_match_20.csv"),
                FarewellAction(),
                StaticQuestionAction(),
                GreetAction(),
                TopicSuggestionAction("../data/suggestions.json", 3),
                FallbackAction(),
                RecapAction(),
                RegexStaticQuestionAction()
            ])
        try:
            await agent.run()
        except websockets.exceptions.ConnectionClosed:
            pass
コード例 #21
0
ファイル: Main.py プロジェクト: dtbinh/m2s3_sci_tp1_sma
 def createAgent(self, agentlist, x, y, name):
     agent = Agent(self.environment, x, y, name, data["torus"],
                   data["trace"])
     agentlist.append(agent)
     self.environment.setInCell(x, y, agent)
     if data["trace"]:
         agent.printTrace()
コード例 #22
0
ファイル: Game.py プロジェクト: gpanneau/PBIM
 def EvolveByDivision(self, IndivMax, MutationsRate, Generation=500):
     t = time.time()
     end = False
     for i in range(Generation):
         self.PopTest(
         )  #fait résoudre le circuit à l'ensemble de la population
         self.SortByFitness(
         )  # tris les individus par fitness dans l'ordre décroissant
         if (
                 self.Pop[0].posX_ == len(self.Grid[0, :]) - 3
         ):  # verifie quaucun individu n'a complètement résolut le circuit
             end = True
         j = 0
         PopBis = []
         for agent in self.Pop:  #chaque agent peut se reproduire dans la limite des places disponible. Les plus performent se reproduiront en premier
             if j < IndivMax:
                 agent.posX_ = 4
                 agent.posy_ = 2
                 #agent.Mutate(MutationsRate,1)
                 PopBis.append(agent)
                 j += 1
                 if j < IndivMax:
                     G = Genome.Genome(25, 3)
                     """Add an agent to the list Pop"""
                     G.Set_Map(agent.Genome_.Map_[:, :])
                     A = Agent.Agent(4, 2, G, self.Grid)
                     A.Mutate(MutationsRate, 1)
                     PopBis.append(A)
                     j += 1
         self.Pop = PopBis
         if end:
             return time.time() - t
     return time.time() - t
コード例 #23
0
def agent_login(failed_attempt, database):
    failed = failed_attempt
    validate = Agent(database)  #intilializes Agent object
    Valid_bool = False
    aid = input('Username: '******'Password(hidden):')
    Valid_bool = validate.validate_agent(
        aid, pwd)  #validates that aid and pwd are correct
    ###############################################################################################################################
    # remainder of lines notifies user that their aid or pwd are incorrect, then lets them know that if they dont have an account
    # they should get in contact with System Admin to creat account
    ###############################################################################################################################
    if Valid_bool == False:
        print('Username or Password was incorrect')
        print(
            'If you do not have an account, please see System Admin or Login as Customer'
        )
        failed += 1
        agent_login(failed, database)

    else:
        agent_session = Agent_Session(database)
        agent_session.start_session(aid)
        agent_session.close()
        os.system('clear')
        main()
コード例 #24
0
def train_sarsa(num_trials, num_episodes, lr, epsilon, gamma,
                fourier_basis_order, total_trials):
    undiscounted_returns = np.zeros(shape=(total_trials, num_episodes))

    environment = Environment.Environment()
    agent = Agent.Agent(fourier_basis_order=fourier_basis_order,
                        epsilon=epsilon)

    for trial in xrange(num_trials[0], num_trials[1]):
        print "Trial: ", trial
        agent.reset()
        for episode in xrange(num_episodes):
            environment.reset()
            current_state = environment.current_state
            current_action = agent.get_action(current_state)
            for time_step in xrange(Constants.episode_end_time_step):
                next_state, reward, done = environment.step(current_action)
                if done:
                    # print time_step
                    break
                next_action = agent.get_action(next_state)
                agent.sarsa_update(current_state, current_action, reward,
                                   next_state, next_action, lr, gamma)
                current_state = next_state
                current_action = next_action

            undiscounted_returns[trial][episode] += -1 * environment.time_step

    return undiscounted_returns
コード例 #25
0
def SignInAgent():
    print("Agent Sign-in Page")
    ''' The sign-in page for agents, ensures a valid aid is entered
        and if matching password is entered goes to customer page '''

    print("Please enter a valid aid:")
    aid = input().strip()
    AgentInfo = aidSearch(aid)
    while not AgentInfo or not validateLogInInfo(aid):
        print("The aid doesn't exist. Please enter a valid aid:")
        aid = input().strip()
        AgentInfo = aidSearch(aid)

    password = AgentInfo[0][2]
    #get the password and check for match
    PasswordInput = getpass.getpass("Please enter your password: "******"Incorrect Password. Please try again: ").strip()

    # authentication complete
    # calling the CustomerPage function with string cid as parameter

    print("Signed in as agent")
    Agent(DBpath)
コード例 #26
0
def train_q_learning(num_trials, num_episodes, lr, epsilon, gamma,
                     fourier_basis_order, total_trials):
    undiscounted_returns = np.zeros(shape=(total_trials, num_episodes))

    environment = Environment.Environment()
    # environment = gym.make("MountainCar-v0")
    agent = Agent.Agent(fourier_basis_order=fourier_basis_order,
                        epsilon=epsilon)
    for trial in xrange(num_trials[0], num_trials[1]):
        print "Trial: ", trial
        agent.reset()
        for episode in xrange(num_episodes):
            environment.reset()
            # current_state = [-0.5, 0]
            current_state = environment.current_state
            current_action = agent.get_action(current_state)
            while True:
                # environment.render()
                next_state, reward, done = environment.step(
                    Constants.action_representation[current_action])
                if done:
                    # print environment.time_step
                    break
                agent.q_learning_update(current_state, current_action, reward,
                                        next_state, lr, gamma)
                current_state = next_state
                current_action = agent.get_action(next_state)
            undiscounted_returns[trial][episode] = -1 * environment.time_step

    return undiscounted_returns
コード例 #27
0
 def __init__(self, name, thread_type, parameter_server, feed_dict):
     self.feed_dict = feed_dict
     self.frames= 0
     self.name = name
     self.thread_type = thread_type
     self.env = gym.make(self.feed_dict['ENV'])
     self.agent = Agent.Agent(name, parameter_server, feed_dict)    # 環境内で行動するagentを生成
コード例 #28
0
def main(exp_per, days, qdegree, graph_obj, error_CT, disease_paramters):

    beta_sy, beta_asy, mu_sy, mu_asy, gamma_sy, gamma_asy, delta = disease_paramters

    agents = []
    for i in range(graph_obj.size):
        state = 'Susceptible'
        if random.random() < exp_per:
            state = 'Exposed'
        agent = Agent.Agent(state, i)
        agents.append(agent)

    #create graph of agents from graph_obj
    for indx, agent in enumerate(agents):
        agent.index = indx
        for j in graph_obj.adj_list[indx]:
            agent.neighbours.append(agents[j])

    individual_types = [
        'Susceptible', 'Exposed', 'Asymptomatic', 'Symptomatic', 'Recovered'
    ]

    def p_infection(p1, p2):  # probability of infectiong neighbour
        def p_fn(my_agent, neighbour_agents):
            p_inf_symp = p1
            p_inf_asymp = p2
            p_not_inf = 1
            for nbr_agent in neighbour_agents:
                if nbr_agent.state == 'Symptomatic' and not nbr_agent.quarantined and not my_agent.quarantined:
                    p_not_inf *= (1 - p_inf_symp)
                if nbr_agent.state == 'Asymptomatic' and not nbr_agent.quarantined and not my_agent.quarantined:
                    p_not_inf *= (1 - p_inf_asymp)
            return 1 - p_not_inf

        return p_fn

    def p_standard(p):
        def p_fn(my_agent, neighbour_agents):
            return p

        return p_fn

    transmission_prob = {}
    for t in individual_types:
        transmission_prob[t] = {}

    for t1 in individual_types:
        for t2 in individual_types:
            transmission_prob[t1][t2] = p_standard(0)
    transmission_prob['Susceptible']['Exposed'] = p_infection(
        beta_sy, beta_asy)
    transmission_prob['Exposed']['Symptomatic'] = p_standard(mu_sy)
    transmission_prob['Exposed']['Asymptomatic'] = p_standard(mu_asy)
    transmission_prob['Symptomatic']['Recovered'] = p_standard(gamma_sy)
    transmission_prob['Asymptomatic']['Recovered'] = p_standard(gamma_asy)
    transmission_prob['Recovered']['Susceptible'] = p_standard(delta)

    sim_obj = Simulate.Simulate(graph_obj, agents, transmission_prob)
    sim_obj.simulate_days(days, qdegree, error_CT)
    return sim_obj.state_history, sim_obj.quarantine_history
コード例 #29
0
def hybrid_agent():
    kb = KB()
    ag = Agent()

    matrix = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

    print("Starting Location = ", ag.FindCurrentLocation())

    while (1):
        if (ag.FindCurrentLocation() == [4, 4]):
            print("Game won!!\n")
            break

        x, y = _FindIndicesForLocation(ag.FindCurrentLocation())

        if ([x, y] not in visited):
            visited.append([x, y])  #append cur loc

        matrix[x][y] = 1  #current loc is valid
        kb.tell(ag)  #perceive

        #updating safe
        for room in unvisited:
            val = 4 * room[0] + room[1] + 1
            if ((kb.ask(-val) == True) and room not in safe):
                safe.append(room)

        goals = intersection(safe, unvisited)
        if (len(goals) == 0):
            print("No safe path ahead is possible!")
            break

        #matrix gets modified in the func itself
        Plan_route(ag, matrix, goals)
コード例 #30
0
ファイル: Engine.py プロジェクト: EyreC/EnvEcon
    def GenerateAgents(self, num_agents):
        """
        Initiates n number of Agent objects within the Engine. The agents are initiated with affinity to consume,
        eco-consciousness

        :param num_agents:
        :return:
        """
        friendList = [j for j in range(num_agents)]
        for i in range(num_agents):
            a = beta.rvs(self.A_params[0],
                         self.A_params[1])  # draw from beta distribution
            b = 1 - a

            mu = beta.rvs(self.Mu_params[0], self.Mu_params[1])
            if round(mu, 5) == 0:
                mu = 0.0001
            income = np.exp(norm.rvs(self.Income_int[0], self.Income_int[1]))

            # income drawn from log-normal distribution, then exponentiated to get normal (level) income.

            delta = rand.uniform(self.Delta_int[0], self.Delta_int[1])

            agent = Agent(i, a, b, mu, income, self.Price, delta)

            # Assign friends
            friendList.remove(i)  # Agents cannot be friends with themselves
            agent.Friends = rand.sample(
                friendList,
                rand.choice(range(self.Friend_int[0], self.Friend_int[1])))
            friendList.append(i)

            self.Agents[i] = agent