def test_mutation_parent_selection():
    """ Tests mutation_parent_selection function """
    gp_par = gp.GpParameters()
    gp_par.n_population = 8
    gp_par.f_mutation = 0.5
    gp_par.parent_selection = gp.SelectionMethods.ELITISM
    gp_par.mutate_co_parents = True
    gp_par.mutate_co_offspring = True
    population = gp.create_population(gp_par.n_population, 5)
    crossover_parents = [5, 6, 7]
    crossover_offspring = gp.create_population(2, 5)
    fitness = [0, 1, 2, 1, 2, 1, 1, 2, 2, 0]
    parents = gp.mutation_parent_selection(population, fitness,
                                           crossover_parents,
                                           crossover_offspring, gp_par)
    assert parents == [8, 7, 4, 2]

    gp_par.mutate_co_parents = False
    gp_par.mutate_co_offspring = False
    parents = gp.mutation_parent_selection(population, fitness,
                                           crossover_parents,
                                           crossover_offspring, gp_par)
    assert parents == [4, 2, 3, 1]

    gp_par.n_population = 4
    gp_par.f_mutation = 0.1
    fitness = [0, 1, 2, 1]
    parents = gp.mutation_parent_selection(population, fitness, [], [], gp_par)
    assert parents == []
def test_run():
    """ Tests run function """
    gp_par = gp.GpParameters()
    gp_par.ind_start_length = 3
    gp_par.n_population = 20
    gp_par.f_crossover = 0.5
    gp_par.n_offspring_crossover = 10
    gp_par.f_mutation = 0.25
    gp_par.n_offspring_mutation = 10
    gp_par.f_elites = 0.1
    gp_par.f_parents = 1
    gp_par.plot = False
    gp_par.n_generations = 100
    gp_par.verbose = True

    gp.set_seeds(1337)
    population, fitness, _, _ = gp.run(environment, gp_par)
    assert (max(fitness)) == 3.4

    gp.set_seeds(1337)
    population2, fitness2, _, _ = gp.run(environment, gp_par)
    assert population == population2
    assert fitness == fitness2

    gp_par.parent_selection = gp.SelectionMethods.RANK
    gp_par.survivor_selection = gp.SelectionMethods.RANK
    gp.set_seeds(1337)
    population, fitness, _, _ = gp.run(environment, gp_par)
    assert (max(fitness)) == 3.4

    gp.set_seeds(1337)
    population2, fitness2, _, _ = gp.run(environment, gp_par)
    assert population == population2
    assert fitness == fitness2
def test_survivor_selection():
    """ Tests survivor_selection function """
    gp_par = gp.GpParameters()
    gp_par.f_parents = 1 / 3
    gp_par.f_elites = 0
    gp_par.n_population = 6
    gp_par.survivor_selection = gp.SelectionMethods.ELITISM
    population = list(range(gp_par.n_population))
    crossover_offspring = list(
        range(gp_par.n_population, gp_par.n_population + 2))
    mutated_offspring = list(
        range(gp_par.n_population + 2, gp_par.n_population + 4))
    fitness = [0, 1, 2, 1, 2, 1, 1, 2, 2, 0]
    survivors, survivor_fitness = gp.survivor_selection(population, \
                                                        fitness, \
                                                        crossover_offspring, \
                                                        mutated_offspring, \
                                                        gp_par)
    assert len(survivors) == len(survivor_fitness)
    assert survivors == [8, 7, 2, 4, 6, 9]
    assert survivor_fitness == [2, 2, 2, 2, 1, 0]

    gp_par.f_parents = 0
    gp_par.f_elites = 2 / 3
    gp_par.n_population = 3
    gp_par.survivor_selection = gp.SelectionMethods.RANDOM
    survivors, survivor_fitness = gp.survivor_selection(population, \
                                                        fitness, \
                                                        crossover_offspring, \
                                                        mutated_offspring, \
                                                        gp_par)
    assert len(survivors) == len(survivor_fitness)
    assert survivors[0] == 8
    assert survivors[1] == 7
    assert survivors[2] == 9 or survivors[2] == 6
Пример #4
0
def test_balance():
    """ Test balance scenario """
    gp_par = gp.GpParameters()
    gp_par.ind_start_length = 8
    gp_par.n_population = 16
    gp_par.f_crossover = 0.5
    gp_par.n_offspring_crossover = 2
    gp_par.replace_crossover = False
    gp_par.f_mutation = 0.5
    gp_par.n_offspring_mutation = 2
    gp_par.parent_selection = gp.SelectionMethods.RANK
    gp_par.survivor_selection = gp.SelectionMethods.RANK
    gp_par.f_elites = 0.1
    gp_par.f_parents = gp_par.f_elites
    gp_par.mutate_co_offspring = False
    gp_par.mutate_co_parents = True
    gp_par.mutation_p_add = 0.4
    gp_par.mutation_p_delete = 0.3
    gp_par.allow_identical = False
    gp_par.plot = True
    gp_par.n_generations = 300
    gp_par.verbose = False
    gp_par.fig_last_gen = False

    behavior_tree.load_settings_from_file(
        'duplo_state_machine/BT_SETTINGS_BALANCE.yaml')
    planner_baseline = ['s(', 'f(', '0 at pos (0.0, 0.0, 0.0192)?', \
                                    's(', 'f(', '1 at pos (0.0, 0.0, 0.0)?', 'put 1 at (0.0, 0.0, 0.0)!', ')', \
                                          'f(', '0 on 1?', 'put 0 on 1!', ')', 'apply force 0!', ')', ')', ')']

    solved = ['f(', '0 at pos (0.0, 0.0, 0.0192)?', \
                    's(', 'put 2 at (0.0, 0.0, 0.0)!', 'put 0 on 2!', 'apply force 0!', ')', ')']

    start_positions = []
    start_positions.append(Pos(-0.05, -0.1, 0.0))
    start_positions.append(Pos(0.0, -0.1, 0.0))
    start_positions.append(Pos(0.05, -0.1, 0.0))
    targets = []
    targets.append(Pos(0.0, 0.0, 0.0192))
    fitness_coeff = Coefficients()
    fitness_coeff.hand_not_empty = 100.0

    environment = sm_environment.Environment(start_positions, targets, verbose=False, \
                                             mode=sm.SMMode.BALANCE, fitness_coeff=fitness_coeff)

    fitness = environment.get_fitness(planner_baseline)
    assert fitness < -10

    fitness = environment.get_fitness(solved)
    assert fitness > -1

    gp_par.log_name = 'test_balance'
    _, _, best_fitness, _ = gp.run(environment,
                                   gp_par,
                                   baseline=planner_baseline)
    assert best_fitness[-1] > -1

    behavior_tree.load_settings_from_file(
        'behavior_tree_learning/tests/BT_TEST_SETTINGS.yaml')
