Beispiel #1
0
 def __init__(self, params=None, strategy=None):
     super().__init__(params)
     self.strategy = strategy if strategy else [
         rand_bound(0,
                    (self.BOUNDS[i][1] - CitizenStrat.BOUNDS[i][0]) * 0.05)
         for i in range(self.P_SIZE)
     ]
Beispiel #2
0
 def __init__(self):
     super().__init__()
     self.velocity = [rand_bound(self.VEL_BOUNDS[i][0],
                                 self.VEL_BOUNDS[i][1])
                      for i in range(self.P_SIZE)]
     self.best_cost = self.error
     self.best_pos = self.parameters.copy()
Beispiel #3
0
 def set_value(self, param, value=None):
     if value:
         if value < self.BOUNDS[param][0]:
             value = self.BOUNDS[param][0]
         if value > self.BOUNDS[param][1]:
             value = self.BOUNDS[param][1]
         self.parameters[param] = value
     else:
         self.parameters[param] = rand_bound(*self.BOUNDS[param])
Beispiel #4
0
 def create_harmony(self):
     harmony = CitizenHar()
     for i in range(CitizenHar.P_SIZE):
         if rand() < self.consid_r:
             pos = randint_bound(self.pop_size)
             value = self.population[pos].get_value(i)
             if rand() < self.adjust_r:
                 value = value + self.ran * rand_bound(-1, 1)
             harmony.set_value(i, value)
         else:
             harmony.set_value(i)
     return harmony
Beispiel #5
0
 def take_step(self, current, step_size):
     position = Citizen()
     for i in range(Citizen.P_SIZE):
         minimum = min(
             [self.bounds[i][0],
              current.get_value(i) - step_size[i]])
         maximum = max(
             [self.bounds[i][1],
              current.get_value(i) + step_size[i]])
         value = rand_bound(minimum, maximum)
         position.set_value(i, value)
     return position
Beispiel #6
0
 def generate_sample(self):
     sample = Citizen()
     for i in range(Citizen.P_SIZE):
         aux = rand_bound(self.means[i], self.stdevs[i])
         sample.set_value(i, aux)
     return sample
Beispiel #7
0
 def create_means(self):
     return [rand_bound(*Citizen.BOUNDS[i]) for i in range(Citizen.P_SIZE)]
Beispiel #8
0
 def __init__(self, params=None):
     self.parameters = params.copy() if params \
         else [rand_bound(*self.BOUNDS[i]) for i in range(self.P_SIZE)]
     self.error = np.inf