Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
finishes multiobjective implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
brenomfviana committed May 4, 2020
1 parent 3c5fa38 commit bb692f7
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 55 deletions.
10 changes: 5 additions & 5 deletions coursework/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
# Problems
problems = [
# Binary
# 'onemax', 'trap5', 'invtrap5',
'onemax', 'trap5', 'invtrap5',
# Real
'sphere', 'rosen',
# Multi binary
# 'multibi',
'multibi',
# Multi real
# 'multirn'
'multirn'
]

# Problem sizes
problem_sizes = [10, 20, 40, ]# 80, 160]
problem_sizes = [10, 20, 40, 80, ]# 160]

# Population sizes
population_sizes = [10, 20, 40, ]# 80, 160]
population_sizes = [10, 20, 40, 80, ]# 160]

# Run algorithms
for posz in population_sizes:
Expand Down
51 changes: 37 additions & 14 deletions coursework/src/cga.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
from .ff import ERR
from . import sort, utils

def best_individual(self, multi=False):
if multi:
return [(a.fitness, a.genes) for a in self.current_generation[0]]
else:
best = self.current_generation[0]
return (best.fitness, best.genes)

# Run cGA for binary problems
def bn_run(self, hmdata, maximise=True, multi=False):
def bn_run(self, hmdata, multi=False):
# Initialize probability vector
prob = [0.5] * len(self.seed_data)
# Initialize best solution
Expand All @@ -35,14 +42,15 @@ def bn_run(self, hmdata, maximise=True, multi=False):
# Update best individuals population
population.append(a)
population.append(b)
population.sort(key=sort.get_key, reverse=maximise)
population.sort(key=sort.get_key, reverse=self.maximise_fitness)
population = population[:self.population_size]
#
# Get the best and worst individual
if multi:
winner, loser = multi_compete(a, b, maximise)
winner, loser = multi_compete(a, b, self.maximise_fitness)
population = sort.nsgaii(population, self.maximise_fitness)
else:
winner, loser = mono_compete(a, b, maximise)
winner, loser = mono_compete(a, b, self.maximise_fitness)
# Update best solution
if best:
if winner.fitness > best.fitness:
Expand All @@ -65,13 +73,20 @@ def bn_run(self, hmdata, maximise=True, multi=False):
# Update the probability vector based on the success of each bit
bn_update_prob(winner.genes, loser.genes, prob, self.population_size)
#
# Add final solution
self.current_generation.append(best)
#
# Final Covariance Matrix
arrs = [np.transpose(i.genes) for i in population]
hmdata['f'] = np.cov(arrs)

#
# Add final solution
if multi:
indexes = sort.nsgaii_select(population, self.maximise_fitness)
nondominated = []
for i in indexes:
nondominated.append(population[i])
self.current_generation.clear()
self.current_generation.append(nondominated)
else:
self.current_generation.append(best)

# Update probability vector (for monobjective approach)
def bn_update_prob(winner, loser, prob, popsize):
Expand Down Expand Up @@ -157,9 +172,9 @@ def rn_run(self, hmdata, maximise=False, multi=False):
#
# Update best individuals population
if multi:
population = sort.nsgaii(population, maximise)
population = sort.nsgaii(population, self.maximise_fitness)
else:
population.sort(key=sort.get_key, reverse=maximise)
population.sort(key=sort.get_key, reverse=self.maximise_fitness)
population = population[:self.population_size]
elite = population[:k]
best = population[0]
Expand All @@ -179,16 +194,24 @@ def rn_run(self, hmdata, maximise=False, multi=False):
# Update the probability vector based on the success of each bit
rn_update_prob(elite, prob)
#
# Add final solution
self.current_generation.append(best)
#
# Update best individuals population
population.sort(key=sort.get_key, reverse=maximise)
population.sort(key=sort.get_key, reverse=self.maximise_fitness)
population = population[:self.population_size]
#
# Final Covariance Matrix
arrs = [np.transpose(i.genes) for i in population]
hmdata['f'] = np.cov(arrs)
#
# Add final solution
if multi:
indexes = sort.nsgaii_select(population, self.maximise_fitness)
nondominated = []
for i in indexes:
nondominated.append(population[i])
self.current_generation.clear()
self.current_generation.append(nondominated)
else:
self.current_generation.append(best)


# Generate probability vector
Expand Down
2 changes: 1 addition & 1 deletion coursework/src/ff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import numpy as np

ERR = 10e-5
ERR = 10e-3