def test_hotstart():
    """ Test with hotstart """
    gp_par = gp.GpParameters()
    gp_par.ind_start_length = 3
    gp_par.n_population = 8
    gp_par.f_crossover = 0.5
    gp_par.n_offspring_crossover = 2
    gp_par.f_mutation = 0.5
    gp_par.n_offspring_mutation = 2
    gp_par.f_elites = 1 / 8
    gp_par.f_parents = gp_par.f_elites
    gp_par.plot = True
    gp_par.verbose = False
    gp_par.fig_last_gen = True

    gp.set_seeds(0)
    gp_par.n_generations = 155
    gp.run(environment, gp_par, hotstart=False)

    gp_par.n_generations = 200
    population, fitness, best_fitness, best_individual = gp.run(environment,
                                                                gp_par,
                                                                hotstart=True)

    gp_par.log_name = '2'
    gp.set_seeds(0)
    population2, fitness2, best_fitness2, best_individual2 = gp.run(
        environment, gp_par, hotstart=False)

    assert population == population2
    assert fitness == fitness2
    assert best_fitness == best_fitness2
    assert best_individual == best_individual2

    gp.set_seeds(0)
    gp_par.n_generations = 100
    population, fitness, best_fitness, best_individual = gp.run(environment,
                                                                gp_par,
                                                                hotstart=False)

    gp_par.n_generations = 100
    population2, fitness2, best_fitness2, best_individual2 = gp.run(
        environment, gp_par, hotstart=True)

    assert population == population2
    assert fitness == fitness2
    assert best_fitness == best_fitness2
    assert best_individual == best_individual2
def test_crossover_parent_selection():
    """ Tests crossover_parent_selection function """
    gp_par = gp.GpParameters()
    gp_par.n_population = 10
    gp_par.f_crossover = 0.4
    gp_par.parent_selection = gp.SelectionMethods.ELITISM
    population = gp.create_population(gp_par.n_population, 5)
    fitness = [0, 1, 2, 1, 2, 1, 1, 2, 2, 0]
    parents = gp.crossover_parent_selection(population, fitness, gp_par)
    assert parents == [8, 7, 4, 2]

    gp_par.n_population = 4
    gp_par.f_crossover = 0.1
    fitness = [0, 1, 2, 1]
    parents = gp.crossover_parent_selection(population, fitness, gp_par)
    assert parents == []
def test_mutation():
    """ Tests mutation function """
    gp_par = gp.GpParameters()
    gp_par.n_population = 5
    gp_par.n_offspring_mutation = 1
    gp_par.mutation_p_add = 0.3
    gp_par.mutation_p_delete = 0.3
    gp_par.allow_identical = False

    for _ in range(5):  #Test a number of random possibilities
        population = gp.create_population(gp_par.n_population, 5)
        mutated_population = gp.mutation(population, range(len(population)),
                                         gp_par)
        assert len(mutated_population
                   ) == gp_par.n_offspring_mutation * gp_par.n_population
        for i in range(len(mutated_population)):
            assert mutated_population[i] not in population
            for j in range(len(mutated_population)):
                if i != j:
                    assert mutated_population[i] != mutated_population[j]

    gp_par.n_offspring_mutation = 5 * gp_par.n_population + 1
    gp_par.allow_identical = True
    for _ in range(5):  #Test a number of random possibilities
        population = gp.create_population(gp_par.n_population, 5)
        mutated_population = gp.mutation(population, range(len(population)),
                                         gp_par)
        assert len(mutated_population
                   ) == gp_par.n_offspring_mutation * gp_par.n_population

    #Test with too limited mutation possibilities
    gp_par.n_population = 5
    gp_par.n_offspring_mutation = 1
    gp_par.mutation_p_add = 0.0
    gp_par.mutation_p_delete = 1.0
    gp_par.allow_identical = False
    parents = gp.create_population(gp_par.n_population, 2)
    blockers = gp.create_population(7, 1)
    population = parents + blockers
    mutated_population = gp.mutation(population, range(len(parents)), gp_par)
    assert len(mutated_population
               ) != gp_par.n_offspring_mutation * gp_par.n_population
    for i in range(len(mutated_population)):
        assert mutated_population[i] not in population
        for j in range(len(mutated_population)):
            if i != j:
                assert mutated_population[i] != mutated_population[j]
