Exemple #1
0
 def test_rebound(self):
     """Rebounding from walls of intervals."""
     for _ in range(100):
         x1 = random_in(-100, 100)
         x2 = random_in(-100, 100)
         the_min = min(x1, x2)
         the_max = max(x1, x2)
         v = random_in(the_min, the_max)
         delta = random_in(0, the_max - the_min)
         r = add_and_rebound(v, delta, the_min, the_max)
         self.assertTrue((r >= the_min) and (r <= the_max))
Exemple #2
0
    def mutate_ints2(self, situation):
        def get_mutation_value(m: float, encoder: EncoderDecoder) -> float:
            return (-1 if random() < 0.5 else 1) * \
                   random_in(0, m) * (encoder.extremes[1] - encoder.extremes[0])

        m = 0.1
        center_spread_list = []
        for (center, spread), value, encoder in zip(self, situation,
                                                    self.real_translators):
            tries_left = 1  # 3
            mutation_valid = False
            while (tries_left > 0) and not mutation_valid:
                if random() <= .5:  # TODO self.mutation_prob:
                    # do 'center' mutation:
                    min_value_d = encoder.min_distance_to_ends(value)
                    min_delta = -min_value_d - center + value
                    max_delta = min(
                        min_value_d - center + value,
                        (encoder.extremes[1] - 2 * center + value) / 2)
                    unclipped_center = center + random_in(min_delta, max_delta)
                    unclipped_spread = random_in(
                        abs(unclipped_center - value),
                        encoder.min_distance_to_ends(unclipped_center))
                else:  # no mutation of the center
                    unclipped_center = center
                    if random() <= .5:  # TODO self.mutation_prob:
                        unclipped_spread = random_in(
                            abs(unclipped_center - value),
                            encoder.min_distance_to_ends(unclipped_center))
                    else:  # no mutation of the spread
                        unclipped_spread = spread
                new_center, new_spread = self.clip_center_spread(
                    (unclipped_center, unclipped_spread), encoder)
                # update termination condition
                tries_left -= 1
                mutation_valid = (value >= new_center - new_spread) and (
                    value <= new_center + new_spread)
            if not mutation_valid:
                # stay:
                new_center, new_spread = center, spread
            center_spread_list.append((new_center, new_spread))
        return BitConditionRealEncoding(
            encoders=self.real_translators,
            center_spreads=center_spread_list,
            mutation_strength=self.mutation_strength,
            mutation_prob=self.mutation_prob)
Exemple #3
0
 def _mutate_interval_by_stretching(self, interval: Tuple[float, float],
                                    value: float) -> Tuple[float, float]:
     center, spread = interval
     # let's do a shrinking\stretching of the interval
     if spread == 0 and ((center == self.real_translators.extremes[0]) or
                         (center == self.real_translators.extremes[1])):
         # there is nothing else to do:
         new_center, new_spread = center, spread
     else:
         # Stretching can't make the interval too large. So:
         max_spread = min(center - self.real_translators.extremes[0],
                          self.real_translators.extremes[1] - center)
         min_spread = abs(center - value)
         new_spread = random_in(min_spread, max_spread)
         while new_spread == spread:  # I actually want to mutate!
             new_spread = random_in(min_spread, max_spread)
         new_center = center
     return new_center, new_spread
