示例#1
0
    args = arg_parser.parse_args()
    with open('best_individual.json') as best_agent_weights:
        weights = json.load(best_agent_weights)

    if args.scenario == 'pull':
        genotype_class = BeerTrackerPullGenotype
    elif args.scenario == 'wall':
        genotype_class = BeerTrackerWallGenotype
    else:
        genotype_class = BeerTrackerGenotype

    nn = Rnn(num_input_nodes=genotype_class.num_input_nodes,
             num_hidden_nodes=genotype_class.num_hidden_nodes,
             num_output_nodes=genotype_class.num_output_nodes)
    nn.set_weights(weights)

    g = gfx.Gfx()
    g.fps = 8

    for i in range(args.num_scenarios):
        seed = i + ((997 * args.generation) if args.mode == 'dynamic' else 0)
        print '---', 'seed', seed, '---'
        bt = BeerTracker(nn=nn, seed=seed, scenario=args.scenario)
        bt.gfx = g
        bt.run()
        print bt.world.agent.num_small_misses, 'small miss(es)'
        print bt.world.agent.num_large_misses, 'large miss(es)'
        print bt.world.agent.num_partial_captures, 'partial capture(s)'
        print bt.world.agent.num_small_captures, 'small capture(s)'
        print bt.world.agent.num_large_captures, 'large capture(s)'
示例#2
0
文件: ga.py 项目: iver56/it3708
class BeerTrackerIndividual(Individual):
    range_map = {
        'weight': (-5.0, 5.0),
        'internal_bias': (-10.0, 0.0),
        'gain': (1.0, 5.0),
        'time_constant': (1.0, 2.0)
    }
    genotype_class = None

    def calculate_summed_weight(self, i, range_key):
        j = i * BeerTrackerIndividual.genotype_class.bits_per_weight
        weight = sum(self.genotype.
                     dna[j:j +
                         BeerTrackerIndividual.genotype_class.bits_per_weight])
        weight = self.range_map[range_key][0] + \
                 (self.range_map[range_key][1] - self.range_map[range_key][0]) * \
                 float(weight) / BeerTrackerIndividual.genotype_class.bits_per_weight
        return weight

    def calculate_bitshifted_weight(self, i, range_key):
        j = i * BeerTrackerIndividual.genotype_class.bits_per_weight
        bits = self.genotype.dna[j:j + BeerTrackerIndividual.genotype_class.
                                 bits_per_weight]
        weight = 0
        for bit in bits:
            weight = (weight << 1) | bit
        weight = self.range_map[range_key][0] + \
                 (self.range_map[range_key][1] - self.range_map[range_key][0]) * \
                 float(weight) / (2 ** BeerTrackerIndividual.genotype_class.bits_per_weight)
        return weight

    def calculate_phenotype(self):
        self.phenotype = Rnn(
            BeerTrackerIndividual.genotype_class.num_input_nodes,
            BeerTrackerIndividual.genotype_class.num_hidden_nodes,
            BeerTrackerIndividual.genotype_class.num_output_nodes)

        weights = []

        calculate_weight = self.calculate_summed_weight

        for i in range(BeerTrackerIndividual.genotype_class.rnn.
                       edge_chunks['input_hidden']):
            weight = calculate_weight(i, 'weight')
            weights.append(weight)
        for i in range(BeerTrackerIndividual.genotype_class.rnn.
                       edge_chunks['hidden_hidden']):
            weight = calculate_weight(i, 'weight')
            weights.append(weight)
        for i in range(BeerTrackerIndividual.genotype_class.rnn.
                       edge_chunks['bias_hidden']):
            weight = calculate_weight(i, 'internal_bias')
            weights.append(weight)
        for i in range(BeerTrackerIndividual.genotype_class.rnn.
                       edge_chunks['hidden_output']):
            weight = calculate_weight(i, 'weight')
            weights.append(weight)
        for i in range(BeerTrackerIndividual.genotype_class.rnn.
                       edge_chunks['hidden_gains']):
            weight = calculate_weight(i, 'gain')
            weights.append(weight)
        for i in range(BeerTrackerIndividual.genotype_class.rnn.
                       edge_chunks['hidden_time_constants']):
            weight = calculate_weight(i, 'time_constant')
            weights.append(weight)

        self.phenotype.set_weights(weights)