def test_crossover():
    """ Tests crossover function """
    pop_size = 10
    gp_par = gp.GpParameters()
    gp_par.n_population = pop_size
    gp_par.n_offspring_crossover = 1
    gp_par.allow_identical = False
    for _ in range(5):  #Test a number of random possibilities
        population = gp.create_population(pop_size, 5)
        crossover_population = gp.crossover(population, range(len(population)),
                                            gp_par)
        for i in range(len(crossover_population)):
            assert crossover_population[i] not in population
            for j in range(len(crossover_population)):
                if i != j:
                    assert crossover_population[i] != crossover_population[j]

    gp_par.n_offspring_crossover = 3 * pop_size
    gp_par.allow_identical = True
    for _ in range(5):  #Test a number of random possibilities
        population = gp.create_population(pop_size, 5)
        crossover_population = gp.crossover(population, range(len(population)),
                                            gp_par)
        assert len(crossover_population
                   ) == gp_par.n_offspring_crossover * gp_par.n_population

    #Test what happens if no valid crossovers can be found
    parents = [['s(', 'a0', ')'], ['s(', 'a1', ')']]
    gp_par.n_offspring_crossover = 1
    gp_par.allow_identical = False
    gp_par.n_population = 2
    blockers = [['s(', 'a1', 'a0', ')'], ['s(', 'a0', 'a1', ')']]
    population = parents + blockers
    crossover_population = gp.crossover(population, range(len(parents)),
                                        gp_par)
    assert len(crossover_population
               ) == gp_par.n_offspring_crossover * gp_par.n_population
    for i in range(len(crossover_population)):
        assert crossover_population[i] not in population
        for j in range(len(crossover_population)):
            if i != j:
                assert crossover_population[i] != crossover_population[j]

    #Number of parents not factor of two
    with pytest.raises(ValueError):
        crossover_population = gp.crossover(population, range(3), gp_par)
