forked from GuilhermeCaeiro/trabmh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialization.py
More file actions
130 lines (107 loc) · 5.07 KB
/
initialization.py
File metadata and controls
130 lines (107 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import numpy as np
import random
from individual import Individual
from search import Search
from utils import Utils
from mutation import Mutation
import time
import pdb
from copy import deepcopy
class Initialization:
def __init__(self):
pass
@classmethod
def binary_random(self, environment, population_size): # uniform random
chromosomes = []
for i in range(0, population_size):
chromosome = np.zeros((environment.n, 1))
chromosome[random.sample(range(0, environment.n), k = environment.s)] = 1
chromosomes.append(chromosome)
return chromosomes
@classmethod
def binary_biased(self, environment, population_size, min_diff):
min_diff = float(min_diff)
chromosomes = []
while len(chromosomes) < population_size:
chromosome = np.zeros((environment.n, 1))
chromosome[random.sample(range(0, environment.n), k = environment.s)] = 1
found_close = False
for existing_chromosome in chromosomes:
if np.mean(np.abs(existing_chromosome - chromosome)) < min_diff:
found_close = True
break
if not found_close:
chromosomes.append(chromosome)
return chromosomes
@classmethod
def binary_biasedweighted(self, environment, population_size): # can it be called uniform?
chromosomes = []
chromosome = np.zeros((environment.n, 1))
chromosome[random.sample(range(0, environment.n), k = environment.s)] = 1
chromosomes.append(chromosome)
while len(chromosomes) < population_size:
sum = 1/(np.sum(chromosomes, axis=0)+1)
p = sum.T[0]/sum.sum()
chromosome = np.zeros((environment.n, 1))
chromosome[np.random.choice(range(0, environment.n), p=p, size=environment.s, replace=False)] = 1
chromosomes.append(chromosome)
return chromosomes
@classmethod
def binary_heuristics(self, environment, population_size):
chromosomes = Search.heuristic_solutions(environment)
new_chromosomes = []
remaining_chromosomes = population_size - len(chromosomes)
for i in range(0, remaining_chromosomes):
index = random.choice(range(0, len(chromosomes)))
chosen_chromosome = chromosomes[index]
new_chromosome = Mutation.mutate(chosen_chromosome, environment)
new_chromosomes.append(new_chromosome)
return chromosomes + new_chromosomes
@classmethod
def permutation_random(self, environment, population_size):
chromosomes = self.binary_random(environment, population_size)
return Utils.convert_chromosomes_from_binary_to_permutation(chromosomes)
@classmethod
def permutation_biased(self, environment, population_size, min_diff):
chromosomes = self.binary_biased(environment, population_size, min_diff)
return Utils.convert_chromosomes_from_binary_to_permutation(chromosomes)
@classmethod
def permutation_biasedweighted(self, environment, population_size):
chromosomes = self.binary_biasedweighted(environment, population_size)
return Utils.convert_chromosomes_from_binary_to_permutation(chromosomes)
@classmethod
def permutation_heuristics(self, environment, population_size):
chromosomes = Search.heuristic_solutions(environment)
chromosomes = Utils.convert_chromosomes_from_binary_to_permutation(chromosomes)
new_chromosomes = []
remaining_chromosomes = population_size - len(chromosomes)
for i in range(0, remaining_chromosomes):
index = random.choice(range(0, len(chromosomes)))
chosen_chromosome = chromosomes[index]
new_chromosome = Mutation.mutate(chosen_chromosome, environment)
new_chromosomes.append(new_chromosome)
return chromosomes + new_chromosomes
@classmethod
def initialize_population(self, environment, population_size):
function_and_params = environment.initialization_method.split("_")
function_name = function_and_params[0]
params = function_and_params[1:] if len(function_and_params) > 0 else []
function_name = environment.encoding + "_" + function_name
if hasattr(self, function_name) and callable(getattr(self, function_name)):
func = getattr(self, function_name)
population_chromosomes = func(environment, population_size, *params)
population = []
best_sol = - np.inf
best_individual = None
for chromosome in population_chromosomes:
individual = Individual(
chromosome,
environment
)
if individual.fitness > best_sol:
best_sol = individual.fitness
best_individual = individual
population.append(individual)
return population, best_sol, best_individual
else:
raise Exception("Method \"{}\" not found.".format(function_name))