Exemplo n.º 1
0
    def test_mutate(self):
        self.g = genGraph(10, 1)
        population = AiUtils.initPopulation(100, self.g.vertices())

        mutated = AiUtils.mutatePopulation(population, 1, 1)

        self.assertTrue(all(x not in population for x in mutated))
Exemplo n.º 2
0
    def test_rankPaths(self):
        self.g = genGraph(100, 0.9)

        population = AiUtils.initPopulation(50, self.g.vertices())
        ranked = AiUtils.rankPaths(self.g, population)
        self.assertEqual(50, len(ranked))
        for _, fitness in ranked:
            self.assertTrue(fitness > 0 or fitness == -1)
Exemplo n.º 3
0
    def test_selection(self):
        self.g = genGraph(100, 1)

        population = AiUtils.initPopulation(100, self.g.vertices())
        ranked = AiUtils.rankPaths(self.g, population)

        with self.assertRaises(Exception) as context:
            AiUtils.selection(ranked, 30, selectionSize=1.1)

            self.assertTrue('Selection size must be in [0 .. 1]' in context.exception)
Exemplo n.º 4
0
    def __init__(self, mode):
        self.agent_black = None
        self.agent_white = None
        self.alpha: float = 0.2
        self.discount: float = 0.9
        self.greedy: float = 0.8
        if mode == "train":
            self.environment: List[Dict[str, List[int]]] = AiUtils.init_environment()
            self.white_r_matrix = AiUtils.init_white_return_matrix(self.environment)
            self.white_q_matrix = AiUtils.init_white_q_matrix(self.environment)
            self.white_rule_matrix = AiUtils.init_white_rule_matrix(self.environment)
            self.black_p_matrix = AiUtils.init_black_p_matrix(self.environment)
            self.black_r_matrix = AiUtils.init_black_return_matrix(self.environment)

            self.init_state = None
            self.game_count: int = 0
            self.step_count: int = 0
            self.white_route: List[List[int]] = []
            self.black_route: List[List[int]] = []
            self.last_white_action_index: int = -1
        else:
            self.is_train = True
            self.environment: List[Dict[str, List[int]]] = AiUtils.init_environment()
            self.white_r_matrix = np.load("r_matrix.npy")
            self.white_q_matrix = np.load("q_matrix.npy")
            self.white_rule_matrix = AiUtils.init_white_rule_matrix(self.environment)
            self.black_p_matrix = np.load("p_matrix.npy")
            self.black_r_matrix = AiUtils.init_black_return_matrix(self.environment)

            self.game_count: int = (np.load("count.npy"))[0]
            self.step_count: int = 0
            self.white_route: List[List[int]] = []
            self.black_route: List[List[int]] = []
            self.last_white_action_index: int = -1
Exemplo n.º 5
0
    def test_calcFitness(self):
        self.g.addEdge('a', 'b', 10)
        self.g.addEdge('c', 'a', 3)

        path = AiUtils.genPath(self.g.vertices())
        fitness = AiUtils.calcFitness(self.g, path)
        self.assertEqual(-1, fitness)

        self.g.addEdge('b', 'c', 5)
        path = AiUtils.genPath(self.g.vertices())
        fitness = AiUtils.calcFitness(self.g, path)
        self.assertTrue(fitness == 1.0/18)
Exemplo n.º 6
0
    def test_calcDistance(self):
        self.g.addEdge('a', 'b', 12)
        self.g.addEdge('a', 'c', 3)
        self.g.addEdge('b', 'd', 5)
        self.g.addEdge('b', 'c', 6)
        self.g.addEdge('b', 'd', 4)

        path = random.sample(self.g.vertices(), len(self.g.vertices()))
        pathDist = AiUtils.calcDistance(self.g, path)
        self.assertEqual(-1, pathDist)

        self.g.addEdge('c', 'd', 7)
        self.g.addEdge('d', 'a', 8)
        pathDist = AiUtils.calcDistance(self.g, path)
        self.assertTrue(pathDist > 0)
Exemplo n.º 7
0
    def test_breed(self):
        parent1 = list(range(10))
        parent2 = parent1[:]
        random.shuffle(parent2)

        self.assertTrue(len(parent1) == len(parent2))

        child1, child2 = AiUtils.breed(parent1, parent2)

        self.assertTrue(len(child1) == len(child2))
        self.assertTrue(len(child1) == len(parent1))
        self.assertTrue(len(child1) == len(set(child1)))
        self.assertTrue(len(child2) == len(set(child2)))
Exemplo n.º 8
0
async def websocket_endpoint(ws: WebSocket, room: str):
    await manager.connect(room, ws)

    try:
        while True:
            data = await ws.receive_text()
            data = json.loads(data)
            room = data["room"]
            if room:
                # 与ai对战
                if "ai" in room and room != "train":
                    state = copy.deepcopy(data["data"])
                    for agent_item in agents:
                        if agent_item["room"] == room:
                            agent = agent_item["agent"]
                            if state["winner"] is not None:
                                new_return = 1 / agent.step_count if state["winner"] == 1 else -1 / agent.step_count
                                agent.train(new_return)
                                print("return", new_return)
                                print("game count", agent.game_count)
                                await asyncio.sleep(0.1)
                                await manager.send_other_message("continue", room)
                                AiUtils.save_obj(agent.white_q_matrix, agent.white_r_matrix, agent.black_p_matrix, agent.game_count)
                                print("saved Q")
                            else:
                                if state["player"] == 1:
                                    agent.step_count += 1
                                    state = agent.agent_process(state)
                                if state["winner"] is not None:
                                    new_return = 1 / agent.step_count if state["winner"] == 1 else -1 / agent.step_count
                                    agent.train(new_return)
                                    print("return", new_return)
                                    print("game count", agent.game_count)
                                    await asyncio.sleep(0.1)
                                    AiUtils.save_obj(agent.white_q_matrix, agent.white_r_matrix, agent.black_p_matrix, agent.game_count)
                                    print("saved Q")
                                data["data"] = state
                                await asyncio.sleep(0.1)
                                await manager.send_other_message(data, room)
                # 可视化对抗训练1次
                elif room == "train":
                    state = data["data"]

                    if state["winner"] is not None:
                        new_return = 1 / learner.step_count if state["winner"] == 1 else -1 / learner.step_count
                        learner.train(new_return)
                        print("return", new_return)
                        print("game count", learner.game_count)
                        await asyncio.sleep(0.1)
                        if learner.game_count > 0:
                            AiUtils.save_obj(learner.white_q_matrix, learner.white_r_matrix, learner.black_p_matrix, learner.game_count)
                            print("saved Q")
                        else:
                            await manager.send_other_message("continue", room)
                    else:
                        if state["player"] == 1:
                            learner.step_count += 1
                            state = learner.training_white_agent(state)
                        if state["winner"] is not None:
                            new_return = 1 / learner.step_count if state["winner"] == 1 else -1 / learner.step_count
                            learner.train(new_return)
                            print("return", new_return)
                            print("game count", learner.game_count)
                            await asyncio.sleep(1)
                            if learner.game_count > 0:
                                AiUtils.save_obj(learner.white_q_matrix, learner.white_r_matrix, learner.black_p_matrix, learner.game_count)
                                print("saved Q")
                        if learner.game_count <= 0:
                            data["data"] = state
                            await asyncio.sleep(0.1)
                            await manager.send_other_message(data, room)
                # 正常与别人对战
                else:
                    await manager.send_other_message(data, room)
    except WebSocketDisconnect:
        manager.disconnect(room, ws)