Пример #9
0
def paper_run():
    # pylint: disable=too-many-branches, too-many-statements
    """ Run this to produce results from the paper 'Combining Planning and Learning of Behavior Trees for Robotic Assembly' """
    rosid = "1"
    if len(sys.argv) > 1:
        rosid = sys.argv[1]

    behavior_tree.load_settings_from_file('BT_SETTINGS_TOWER.yaml')
    gp_par = gp.GpParameters()
    gp_par.ind_start_length = 8
    gp_par.n_population = 16
    gp_par.f_crossover = 0.5
    gp_par.n_offspring_crossover = 2
    gp_par.replace_crossover = False
    gp_par.f_mutation = 0.5
    gp_par.n_offspring_mutation = 2
    gp_par.parent_selection = gp.SelectionMethods.RANK
    gp_par.survivor_selection = gp.SelectionMethods.RANK
    gp_par.f_elites = 0.1
    gp_par.f_parents = gp_par.f_elites
    gp_par.mutate_co_offspring = False
    gp_par.mutate_co_parents = True
    gp_par.mutation_p_add = 0.4
    gp_par.mutation_p_delete = 0.3
    gp_par.allow_identical = False
    gp_par.plot = True
    gp_par.n_generations = 200
    gp_par.verbose = False
    gp_par.fig_last_gen = False

    world_interface = agx_interface.AgxInterface(rosid)

    ########################################################################################
    # Tower
    ########################################################################################
    targets = []
    targets.append(agx_interface.Pos(0.0, 0.05, 0))
    targets.append(agx_interface.Pos(0.0, 0.05, 0.0192))
    targets.append(agx_interface.Pos(0.0, 0.05, 2 * 0.0192))
    environment = Environment(world_interface, targets, verbose=False)

    planner_baseline = ['s(', 'f(', '0 at pos (0.0, 0.05, 0.0)?', 's(', 'f(', 'picked 0?', 'pick 0!', ')', 'place at (0.0, 0.05, 0.0)!', ')', ')', \
                        'f(', '1 at pos (0.0, 0.05, 0.0192)?', 's(', 'f(', '1 on 0?', 's(', 'f(', 'picked 1?', 'pick 1!', ')', 'place on 0!', ')', ')', 'apply force 1!', ')', ')',  \
                        'f(', '2 at pos (0.0, 0.05, 0.0384)?', 's(', 'f(', '2 on 1?', 's(', 'f(', 'picked 2?', 'pick 2!', ')', 'place on 1!', ')', ')', 'apply force 2!', ')', ')', ')'] #-3.0109... 0.0109

    #Best from experiments
    #planner_baseline = ['s(', 'f(', '0 at pos (0.0, 0.05, 0.0)?', 's(', 'pick 0!', 'place at (0.0, 0.05, 0.0)!', ')', ')', \
    #                    'f(', '1 at pos (0.0, 0.05, 0.0192)?', 's(', 'f(', '1 on 0?', 's(', 'pick 1!', 'place on 0!', ')', ')', 'apply force 1!', ')', ')',  \
    #                    'f(', '2 at pos (0.0, 0.05, 0.0384)?', 's(', 'f(', '2 on 1?', 's(', 'pick 2!', 'place on 1!', ')', ')', 'apply force 2!', ')', ')', ')'] #-2.4109819330136357 0.0109

    #planner_baseline = ['s(', 'f(', '0 at pos (0.0, 0.05, 0.0)?', 's(', 'pick 0!', 'place at (0.0, 0.05, 0.0)!', ')', ')', \
    #                    'f(', '1 on 0?', 's(', 'pick 1!', 'place on 0!', ')', ')', \
    #                    'f(', '1 at pos (0.0, 0.05, 0.0192)?', 'apply force 1!', ')',  \
    #                    'f(', '2 on 1?', 's(', 'pick 2!', 'place on 1!', ')', ')', \
    #                    'f(', '2 at pos (0.0, 0.05, 0.0384)?', 'apply force 2!', ')',')'] #-2.2109819330136357 0.0109

    n_logs = 10
    for i in range(1, n_logs + 1):
        gp_par.log_name = 'tower_no_baseline_' + str(i)
        gp.set_seeds(i)
        gp.run(environment, gp_par)

    for i in range(1, n_logs + 1):
        gp_par.log_name = 'tower_planner_baseline_' + str(i)
        gp.set_seeds(i)
        gp.run(environment, gp_par, baseline=planner_baseline)

    for i in range(0):
        start = time.time()
        fitness = environment.get_fitness(planner_baseline)
        print(str(i) + ": " + str(time.time() - start))
        print(fitness)

    ########################################################################################
    # Croissant
    ########################################################################################
    behavior_tree.load_settings_from_file('BT_SETTINGS_CROISSANT.yaml')
    planner_baseline = ['s(', 'f(', '0 at pos (0.0, 0.0, 0.0)?', 's(', 'f(', 'picked 0?', 'pick 0!', ')', 'place at (0.0, 0.0, 0.0)!', ')', ')', \
                        'f(', '1 at pos (0.0, 0.0, 0.0192)?', 's(', 'f(', '1 on 0?', 's(', 'f(', 'picked 1?', 'pick 1!', ')', 'place on 0!', ')', ')', 'apply force 1!', ')', ')', \
                        'f(', '2 at pos (0.016, -0.032, 0.0)?', 's(', 'f(', 'picked 2?', 'pick 2!', ')', 'place at (0.016, -0.032, 0.0)!', ')', ')', \
                        'f(', '3 at pos (0.016, 0.032, 0.0)?', 's(', 'f(', 'picked 3?', 'pick 3!', ')', 'place at (0.016, 0.032, 0.0)!', ')', ')', ')'] #-237...

    #best from experiments
    #planner_baseline = ['s(', 'f(', '0 at pos (0.0, 0.0, 0.0)?', 's(', 'pick 0!', 'place at (0.0, 0.0, 0.0)!', ')', ')', \
    #                    'f(', '2 at pos (0.016, -0.032, 0.0)?', 's(', 'pick 2!', 'place at (0.016, -0.032, 0.0)!', ')', ')', \
    #                    'f(', '3 at pos (0.016, 0.032, 0.0)?', 's(', 'pick 3!', 'place at (0.016, 0.032, 0.0)!', ')', ')', \
    #                    'f(', '1 at pos (0.0, 0.0, 0.0192)?', 's(', 'f(', '1 on 0?', 's(', 'pick 1!', 'place on 0!', ')', ')', 'apply force 1!', ')', ')', ')'] #-2.5
    #planner_baseline = ['s(', 'f(', '0 at pos (0.0, 0.0, 0.0)?', 's(', 'pick 0!', 'place at (0.0, 0.0, 0.0)!', ')', ')', \
    #                    'f(', '3 at pos (0.016, 0.032, 0.0)?', 's(', 'pick 3!', 'place at (0.016, 0.032, 0.0)!', ')', ')', \
    #                    'f(', '2 at pos (0.016, -0.032, 0.0)?', 's(', 'pick 2!', 'place at (0.016, -0.032, 0.0)!', ')', ')', \
    #                    'f(', '1 on 0?', 's(', 'pick 1!', 'place on 0!', ')', ')',
    #                    'f(', '1 at pos (0.0, 0.0, 0.0192)?', 'apply force 1!', ')', ')'] #-2.4
    #planner_baseline = ['s(', 'f(', '0 at pos (0.0, 0.0, 0.0)?', 's(', 'pick 0!', 'place at (0.0, 0.0, 0.0)!', ')', ')', \
    #                    'f(', '1 at pos (0.0, 0.0, 0.0192)?', 's(', 'f(', '1 on 0?', 's(', 'f(', '2 at pos (0.016, -0.032, 0.0)?', 's(', '3 at pos (0.016, 0.032, 0.0)?', 'pick 2!', 'place at (0.016, -0.032, 0.0)!', ')', 'place at (0.016, 0.032, 0.0)!', ')', \
    #                    'pick 1!', 'place on 0!', ')', ')', 'apply force 1!', ')', 'pick 3!', ')', ')'] #-2.33... Very clever with brick 3..

    targets = []
    targets.append(agx_interface.Pos(0.0, 0.0, 0.0))
    targets.append(agx_interface.Pos(0.0, 0.0, 0.0192))
    targets.append(agx_interface.Pos(0.016, -0.032, 0.0))
    targets.append(agx_interface.Pos(0.016, 0.032, 0.0))
    environment = Environment(world_interface, targets, verbose=False)
    gp_par.n_generations = 1000

    n_logs = 5

    for i in range(1, n_logs + 1):
        gp_par.log_name = 'cro_planner_baseline_' + str(i)
        gp.set_seeds(i)
        #gp.run(environment, gp_par, baseline=planner_baseline)

    for i in range(1, n_logs + 1):
        gp_par.log_name = 'cro_planner_baseline_boost_co_' + str(i)
        gp_par.boost_baseline = True
        gp_par.boost_baseline_only_co = True
        gp.set_seeds(i)
        gp.run(environment, gp_par, baseline=planner_baseline, hotstart=False)
        gp_par.boost_baseline = False

    for i in range(1, n_logs + 1):
        gp_par.log_name = 'cro_planner_baseline_boost_all_' + str(i)
        gp_par.boost_baseline = True
        gp_par.boost_baseline_only_co = False
        gp.set_seeds(i)
        gp.run(environment, gp_par, baseline=planner_baseline, hotstart=False)
        gp_par.boost_baseline = False
        gp_par.boost_baseline_only_co = True

    for i in range(1, n_logs + 1):
        gp_par.log_name = 'cro_no_baseline_' + str(i)
        gp.set_seeds(i)
        gp.run(environment, gp_par)

    for i in range(0):
        start = time.time()
        fitness = environment.get_fitness(planner_baseline)
        print(str(i) + ": " + str(time.time() - start))
        print(fitness)

    ########################################################################################
    # Balance
    ########################################################################################
    behavior_tree.load_settings_from_file('BT_SETTINGS_BALANCE.yaml')
    planner_baseline = [
        's(', 'f(', '0 at pos (0.0, 0.0, 0.0192)?', 's(', 'f(',
        '1 at pos (0.0, 0.0, 0.0)?', 'put 1 at (0.0, 0.0, 0.0)!', ')', 'f(',
        '0 on 1?', 'put 0 on 1!', ')', 'apply force 0!', ')', ')', ')'
    ]  #-117.7... 6.6...

    #Solved
    #planner_baseline = ['f(', '0 at pos (0.0, 0.0, 0.0192)?', 's(', 'put 2 at (0.0, 0.0, 0.0)!', 'put 0 on 2!', 'apply force 0!', ')', ')'] #-0.6...

    targets = []
    targets.append(agx_interface.Pos(0.0, 0.0, 0.0192))
    fitness_coeff = Coefficients()
    fitness_coeff.hand_not_empty = 100.0

    environment = Environment(world_interface,
                              targets,
                              verbose=False,
                              fitness_coeff=fitness_coeff)
    gp_par.n_generations = 800

    n_logs = 10

    for i in range(1, n_logs + 1):
        gp_par.log_name = 'balance_planner_baseline_' + str(i)
        gp.set_seeds(i)
        gp.run(environment, gp_par, baseline=planner_baseline, hotstart=False)

    for i in range(1, n_logs + 1):
        gp_par.log_name = 'balance_no_baseline_' + str(i)
        gp.set_seeds(i)
        gp.run(environment, gp_par, hotstart=False)

    for i in range(1, n_logs + 1):
        gp_par.log_name = 'balance_planner_baseline_boost_all_' + str(i)
        gp_par.boost_baseline = True
        gp_par.boost_baseline_only_co = False
        gp.set_seeds(i)
        gp.run(environment, gp_par, baseline=planner_baseline, hotstart=False)
        gp_par.boost_baseline = False
        gp_par.boost_baseline_only_co = True

    for i in range(1, n_logs + 1):
        gp_par.log_name = 'balance_planner_baseline_boost_co_' + str(i)
        gp_par.boost_baseline = True
        gp_par.boost_baseline_only_co = True
        gp.set_seeds(i)
        gp.run(environment, gp_par, baseline=planner_baseline, hotstart=False)
        gp_par.boost_baseline = False

    for i in range(0):
        start = time.time()
        fitness = environment.get_fitness(planner_baseline)
        print(str(i) + ": " + str(time.time() - start))
        print(fitness)

    ########################################################################################
    # Blocking
    ########################################################################################
    behavior_tree.load_settings_from_file('BT_SETTINGS_BLOCKING.yaml')
    planner_baseline = ['s(', 'f(', '0 at pos (-0.1, 0.0, 0.0)?', 'put 0 at (-0.1, 0.0, 0.0)!', ')', \
                        'f(', '1 at pos (-0.1, 0.0, 0.0192)?', 's(', 'f(', '1 on 0?', 'put 1 on 0!', ')', 'apply force 1!', ')', ')', \
                        'f(', '2 at pos (0.0, 0.0, 0.0)?', 'put 2 at (0.0, 0.0, 0.0)!', ')', ')'] #-288...

    #Solved
    #planner_baseline = ['s(', 'put 1 on 0!', 'f(', '0 at pos (-0.1, 0.0, 0.0)?', 'put 2 at (0.0, 0.05, 0.0)!', ')', \
    #                    'put 0 at (-0.1, 0.0, 0.0)!', \
    #                    'f(', '1 at pos (-0.1, 0.0, 0.0192)?', 'apply force 1!', ')', \
    #                    'put 2 at (0.0, 0.0, 0.0)!', ')'] #-1.0

    #planner_baseline = ['s(', 'f(', '1 at pos (-0.1, 0.0, 0.0192)?', \
    #                                's(', 'put 1 on 0!', 'put 2 at (0.0, 0.05, 0.0)!', 'put 0 at (-0.1, 0.0, 0.0)!', 'apply force 1!', ')', ')',
    #                          'put 2 at (0.0, 0.0, 0.0)!', ')'] #-0.9

    targets = []
    targets.append(agx_interface.Pos(-0.1, 0.0, 0.0))
    targets.append(agx_interface.Pos(-0.1, 0.0, 0.0192))
    targets.append(agx_interface.Pos(0.0, 0.0, 0.0))
    fitness_coeff = Coefficients()
    fitness_coeff.hand_not_empty = 0.0

    environment = Environment(world_interface,
                              targets,
                              verbose=False,
                              fitness_coeff=fitness_coeff)
    gp_par.n_generations = 300

    n_logs = 5
    for i in range(1, n_logs + 1):
        gp_par.log_name = 'block_planner_baseline_' + str(i)
        gp.set_seeds(i)
        #gp.run(environment, gp_par, baseline=planner_baseline)

    for i in range(1, n_logs + 1):
        gp_par.log_name = 'block_no_baseline_' + str(i)
        gp.set_seeds(i)
        gp.run(environment, gp_par, hotstart=False)

    for i in range(1, n_logs + 1):
        gp_par.log_name = 'block_planner_baseline_boost_co_' + str(i)
        gp_par.boost_baseline = True
        gp_par.boost_baseline_only_co = True
        gp.set_seeds(i)
        gp.run(environment, gp_par, baseline=planner_baseline, hotstart=False)
        gp_par.boost_baseline = False

    for i in range(1, n_logs + 1):
        gp_par.log_name = 'block_planner_baseline_boost_all_' + str(22)
        gp_par.boost_baseline = True
        gp_par.boost_baseline_only_co = False
        gp.set_seeds(i)
        gp.run(environment, gp_par, baseline=planner_baseline, hotstart=False)
        gp_par.boost_baseline = False
        gp_par.boost_baseline_only_co = True

    for i in range(0):
        start = time.time()
        fitness = environment.get_fitness(planner_baseline)
        print(str(i) + " time: " + str(time.time() - start))
        print(fitness)

    world_interface.shutdown()
    paper_plots()