Exemple #4
0
    def mutate_as_in_paper(self, situation):
        def get_mutation_value(m: float, encoder: EncoderDecoder) -> float:
            return (-1 if random() < 0.5 else 1) * \
                   random_in(0, m) * (encoder.extremes[1] - encoder.extremes[0])

        m = 0.1
        center_spread_list = []
        for (center, spread), value, encoder in zip(self, situation,
                                                    self.real_translators):
            tries_left = 1  # 3
            mutation_valid = False
            while (tries_left > 0) and not mutation_valid:
                if random() <= .5:  # TODO self.mutation_prob:
                    # do 'center' mutation:
                    delta = get_mutation_value(m, encoder)
                    unclipped_center = add_and_rebound(center,
                                                       delta,
                                                       m=encoder.extremes[0],
                                                       M=encoder.extremes[1])
                    unclipped_spread = random_in(
                        0,
                        min(unclipped_center - encoder.extremes[0],
                            encoder.extremes[1] - unclipped_center))
                    # unclipped_center = encoder.clip(center + get_mutation_value(m, encoder))
                else:  # no mutation of the center
                    unclipped_center = center
                    if random() <= .5:  # TODO self.mutation_prob:
                        delta = get_mutation_value(m, encoder)
                        unclipped_spread = add_and_rebound(
                            spread,
                            delta,
                            m=0,
                            M=encoder.extremes[1] - encoder.extremes[0])
                        # unclipped_spread = encoder.clip(spread + get_mutation_value(m, encoder))  # TODO: is this a bug?? Shouldn't 'spread, be in [0, M - m] ?
                    else:  # no mutation of the spread
                        unclipped_spread = spread
                new_center, new_spread = self.clip_center_spread(
                    (unclipped_center, unclipped_spread), encoder)
                # update termination condition
                tries_left -= 1
                mutation_valid = (value >= new_center - new_spread) and (
                    value <= new_center + new_spread)
            if not mutation_valid:
                # stay:
                new_center, new_spread = center, spread

                #
                # TODO: this new way of doing misses lots of covering!
                #

            center_spread_list.append((new_center, new_spread))
        return BitConditionRealEncoding(
            encoders=self.real_translators,
            center_spreads=center_spread_list,
            mutation_strength=self.mutation_strength,
            mutation_prob=self.mutation_prob)
Exemple #5
0
 def _mutate_interval_by_translation(self, interval: Tuple[float, float],
                                     value: float) -> Tuple[float, float]:
     center, spread = interval
     bottom, top = center - spread, center + spread  # interval is [bottom, top]
     # let's do a translate
     if (spread == 0) or ((top - bottom)
                          == (self.real_translators.extremes[1] -
                              self.real_translators.extremes[0])):
         # there is nothing else to do:
         new_center, new_spread = center, spread
     else:
         delta_min_max = (max(self.real_translators.extremes[0] - bottom,
                              value - top),
                          min(self.real_translators.extremes[1] - top,
                              value - bottom))
         # let's choose a delta - preventing a 0 which won't translate anything:
         delta = random_in(delta_min_max[0], delta_min_max[1])
         while delta == 0:
             delta = random_in(delta_min_max[0], delta_min_max[1])
         new_center = center + delta
         new_spread = spread
     return new_center, new_spread
Exemple #6
0
 def test_matched(self):
     for _ in range(10):
         condition = BitConditionRealEncoding.random(self.real_translator,
                                                     mutation_strength=.1,
                                                     mutation_prob=.2,
                                                     length=randint(2, 20))
         # situation(s)
         reals = [
             self.real_translator.clip(
                 random_in(center - spread, center + spread))
             for center, spread in condition.center_spreads
         ]
         situation = BitString(self.real_translator, reals)
         # print(condition)
         # print(situation)
         self.assertTrue(condition(situation))
     # condition(s)
     self.assertTrue(self.condition(self.condition))
     a_condition = BitConditionRealEncoding(encoders=self.real_translator,
                                            center_spreads=[(3, 1), (5, 0)],
                                            mutation_prob=.2,
                                            mutation_strength=.1)
     self.assertTrue(self.condition(a_condition))
Exemple #7
0
 def random_from_center_list(cls, center_list: List[float],
                             encoders: Union[EncoderDecoder,
                                             List[EncoderDecoder]],
                             mutation_strength: float,
                             mutation_prob: float):
     # did I get just 1 encoder, applicable for all reals?
     if isinstance(encoders, EncoderDecoder):
         encoders = [encoders] * len(center_list)
     assert len(center_list) == len(encoders)
     assert all([(center <= encoder.extremes[1])
                 and (center >= encoder.extremes[0])
                 for center, encoder in zip(center_list, encoders)])
     center_spread_list = []
     for center, encoder in zip(center_list, encoders):
         spread = random_in(0, (encoder.extremes[1] - encoder.extremes[0]) /
                            2)  # relatively small spread
         center, spread = BitConditionRealEncoding.clip_center_spread_class(
             encoder, (center, spread))
         center_spread_list.append((center, spread))
     return cls(encoders=encoders,
                center_spreads=center_spread_list,
                mutation_strength=mutation_strength,
                mutation_prob=mutation_prob)
Exemple #8
0
 def get_mutation_value(m: float, encoder: EncoderDecoder) -> float:
     return (-1 if random() < 0.5 else 1) * \
            random_in(0, m) * (encoder.extremes[1] - encoder.extremes[0])