示例#1
0
    def objective(trial):
        # Start time needs to be global due to hidden optimization costs
        global start_time

        # Organize all optuna suggestions
        proposed_team = []
        for i in range(10):
            proposed_team.append([
                trial.suggest_int('x' + str(i), adversary.x_min,
                                  adversary.x_max),
                trial.suggest_uniform('y' + str(i), 0, 100)
            ])

        # Calculating fitness
        fitness = adversary.calculate_heuristic(
            meval.create_team(proposed_team))

        # Save results for post-hoc analysis
        register['proposal'].append(proposed_team)
        register['fitness'].append(fitness)
        register['cycle_time'].append(time() - start_time)

        # Starting clock after registering to count for hidden optimization costs
        start_time = time()

        # Return fitness for optimization
        return fitness
示例#2
0
def calculate_sucessor_fitnesses(adversary, successors):
    # Calculating the fitness for all successors
    successor_fitnesses = []
    for successor in successors:
        successor_fitnesses.append(adversary.calculate_heuristic(meval.create_team(successor)))

    # Returns list with the fitness of all successors
    return successor_fitnesses
def evaluate(population):
    # Calculate fitness for all individuals
    population_fitnesses = []
    for individual in population:
        population_fitnesses.append(
            adversary.calculate_heuristic(meval.create_team(individual)))

    # Returns list with all population fitnesses
    return population_fitnesses
示例#4
0
def plot_team(team, save_fig_dir='', dpi=120):
    adversary = meval.default_adversary_1
    team = [
        float(i) for i in team.replace('[', '').replace(']', '').replace(
            ' ', '').split(',')
    ]
    aux = []
    for i in range(10):
        aux.append([team[2 * i], team[2 * i + 1]])
    team = meval.create_team(aux)

    adversary.plot_result(team, save_fig_dir=save_fig_dir, dpi=dpi)
def hillclimb():
    # Create register to save information about the run
    register = {
        'iteration': [],
        'proposal': [],
        'fitness': [],
        'cycle_time': []
    }

    # Starting from a random position
    proposed_team = [[52, 96], [53, 67], [52, 32], [51, 4], [31, 5], [32, 31],
                     [33, 65], [32, 96], [18, 66], [17, 32]]
    best_fitness = adversary.calculate_heuristic(
        meval.create_team(proposed_team))

    # Defining step related variables
    step = 10
    step_decrease_cycle = 125
    max_steps_at_current_step = 250
    counter = 0
    internal_counter = 0

    # Search for a better solution until:
    # No better solution can be found and step cannot be lowered
    while 1:
        counter += 1

        # Reset timer
        start_time = time()

        # Halve step every *step_decrease_cycle* iterations
        internal_counter += 1
        if internal_counter % step_decrease_cycle == 0:
            internal_counter = 0
            step = int(step / 2)

        # Find the best sucessor
        successors = list_successors(proposed_team, step)
        successor_fitnesses = calculate_sucessor_fitnesses(
            adversary, successors)
        proposed_team, fitness = find_best(successors, successor_fitnesses)

        # If fitness did not improve
        if fitness <= best_fitness:
            # If step is already one, end optimization
            if step == 1:
                break

            # If we can decrease the step, do it before ending
            else:
                internal_counter = 0
                step = int(step / 2)

        # If fitness improved
        else:
            # If the hill climb is stuck at the minimum temperature, break
            if internal_counter > max_steps_at_current_step:
                break

            # Else, save the result and continue
            best_fitness = fitness

        # Save results for post-hoc analysis
        for successor, successor_fitness in zip(successors,
                                                successor_fitnesses):
            register['iteration'].append(counter)
            register['proposal'].append(successor)
            register['fitness'].append(successor_fitness)
            register['cycle_time'].append(time() - start_time)

    # Export registers to CSV
    current_time = asctime().replace(':', '_').split(' ')
    export_time = f'{current_time[1]}_{current_time[2]}_{current_time[3]}'
    pd.DataFrame(register).to_csv(
        f'results/015_generalize_{adv}_{export_time}.csv', index=False)
示例#6
0
n_runs = 20000

