示例#1
0
from learners.genetic_learner import GeneticLearner, Task
from tools.common_run import get_coordinates, model_dict, read_net, add_common_options


if __name__ == "__main__":
    usage = "map_picture.png net_params.dat"
    parser = optparse.OptionParser(usage=usage)
    parser.add_option('--pop', type='int', dest='population', help="size of population", default=50)
    parser.add_option('--mb', type='float', dest='mutation', help="mutation probability", default=0.2)
    parser.add_option('--cb', type='float', dest='crossover', help="crossover probability", default=0.5)
    parser.add_option('-p', action='store_true', dest='print_to_stdout', default=True,
                      help='print the fitness and number of generation to stdout, runs on --save-interval')
    parser = add_common_options(parser,["-i","-g","-m","-n","-s"])
    opts, args = parser.parse_args()

    map_filename = args[0]
    start, finish = get_coordinates(map_filename)
    ModelClass = model_dict[opts.model]
    ModelClass.inner_layer_nods = opts.inner_nods
    Task.ModelClass = ModelClass
    t = Task(start, finish, map_path=map_filename, number_of_sim_steps=opts.sim_steps)
    l = GeneticLearner(t, generations=opts.generations,
                print_to_stdout=opts.print_to_stdout, mutation_probability=opts.mutation,
                crossover_probability=opts.crossover, population=opts.population)
    if opts.initial_inds is not None:
        initial_net = read_net(opts.initial_inds, ModelClass)
        l.add_initial_individual(initial_net)
    l.savep = args[1]
    l.main()

示例#2
0
import optparse
from visualizer import Visualize
from tools.common_run import get_coordinates, model_dict, read_net, add_common_options, read_net_from_web



if __name__ == "__main__":
    usage = "\t map_picture.png net_params.dat\n or if tou want to download trained net from web: \n \t map_picture.png" \
            " http://adress.of.file \n where address of file must return non html plaintext with net in csv file separated " \
            "by comma, and the adress must start with \"http://\""
    parser = optparse.OptionParser(usage=usage)
    parser = add_common_options(parser,["-n","-m"])
    parser.add_option('-s', type='string', dest='save_path', help="path to save the plot after simulation", default=None)
    parser.add_option('-f', type='string', dest='save_fitness_path', help="path to save the fitness data after simulation", default=None)
    opts, args = parser.parse_args()
    map_filename = args[0]
    start, finish = get_coordinates(map_filename)
    net_filename = args[1]

    ModelClass = model_dict[opts.model]
    ModelClass.inner_layer_nods = opts.inner_nods
    net = None
    if net_filename[:7] == "http://":
        net = read_net_from_web(net_filename, ModelClass)
    else:
        net = read_net(net_filename, ModelClass)
    model = ModelClass(start, finish, map_path=map_filename, neural_net=net)
    Visualize.ModelClass = ModelClass
    v = Visualize(model, save_path=opts.save_path, save_fitness_path=opts.save_fitness_path)
    v.run()