def onemax(array, data=None):
return sum(array)
Expand Down
109 changes: 75 additions & 34 deletions coursework/src/ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ def mono_binary(function, goal_function, size, popsz, noe, maximise=True):
hmdata = {}
# Set sGA
ga = pyeasyga.GeneticAlgorithm(data, population_size=popsz,
maximise_fitness=maximise)
maximise_fitness=maximise, generations=10)
ga.fitness_function = function
# Update default functions
ga.create_first_generation = sga.create_first_generation
ga.create_next_generation = sga.create_next_generation
ga.best_individual = sga.best_individual
ga.rank_population = sga.rank_population
ga.calculate_population_fitness = sga.bn_calculate_population_fitness
ga.run = sga.bn_run
# Run binary sGA
ga.run(ga, hmdata)
# Get best individual
fitness, _ = ga.best_individual()
fitness, _ = ga.best_individual(ga)
# Update extraction variables
results_sga.append(fitness)
hmdatas_sga.append(hmdata)
Expand All @@ -38,14 +40,16 @@ def mono_binary(function, goal_function, size, popsz, noe, maximise=True):
hmdata = {}
# Set cGA
ga = pyeasyga.GeneticAlgorithm(data, population_size=popsz,
maximise_fitness=maximise)
maximise_fitness=maximise, generations=10)
ga.fitness_function = function
# Update default functions
ga.best_individual = cga.best_individual
ga.create_individual = cga.bn_create_individual
ga.run = cga.bn_run
# Run binary cGA
ga.run(ga, hmdata)
# Get best individual
fitness, _ = ga.best_individual()
fitness, _ = ga.best_individual(ga)
# Update extraction variables
results_cga.append(fitness)
hmdatas_cga.append(hmdata)
Expand Down Expand Up @@ -75,11 +79,12 @@ def mono_real(function, goal_function, size, popsz, noe, maximise=False):
hmdata = {}
# Set sGA
ga = pyeasyga.GeneticAlgorithm(data, population_size=popsz,
maximise_fitness=maximise)
maximise_fitness=maximise, generations=10)
ga.fitness_function = function
# Update default functions
ga.create_first_generation = sga.create_first_generation
ga.create_next_generation = sga.create_next_generation
ga.best_individual = sga.best_individual
ga.rank_population = sga.rank_population
ga.create_individual = sga.rn_create_individual
ga.mutate_function = sga.rn_mutate_function
Expand All @@ -88,7 +93,7 @@ def mono_real(function, goal_function, size, popsz, noe, maximise=False):
# Run real sGA
ga.run(ga, hmdata)
# Get best individual
fitness, _ = ga.best_individual()
fitness, _ = ga.best_individual(ga)
# Update extraction variables
results_sga.append(fitness)
hmdatas_sga.append(hmdata)
Expand All @@ -102,14 +107,16 @@ def mono_real(function, goal_function, size, popsz, noe, maximise=False):
hmdata = {}
# Set cGA
ga = pyeasyga.GeneticAlgorithm(data, population_size=popsz,
maximise_fitness=maximise)
maximise_fitness=maximise, generations=10)
ga.fitness_function = function
# Update default functions
ga.best_individual = cga.best_individual
ga.create_individual = cga.rn_create_individual
ga.run = cga.rn_run
# Run real cGA
ga.run(ga, hmdata)
# Get best individual
fitness, _ = ga.best_individual()
fitness, _ = ga.best_individual(ga)
# Update extraction variables
results_cga.append(fitness)
hmdatas_cga.append(hmdata)
Expand All @@ -131,59 +138,75 @@ def multi_binary(function, goal_function, size, popsz, noe, maximise=True):
data = [0] * size
#
# Save the fitness of each execution
results_sga = []
results_sga_fit_a = []
results_sga_fit_b = []
hmdatas_sga = []
# Execute the sGA `noe` times (noe: number of executions)
for _ in range(noe):
# Heat map data
hmdata = {}
# Set sGA
ga = pyeasyga.GeneticAlgorithm(data, population_size=popsz,
maximise_fitness=maximise)
maximise_fitness=maximise, generations=10)
ga.fitness_function = function
# Update default functions
ga.create_first_generation = sga.create_first_generation
ga.create_next_generation = sga.create_next_generation
ga.best_individual = sga.best_individual
ga.rank_population = sga.rank_population
ga.calculate_population_fitness = sga.bn_calculate_population_fitness
ga.run = sga.bn_run
# Run binary sGA
ga.run(ga, hmdata, multi=True)
# Get best individual
fitness, _ = ga.best_individual()
# Update extraction variables
results_sga.append(fitness)
nondominated = ga.best_individual(ga, multi=True)
for nd in nondominated:
fitness, _ = nd
fit_a, fit_b = fitness
# Update extraction variables
results_sga_fit_a.append(fit_a)
results_sga_fit_b.append(fit_b)
print('bin', 'sga', fit_a, fit_b)
hmdatas_sga.append(hmdata)
#
# Save the fitness of each execution
results_cga = []
results_cga_fit_a = []
results_cga_fit_b = []
hmdatas_cga = []
# Execute the cGA `noe` times (noe: number of executions)
for _ in range(noe):
# Heat map data
hmdata = {}
# Set cGA
ga = pyeasyga.GeneticAlgorithm(data, population_size=popsz,
maximise_fitness=maximise)
maximise_fitness=maximise, generations=10)
ga.fitness_function = function
# Update default functions
ga.best_individual = cga.best_individual
ga.create_individual = cga.bn_create_individual
ga.run = cga.bn_run
# Run binary cGA
ga.run(ga, hmdata, multi=True)
# Get best individual
fitness, _ = ga.best_individual()
# Update extraction variables
results_cga.append(fitness)
nondominated = ga.best_individual(ga, multi=True)
for nd in nondominated:
fitness, _ = nd
fit_a, fit_b = fitness
# Update extraction variables
results_cga_fit_a.append(fit_a)
results_cga_fit_b.append(fit_b)
print('bin', 'cga', fit_a, fit_b)
hmdatas_cga.append(hmdata)
#
# Get goal of the fitness function
fa_goal, fb_goal = goal_function
goal_a, goal_b = fa_goal(data), fb_goal(data)
#
# Plot hyperplane charts
# Plot hypervolume charts
fa, fb = function
fname = fa.__name__ + '_' + fb.__name__ + '_'
filename = fname + str(size) + '_' + str(popsz)
# TODO
#
# # Plot heat map charts
# for i, _ in enumerate(hmdatas_sga):
# charts.heat_map(hmdatas_sga[i], hmdatas_cga[i], filename, i + 1)
Expand All @@ -195,19 +218,21 @@ def multi_real(function, goal_function, size, popsz, noe, maximise=False):
data = [0] * size
#
# Save the fitness of each execution
results_sga = []
results_sga_fit_a = []
results_sga_fit_b = []
hmdatas_sga = []
# Execute the sGA `noe` times (noe: number of executions)
for _ in range(noe):
# Heat map data
hmdata = {}
# Set sGA
ga = pyeasyga.GeneticAlgorithm(data, population_size=popsz,
maximise_fitness=maximise)
maximise_fitness=maximise, generations=10)
ga.fitness_function = function
# Update default functions
ga.create_first_generation = sga.create_first_generation
ga.create_next_generation = sga.create_next_generation
ga.best_individual = sga.best_individual
ga.rank_population = sga.rank_population
ga.create_individual = sga.rn_create_individual
ga.mutate_function = sga.rn_mutate_function
Expand All @@ -216,38 +241,54 @@ def multi_real(function, goal_function, size, popsz, noe, maximise=False):
# Run real sGA
ga.run(ga, hmdata, multi=True)
# Get best individual
fitness, _ = ga.best_individual()
# Update extraction variables
results_sga.append(fitness)
nondominated = ga.best_individual(ga, multi=True)
for nd in nondominated:
fitness, _ = nd
fit_a, fit_b = fitness
# Update extraction variables
results_sga_fit_a.append(fit_a)
results_sga_fit_b.append(fit_b)
print('real', 'sga', fit_a, fit_b)
hmdatas_sga.append(hmdata)
#
# Save the fitness of each execution
results_cga = []
results_cga_fit_a = []
results_cga_fit_b = []
hmdatas_cga = []
# Execute the cGA `noe` times (noe: number of executions)
for _ in range(noe):
# Heat map data
hmdata = {}
# Set cGA
ga = pyeasyga.GeneticAlgorithm(data, population_size=popsz,
maximise_fitness=maximise)
maximise_fitness=maximise, generations=10)
ga.fitness_function = function
# Update default functions
ga.best_individual = cga.best_individual
ga.create_individual = cga.rn_create_individual
ga.run = cga.rn_run
# Run real cGA
ga.run(ga, hmdata, multi=True)
# Get best individual
fitness, _ = ga.best_individual()
# Update extraction variables
results_cga.append(fitness)
nondominated = ga.best_individual(ga, multi=True)
for nd in nondominated:
fitness, _ = nd
fit_a, fit_b = fitness
# Update extraction variables
results_cga_fit_a.append(fit_a)
results_cga_fit_b.append(fit_b)
print('real', 'cga', fit_a, fit_b)
hmdatas_cga.append(hmdata)
#
# Get goal of the fitness function
goal = goal_function(data)
fa_goal, fb_goal = goal_function
goal_a, goal_b = fa_goal(data), fb_goal(data)
#
# Plot hypervolume charts
fa, fb = function
fname = fa.__name__ + '_' + fb.__name__ + '_'
filename = fname + str(size) + '_' + str(popsz)
#
# Plot hyperplane charts
filename = function.__name__ + '_' + str(size) + '_' + str(popsz)
# TODO
# # Plot heat map charts
# for i, _ in enumerate(hmdatas_sga):
# charts.heat_map(hmdatas_sga[i], hmdatas_cga[i], filename, i + 1)
Loading

0 comments on commit bb692f7

Please sign in to comment.