예제 #1
0
    def crossover(self, agents: [Agent], last_agent_id: int) -> [Agent]:
        agent_id = last_agent_id
        offspring = []
        for _ in range(self.to_create):
            [parent1, parent2] = random.sample(agents, 2)

            split1 = random.randint(1, len(parent1.genome) - 1)

            min_for_split2 = max(split1 + len(parent2.genome) - self.max_genes,
                                 1)
            max_for_split2 = min(split1 - len(parent1.genome) + self.max_genes,
                                 len(parent2.genome) - 1)

            split2 = random.randint(min_for_split2, max_for_split2)

            child1_genome = parent1.genome[:split1] + parent2.genome[split2:]
            child2_genome = parent2.genome[:split2] + parent1.genome[split1:]

            child1 = Agent(agent_id, child1_genome, self.config)
            child2 = Agent(agent_id + 1, child2_genome, self.config)

            offspring.append(child1)
            offspring.append(child2)

            agent_id += 2
        return offspring
예제 #2
0
def test_calculateStrength_validStock_returnsStockStrength(config):
    stock = Company("test", "TEST", "/test", "sector:test")
    day = date(2000, 1, 1)
    genome = [FakeTrueGene(), FakeTrueGene()]
    agent = Agent(1, genome, config)

    strength = agent.calculate_strength(stock, day)

    assert strength == 2
예제 #3
0
def test_calculate_testing_fitness_validDatabase_returnsFitness(config):
    genome = [FakeTrueGene]
    agent = Agent(1, genome, config)
    agent.trader = FakeTrader(config)

    database = []

    agent.calculate_testing_fitness(database, 0)

    assert agent.testing_fitnesses[0] == 260
예제 #4
0
def test_testing_simulate_validDatabase_returnsFitness(config):
    genome = [FakeTrueGene]
    agent = Agent(1, genome, config)
    agent.trader = FakeTrader(config)

    database = []

    fitness = agent.testing_simulate(database, 0)

    # Iterations (trade days) between date(2000,1,1) and date(2001,1,1)
    assert fitness == 260
예제 #5
0
def test_to_json_ready_validAgent_returnsValidDict(config):
    genome = [FakeTrueGene()]
    agent = Agent(1, genome, config)

    agent_dict = agent.to_json_ready()

    assert agent_dict == {
        "id": 1,
        "strategy": ["1.00 x If(Fake) then 1 else -1"],
        "fitness": "0.00",
        "validations": ["0.00"],
    }
예제 #6
0
def test_crossover_validAgents_createsNewMixedGenomeAgents():
    config = Config()
    genome1 = [FakeTrueGene(), FakeTrueGene()]
    agent1 = Agent(1, genome1, config)

    genome2 = [FakeFalseGene(), FakeFalseGene()]
    agent2 = Agent(2, genome2, config)

    crossover_op = Constant(2, 2, config)
    new_agents = crossover_op.crossover([agent1, agent2], 3)

    assert (new_agents[0].agent_id == 3 and new_agents[1].agent_id == 4
            and len(new_agents[0].genome) == 2
            and len(new_agents[1].genome) == 2)
 def init_non_constant_length_agents(self) -> [Agent]:
     agents = []
     for i in range(self.config.initial_population):
         genome_length = random.randint(2, self.config.max_genes)
         genome = self.gene_factory.create_n_random_genes(genome_length)
         agents += Agent(i, genome, self.config)
     return agents
 def init_constant_length_agents(self) -> [Agent]:
     agents = []
     for i in range(self.config.initial_population):
         genome = self.gene_factory.create_n_random_genes(
             self.config.initial_length)
         agents.append(Agent(i, genome, self.config))
     return agents
예제 #9
0
    def crossover(self, agents: [Agent], last_agent_id: int) -> [Agent]:
        agent_id = last_agent_id
        offspring = []
        for _ in range(self.to_create):
            [parent1, parent2] = random.sample(agents, 2)

            split = random.randint(0, self.agent_len)
            child1_genome = parent1.genome[:split] + parent2.genome[split:]
            child2_genome = parent2.genome[:split] + parent1.genome[split:]

            child1 = Agent(agent_id, child1_genome, self.config)
            child2 = Agent(agent_id + 1, child2_genome, self.config)

            offspring.append(child1)
            offspring.append(child2)

            agent_id += 2
        return offspring
def test_mutate_validAgent_returnsAgentWithDifferentGene(config, gene_factory):
    one_gene = gene_factory.create_random_gene()
    agent = Agent(1, [one_gene], config)

    old_gene_str = one_gene.to_string()
    operator = GeneCreation(1.0, gene_factory)

    new_agent = operator.mutate([agent])[0]
    new_gene_str = new_agent.genome[0].to_string()
    assert old_gene_str != new_gene_str
def test_mutate_validAgent_returnsAgentWithChangedWeights(
        config, gene_factory):
    one_gene = gene_factory.create_random_gene()
    agent = Agent(1, [one_gene], config)

    old_weght = one_gene.weight
    operator = Normalization(1.0)

    new_agent = operator.mutate([agent])[0]
    new_weight = new_agent.genome[0].weight
    assert new_weight != old_weght
예제 #12
0
def test_select_one_aspirant_validAgents_returnsAgentWithGreaterFitness(
        config):
    agents_to_create = 2
    agents_to_save_rate = 0.0
    rating = Tournament(agents_to_save_rate)

    agents = [Agent(i, [], config) for i in range(agents_to_create)]
    agents[0].learning_fitness = 10.0
    agents[1].learning_fitness = 17.0

    selected_agent = rating.select_one_aspirant(agents, 2)
    assert selected_agent.agent_id == agents[1].agent_id
예제 #13
0
def test_select_validAgents_returnsSelectedAmount(config):
    agents_to_create = 4
    agents_to_save_rate = 0.5
    rating = Tournament(agents_to_save_rate)

    agents = [Agent(i, [], config) for i in range(agents_to_create)]
    agents[0].learning_fitness = 10.0
    agents[1].learning_fitness = 7.0
    agents[2].learning_fitness = 5.0
    agents[3].learning_fitness = 3.0
    new_agents = rating.select(agents)

    assert len(new_agents) == 2
 def calculate_agent_validation_performance(self, agent: Agent):
     for idx in range(len(self.config.validations)):
         fit = agent.calculate_testing_fitness(
             self.testing_databases[idx].companies, idx)
         print(f"[{agent.agent_id}] validation #{idx} - {fit}")
예제 #15
0
def test_next_business_day_weekend_returnsNextMonday(config, day):
    agent = Agent(1, [], config)

    next_monday_from_weekend = agent.shift_to_weekday(day)

    assert next_monday_from_weekend.weekday() == 0
예제 #16
0
def test_next_business_day_weekday_returnsSameDay(config, day):
    agent = Agent(1, [], config)

    same_day = agent.shift_to_weekday(day)

    assert same_day == day