Пример #10
0
def test_croissant():
    """ Test croissant scenario """
    gp_par = gp.GpParameters()
    gp_par.ind_start_length = 8
    gp_par.n_population = 16
    gp_par.f_crossover = 0.5
    gp_par.n_offspring_crossover = 2
    gp_par.replace_crossover = False
    gp_par.f_mutation = 0.5
    gp_par.n_offspring_mutation = 2
    gp_par.parent_selection = gp.SelectionMethods.RANK
    gp_par.survivor_selection = gp.SelectionMethods.RANK
    gp_par.f_elites = 0.1
    gp_par.f_parents = gp_par.f_elites
    gp_par.mutate_co_offspring = False
    gp_par.mutate_co_parents = True
    gp_par.mutation_p_add = 0.4
    gp_par.mutation_p_delete = 0.3
    gp_par.allow_identical = False
    gp_par.plot = True
    gp_par.n_generations = 1000
    gp_par.verbose = False
    gp_par.fig_last_gen = False

    behavior_tree.load_settings_from_file(
        'duplo_state_machine/BT_SETTINGS_CROISSANT.yaml')
    planner_baseline = ['s(', 'f(', '0 at pos (0.0, 0.0, 0.0)?', \
                                    's(', 'f(', 'picked 0?', 'pick 0!', ')', 'place at (0.0, 0.0, 0.0)!', ')', ')', \
                        'f(', '1 at pos (0.0, 0.0, 0.0192)?', \
                              's(', 'f(', '1 on 0?', 's(', 'f(', 'picked 1?', 'pick 1!', ')', 'place on 0!', ')', ')', \
                                    'apply force 1!', ')', ')', \
                        'f(', '2 at pos (0.016, -0.032, 0.0)?', \
                              's(', 'f(', 'picked 2?', 'pick 2!', ')', 'place at (0.016, -0.032, 0.0)!', ')', ')', \
                        'f(', '3 at pos (0.016, 0.032, 0.0)?', \
                              's(', 'f(', 'picked 3?', 'pick 3!', ')', 'place at (0.016, 0.032, 0.0)!', ')', ')', ')']

    solved = ['s(', 'f(', '0 at pos (0.0, 0.0, 0.0)?', 's(', 'pick 0!', 'place at (0.0, 0.0, 0.0)!', ')', ')', \
                        'f(', '2 at pos (0.016, -0.032, 0.0)?', \
                              's(', 'pick 2!', 'place at (0.016, -0.032, 0.0)!', ')', ')', \
                        'f(', '3 at pos (0.016, 0.032, 0.0)?', \
                              's(', 'pick 3!', 'place at (0.016, 0.032, 0.0)!', ')', ')', \
                        'f(', '1 at pos (0.0, 0.0, 0.0192)?', \
                              's(', 'f(', '1 on 0?', 's(', 'pick 1!', 'place on 0!', ')', ')', \
                                    'apply force 1!', ')', ')', ')']

    start_positions = []
    start_positions.append(Pos(-0.05, -0.1, 0))
    start_positions.append(Pos(0, -0.1, 0))
    start_positions.append(Pos(0.05, -0.1, 0))
    start_positions.append(Pos(0.1, -0.1, 0))
    targets = []
    targets.append(Pos(0.0, 0.0, 0.0))
    targets.append(Pos(0.0, 0.0, 0.0192))
    targets.append(Pos(0.016, -0.032, 0.0))
    targets.append(Pos(0.016, 0.032, 0.0))
    environment = sm_environment.Environment(start_positions,
                                             targets,
                                             verbose=False,
                                             mode=sm.SMMode.CROISSANT)

    fitness = environment.get_fitness(planner_baseline)
    assert fitness < -100

    fitness = environment.get_fitness(solved)
    assert fitness > -3

    gp_par.log_name = 'test_croissant'
    _, _, best_fitness, _ = gp.run(environment,
                                   gp_par,
                                   baseline=planner_baseline)
    assert best_fitness[-1] > -4

    behavior_tree.load_settings_from_file(
        'behavior_tree_learning/tests/BT_TEST_SETTINGS.yaml')