# Run algorithm multiple times
for _ in tqdm(range(20)):
    # Create register to save information about the run
    register = {'proposal': [], 'fitness': [], 'cycle_time': []}

    # Perform n_runs
    for _ in range(n_runs):
        # Starts timer
        start_time = time()

        # Generating a random team
        proposed_team = meval.generate_random_start(adversary.x_min,
                                                    adversary.x_max)

        # Evaluating randomly created team
        fitness = adversary.calculate_heuristic(
            meval.create_team(proposed_team))

        # Save results for post-hoc analysis
        register['proposal'].append(proposed_team)
        register['fitness'].append(fitness)
        register['cycle_time'].append(time() - start_time)

    # Export registers to CSV
    current_time = asctime().replace(':', '_').split(' ')
    export_time = f'{current_time[1]}_{current_time[2]}_{current_time[3]}'
    pd.DataFrame(register).to_csv(f'results/010_randsearch_{export_time}.csv',
                                  index=False)
示例#7
0
def simulatedannealing():
    # Create register to save information about the run
    register = {
        'iteration': [],
        'proposal': [],
        'fitness': [],
        'cycle_time': [],
        'temperature': []
    }

    # Starting from a random position
    proposed_team = meval.generate_random_start(adversary.x_min,
                                                adversary.x_max)
    fitness = adversary.calculate_heuristic(meval.create_team(proposed_team))

    # Defining step related variables
    step = 10
    step_decrease_cycle = 125
    max_steps_at_min_temperature = 250
    counter = 0

    # Initializing variables
    temperature = 1
    best_fitness = fitness
    probability_of_acceptance = 0
    counter = 0
    internal_counter = 0
    counter_min_temp = 0
    start_time = time()

    # Search for a better solution until:
    # Temperature < minimum_temperature and no better solution can be found
    while 1:
        counter += 1

        # Reset timer
        start_time = time()

        # Update temperature
        temperature *= temperature_multiplier

        # Halve step every *step_decrease_cycle* iterations
        internal_counter += 1
        if internal_counter % step_decrease_cycle == 0:
            step = int(step / 2)

        # Find the best sucessor
        successors = list_successors(proposed_team)
        successor_fitnesses = calculate_sucessor_fitnesses(
            adversary, successors)
        proposed_team, fitness = find_best(successors, successor_fitnesses)

        # Check for end clauses
        if temperature < minimum_temperature:
            # Break if no improvement is possible
            if best_fitness >= fitness:
                break

            # Break if there are too many steps at minimum temperature
            counter_min_temp += 1
            if counter_min_temp > max_steps_at_min_temperature:
                break

        # Check if temperature enables randomization
        elif temperature > random():
            # Generate and evaluate random proposal
            random_proposal = meval.generate_random_start(
                adversary.x_min, adversary.x_max)
            random_proposal_heuristic = adversary.calculate_heuristic(
                meval.create_team(random_proposal))

            # Calculate probability of acceptance (note: sigmoid not good)
            probability_of_acceptance = ((np.arctan(
                (random_proposal_heuristic - fitness) * 100) + np.pi / 2) /
                                         np.pi) * temperature

            # If proposal is accepted, update new proposal and reset step
            if probability_of_acceptance > random():
                proposed_team = random_proposal
                fitness = random_proposal_heuristic
                step = 10
            continue

        # If solution cannot improved, decrease step, else generate a new random point
        if best_fitness >= fitness:
            # If step is already one, end optimization
            if step == 1:
                proposed_team = meval.generate_random_start(
                    adversary.x_min, adversary.x_max)

            # If we can decrease the step, do it before ending
            else:
                internal_counter = 0
                step = int(step / 2)

        # If solution improved, register new best fitness
        elif fitness > best_fitness:
            best_fitness = fitness

        # Save results for post-hoc analysis
        for successor, successor_fitness in zip(successors,
                                                successor_fitnesses):
            register['iteration'].append(counter)
            register['proposal'].append(successor)
            register['fitness'].append(successor_fitness)
            register['cycle_time'].append(time() - start_time)
            register['temperature'].append(temperature)

    # Export registers to CSV
    current_time = asctime().replace(':', '_').split(' ')
    export_time = f'{current_time[1]}_{current_time[2]}_{current_time[3]}'
    pd.DataFrame(register).to_csv(f'results/013_simanneal_{export_time}.csv',
                                  index=False)