Commits

dan mackinlay  committed e869fe4

add plotting of pop stats

  • Participants
  • Parent commits 5177134

Comments (0)

Files changed (2)

File risky_business/market.py

     lottery = trader.choose_lottery([lotteries.get_lottery() for i in range(3)])
     return lottery.decide()
 
-def value_of_lifetime(genome, Klass=privateers.CARATrader, rounds=1000):
+def value_of_lifetime(genome, Klass=privateers.CARATrader, rounds=10):
     "several rounds of market trading"
     trader = Klass(genome)
     for round in range(rounds):

File risky_business/privateer_sim.py

 import PrivateerMutators
 
 import argparse
+
+mean_fitness = []
+peak_fitness = []
+leader_gamma = []
+median_gamma = []
+median_fitness = []
    
 def main():
     import privateers
     population = 100
     elitism_rate = .6*population
     genome = G1DList.G1DList(1)
-    generations = 1150
+    generations = 200
     
     # Sets the range max and min of the 1D List
     genome.setParams(rangemin=-8.0, rangemax=8.0)
     pop.scaleMethod.set(Scaling.SigmaTruncScaling)
     
     ga_engine.stepCallback.add(update_plot)
+    ga_engine.stepCallback.add(update_stats)
     
     ga_engine.setMutationRate(0.01)
     
     
     # Best individual
     print ga_engine.bestIndividual()
-
+    
+    plot_stats()
+    raw_input()
 
 plot_line = None
 plt.ion()
         plot_line, = plt.plot(genomes, fitnesses)
     plot_line.set_ydata(fitnesses)
     plot_line.set_xdata(genomes)
+    plt.title('fitness versus gamma')
     plt.draw()
 
     return False
 
+def update_stats(ga_engine):
+    global mean_fitness, peak_fitness, median_fitness, leader_gamma, median_gamma
+    pop = ga_engine.getPopulation()
+    mean_fitness.append(dict(pop.getStatistics().items())['rawAve'])
+    leader = pop[0]
+    peak_fitness.append(pop[0].fitness)
+    leader_gamma.append(leader.genomeList[0])
+    median = pop[len(pop)/2]
+    median_fitness.append(median.fitness)
+    median_gamma.append(median.genomeList[0])
     
+    return False
+    
+def plot_stats():
+    global mean_fitness, peak_fitness, median_fitness, leader_gamma, median_gamma
+    x = range(len(peak_fitness))
+    plt.figure(2)
+    plt.subplot(211)
+    plt.plot(x, peak_fitness, 'r', x, median_fitness, 'b')
+    plt.title('peak and median fitness')
+    plt.subplot(212)
+    plt.plot(x, leader_gamma, 'r', x, median_gamma, 'b')
+    plt.title('peak and median gamma')
+    
+
 if __name__ == "__main__":
    main()