Пример #11
0
def test_baselining():
    # pylint: disable=too-many-statements
    """ Tests various baseline setups in the blocking task """
    gp_par = gp.GpParameters()
    gp_par.ind_start_length = 8
    gp_par.n_population = 16
    gp_par.f_crossover = 0.5
    gp_par.n_offspring_crossover = 2
    gp_par.replace_crossover = False
    gp_par.f_mutation = 0.5
    gp_par.n_offspring_mutation = 2
    gp_par.parent_selection = gp.SelectionMethods.RANK
    gp_par.survivor_selection = gp.SelectionMethods.RANK
    gp_par.f_elites = 0.1
    gp_par.f_parents = gp_par.f_elites
    gp_par.mutate_co_offspring = False
    gp_par.mutate_co_parents = True
    gp_par.mutation_p_add = 0.4
    gp_par.mutation_p_delete = 0.3
    gp_par.allow_identical = False
    gp_par.plot = True
    gp_par.verbose = False
    gp_par.fig_last_gen = False

    behavior_tree.load_settings_from_file(
        'duplo_state_machine/BT_SETTINGS_BLOCKING.yaml')
    planner_baseline = ['s(', 'f(', '0 at pos (-0.1, 0.0, 0.0)?', 'put 0 at (-0.1, 0.0, 0.0)!', ')', \
                    'f(', '1 at pos (-0.1, 0.0, 0.0192)?', \
                          's(', 'f(', '1 on 0?', 'put 1 on 0!', ')', 'apply force 1!', ')', ')', \
                    'f(', '2 at pos (0.0, 0.0, 0.0)?', 'put 2 at (0.0, 0.0, 0.0)!', ')', ')']

    start_positions = []
    start_positions.append(sm.Pos(0.0, -0.05, 0.0))
    start_positions.append(sm.Pos(0.0, 0.05, 0.0))
    start_positions.append(sm.Pos(-0.1, 0.0, 0.0))

    targets = []
    targets.append(Pos(-0.1, 0.0, 0.0))
    targets.append(Pos(-0.1, 0.0, 0.0192))
    targets.append(Pos(0.0, 0.0, 0.0))

    environment = sm_environment.Environment(start_positions,
                                             targets,
                                             verbose=False,
                                             mode=sm.SMMode.BLOCKING)

    n_logs = 10
    gp_par.n_generations = 10
    best_list_baseline_no_keep = []
    gp_par.keep_baseline = False
    for i in range(1, n_logs + 1):
        gp_par.log_name = 'test_baseline_no_keep' + str(i)
        gp.set_seeds(i)
        _, _, best_fitness, _ = gp.run(environment,
                                       gp_par,
                                       baseline=planner_baseline)
        best_list_baseline_no_keep.append(best_fitness[-1])

    gp_par.n_generations = 200
    best_list_baseline = []
    for i in range(1, n_logs + 1):
        gp_par.log_name = 'test_baseline' + str(i)
        gp.set_seeds(i)
        _, _, best_fitness, _ = gp.run(environment,
                                       gp_par,
                                       baseline=planner_baseline)
        best_list_baseline.append(best_fitness[-1])

    best_list_baseline_boost = []
    gp_par.keep_baseline = True
    gp_par.boost_baseline = True
    gp_par.boost_baseline_only_co = False
    for i in range(1, n_logs + 1):
        gp_par.log_name = 'test_baseline_boost' + str(i)
        gp.set_seeds(i)
        _, _, best_fitness, _ = gp.run(environment,
                                       gp_par,
                                       baseline=planner_baseline)
        best_list_baseline_boost.append(best_fitness[-1])

    best_list_baseline_boost_only_co = []
    gp_par.keep_baseline = True
    gp_par.boost_baseline = True
    gp_par.boost_baseline_only_co = True
    for i in range(1, n_logs + 1):
        gp_par.log_name = 'test_baseline_boost_only_co' + str(i)
        gp.set_seeds(i)
        _, _, best_fitness, _ = gp.run(environment,
                                       gp_par,
                                       baseline=planner_baseline)
        best_list_baseline_boost_only_co.append(best_fitness[-1])

    print(best_list_baseline_no_keep)
    print(best_list_baseline)
    print(best_list_baseline_boost)
    print(best_list_baseline_boost_only_co)

    print(mean(best_list_baseline_no_keep))
    print(mean(best_list_baseline))
    print(mean(best_list_baseline_boost))
    print(mean(best_list_baseline_boost_only_co))

    assert mean(best_list_baseline_boost) > mean(best_list_baseline)
    assert mean(best_list_baseline_boost_only_co) > mean(
        best_list_baseline_boost)

    #Results after 500 gens run
    #[-51.6, -89.6, -12.2, -2.3, -1.0, -50.2, -51.6, -1.0, -1.0, -1.0]
    #[-12.2, -50.2, -10.8, -1.0, -1.0, -1.0, -2.2999999999999985, -0.9, -50.2, -89.6]
    #[-1.0, -1.0, -1.3, -1.0, -1.0, -1.0, -50.2, -1.0, -1.0, -50.199999999999996]
    #[-1.0, -1.0, -1.1, -1.0, -1.0, -1.2, -1.0, -10.8, -40.3, -1.0]
    #-26.15
    #-21.9
    #-10.87
    #-5.9

    behavior_tree.load_settings_from_file(
        'behavior_tree_learning/tests/BT_TEST_SETTINGS.yaml')
Пример #12
0
def test_blocking():
    """ Test scenario of shuffling bricks to avoid blocking """
    gp_par = gp.GpParameters()
    gp_par.ind_start_length = 8
    gp_par.n_population = 16
    gp_par.f_crossover = 0.5
    gp_par.n_offspring_crossover = 2
    gp_par.replace_crossover = False
    gp_par.f_mutation = 0.5
    gp_par.n_offspring_mutation = 2
    gp_par.parent_selection = gp.SelectionMethods.RANK
    gp_par.survivor_selection = gp.SelectionMethods.RANK
    gp_par.f_elites = 0.1
    gp_par.f_parents = gp_par.f_elites
    gp_par.mutate_co_offspring = False
    gp_par.mutate_co_parents = True
    gp_par.mutation_p_add = 0.4
    gp_par.mutation_p_delete = 0.3
    gp_par.allow_identical = False
    gp_par.plot = True
    gp_par.n_generations = 1000
    gp_par.verbose = False
    gp_par.fig_last_gen = False

    behavior_tree.load_settings_from_file(
        'duplo_state_machine/BT_SETTINGS_BLOCKING.yaml')
    planner_baseline = ['s(', 'f(', '0 at pos (-0.1, 0.0, 0.0)?', 'put 0 at (-0.1, 0.0, 0.0)!', ')', \
                    'f(', '1 at pos (-0.1, 0.0, 0.0192)?', \
                          's(', 'f(', '1 on 0?', 'put 1 on 0!', ')', 'apply force 1!', ')', ')', \
                    'f(', '2 at pos (0.0, 0.0, 0.0)?', 'put 2 at (0.0, 0.0, 0.0)!', ')', ')']

    solved = ['s(', 'put 1 on 0!', 'f(', '0 at pos (-0.1, 0.0, 0.0)?', 'put 2 at (0.0, 0.05, 0.0)!', ')', \
                        'put 0 at (-0.1, 0.0, 0.0)!', \
                        'f(', '1 at pos (-0.1, 0.0, 0.0192)?', 'apply force 1!', ')', \
                        'put 2 at (0.0, 0.0, 0.0)!', ')']

    start_positions = []
    start_positions.append(sm.Pos(0.0, -0.05, 0.0))
    start_positions.append(sm.Pos(0.0, 0.05, 0.0))
    start_positions.append(sm.Pos(-0.1, 0.0, 0.0))

    targets = []
    targets.append(Pos(-0.1, 0.0, 0.0))
    targets.append(Pos(-0.1, 0.0, 0.0192))
    targets.append(Pos(0.0, 0.0, 0.0))

    environment = sm_environment.Environment(start_positions,
                                             targets,
                                             verbose=False,
                                             mode=sm.SMMode.BLOCKING)
    fitness = environment.get_fitness(planner_baseline)
    assert fitness < -50

    fitness = environment.get_fitness(solved)
    assert fitness > -2

    gp_par.log_name = 'test_blocking'
    _, _, best_fitness, _ = gp.run(environment,
                                   gp_par,
                                   baseline=planner_baseline)
    assert best_fitness[-1] > -2

    behavior_tree.load_settings_from_file(
        'behavior_tree_learning/tests/BT_TEST_SETTINGS.yaml')
Пример #13
0
def test_duplo_croissant():
    """ Tests state machine for duplo croissant scenario """
    behavior_tree.load_settings_from_file(
        'duplo_state_machine/BT_SETTINGS_CROISSANT.yaml')
    start_positions = []
    start_positions.append(Pos(-0.05, -0.1, 0))
    start_positions.append(Pos(0, -0.1, 0))
    start_positions.append(Pos(0.05, -0.1, 0))
    start_positions.append(Pos(0.1, -0.1, 0))
    targets = []
    targets.append(Pos(0.0, 0.0, 0.0))
    targets.append(Pos(0.0, 0.0, 0.0192))
    targets.append(Pos(0.016, -0.032, 0.0))
    targets.append(Pos(0.016, 0.032, 0.0))
    environment = duplo_state_machine.environment.Environment(
        start_positions, targets)

    best = ['s(', 'f(', '0 at pos (0.0, 0.0, 0.0)?', 's(', 'pick 0!', 'place at (0.0, 0.0, 0.0)!', ')', ')', \
                  'f(', '2 at pos (0.016, -0.032, 0.0)?', 's(', 'pick 2!', 'place at (0.016, -0.032, 0.0)!', ')', ')', \
                  'f(', '3 at pos (0.016, 0.032, 0.0)?', 's(', 'pick 3!', 'place at (0.016, 0.032, 0.0)!', ')', ')', \
                  'f(', '1 at pos (0.0, 0.0, 0.0192)?', \
                        's(', 'f(', '1 on 0?', 's(', 'pick 1!', 'place on 0!', ')', ')', \
                              'apply force 1!', ')', ')', ')']

    print(environment.get_fitness(best))

    planned = ['s(', 'f(', '0 at pos (0.0, 0.0, 0.0)?', 's(', 'pick 0!', 'place at (0.0, 0.0, 0.0)!', ')', ')', \
                     'f(', '1 at pos (0.0, 0.0, 0.0192)?', \
                           's(', 'f(', '1 on 0?', 's(', 'pick 1!', 'place on 0!', ')', ')', \
                                 'apply force 1!', ')', ')', \
                     'f(', '2 at pos (0.016, -0.032, 0.0)?', \
                           's(', 'pick 2!', 'place at (0.016, -0.032, 0.0)!', ')', ')', \
                     'f(', '3 at pos (0.016, 0.032, 0.0)?', \
                           's(', 'pick 3!', 'place at (0.016, 0.032, 0.0)!', ')', ')', ')']

    planned = ['s(', 'f(', '0 at pos (0.0, 0.0, 0.0)?', \
                           's(', 'f(', 'picked 0?', 'pick 0!', ')', 'place at (0.0, 0.0, 0.0)!', ')', ')', \
                     'f(', '1 at pos (0.0, 0.0, 0.0192)?', \
                           's(', 'f(', '1 on 0?', 's(', 'f(', 'picked 1?', 'pick 1!', ')', 'place on 0!', ')', ')', \
                                 'apply force 1!', ')', ')', \
                     'f(', '2 at pos (0.016, -0.032, 0.0)?', \
                           's(', 'f(', 'picked 2?', 'pick 2!', ')', 'place at (0.016, -0.032, 0.0)!', ')', ')', \
                     'f(', '3 at pos (0.016, 0.032, 0.0)?', \
                           's(', 'f(', 'picked 3?', 'pick 3!', ')', 'place at (0.016, 0.032, 0.0)!', ')', ')', ')']

    print(environment.get_fitness(planned))

    gp_par = gp.GpParameters()
    gp_par.ind_start_length = 8
    gp_par.n_population = 16
    gp_par.f_crossover = 0.5
    gp_par.n_offspring_crossover = 2
    gp_par.f_mutation = 0.5
    gp_par.n_offspring_mutation = 2
    gp_par.parent_selection = gp.SelectionMethods.RANK
    gp_par.survivor_selection = gp.SelectionMethods.RANK
    gp_par.f_elites = 0.1
    gp_par.f_parents = 1
    gp_par.mutate_co_offspring = False
    gp_par.mutate_co_parents = True
    gp_par.mutation_p_add = 0.4
    gp_par.mutation_p_delete = 0.3
    gp_par.allow_identical = False
    gp_par.plot = True
    gp_par.n_generations = 50
    gp_par.verbose = False
    gp_par.fig_last_gen = False

    n_logs = 3
    for i in range(1, n_logs + 1):
        gp_par.log_name = 'croissant_baseline_sm_' + str(i)
        gp.set_seeds(i)
        gp.run(environment, gp_par, baseline=planned)