Esempio n. 1
0
 def test_sracos_strategy_lm(self):
     s0 = Solution(x=[1, 1, 1], value=0)
     s1 = Solution(x=[2.2, 2.2, 2.2], value=1)
     s2 = Solution(x=[3, 3, 3], value=2)
     iset = [s0, s1, s2]
     sracos = SRacos()
     test_s1 = Solution(x=[2.1, 2.1, 2.1], value=2.1)
     sracos.strategy_lm(iset, s0, test_s1)
     assert iset[2].get_value() == 2.1
Esempio n. 2
0
 def test_sracos_strategy_rr(self):
     s0 = Solution(value=0)
     s1 = Solution(value=1)
     s2 = Solution(value=2)
     iset = [s0, s1, s2]
     sracos = SRacos()
     test_s1 = Solution(value=2.1)
     sracos.strategy_rr(iset, test_s1)
     assert len(iset) == 3 and (iset[0].get_value() == 2.1
                                or iset[1].get_value() == 2.1
                                or iset[2].get_value() == 2.1)
Esempio n. 3
0
    def opt(self, objective, parameter, strategy='WR'):
        """
        This function will choose optimization algorithm and use it to optimize.

        :param objective: a Objective object
        :param parameter: a Parameter object
        :param strategy: replace strategy, used by SRacos and SSRacos
        :return: the best solution
        """

        self.clear()
        ub = parameter.get_uncertain_bits()
        if ub is None:
            ub = self.choose_ub(objective)
        if parameter.get_sequential():
            if parameter.get_noise_handling(
            ) is True and parameter.get_suppression() is True:
                self.__algorithm = SSRacos()
            else:
                self.__algorithm = SRacos()
            self.__best_solution = self.__algorithm.opt(
                objective, parameter, strategy, ub)
        else:
            self.__algorithm = Racos()
            self.__best_solution = self.__algorithm.opt(
                objective, parameter, ub)
        return self.__best_solution
Esempio n. 4
0
 def test_sracos_binary_search(self):
     s0 = Solution(value=0)
     s1 = Solution(value=1)
     s2 = Solution(value=2)
     s3 = Solution(value=3)
     s4 = Solution(value=4)
     # 1 3 0 2 4
     test_s1 = Solution(value=2.1)
     test_s2 = Solution(value=4.5)
     test_s3 = Solution(value=-1)
     test_s4 = Solution(value=2)
     set = [s0, s1, s2, s3, s4]
     sracos = SRacos()
     assert sracos.binary_search(set, test_s1, 0, 4) == 3
     assert sracos.binary_search(set, test_s1, 0, 2) == 3
     assert sracos.binary_search(set, test_s2, 0, 4) == 5
     assert sracos.binary_search(set, test_s3, 0, 4) == 0
     assert sracos.binary_search(set, test_s4, 0, 4) == 3
Esempio n. 5
0
 def test_sracos_replace(self):
     s0 = Solution(x=[0, 0, 0], value=0.5)
     s1 = Solution(x=[1, 1, 1], value=1)
     s2 = Solution(x=[2, 2, 2], value=2)
     s3 = Solution(x=[3, 3, 3], value=3)
     s4 = Solution(x=[4, 4, 4], value=4)
     pos_set = [s0, s1, s2, s3, s4]
     neg_set = [s2, s3, s1, s4, s0]
     x = Solution(x=[2.1, 2.1, 2.1], value=0.1)
     sracos = SRacos()
     sracos.replace(pos_set, x, 'pos', 'WR')
     assert pos_set[4].get_value() == 3 and pos_set[0].get_value() == 0.1
     sracos.replace(neg_set, x, 'neg', 'LM')
     assert neg_set[3].get_value() == 0.1
Esempio n. 6
0
 def test_sracos_strategy_wr(self):
     s0 = Solution(value=0)
     s1 = Solution(value=1)
     s2 = Solution(value=2)
     s3 = Solution(value=3)
     s4 = Solution(value=4)
     iset = [s0, s1, s2, s3, s4]
     sracos = SRacos()
     test_s1 = Solution(value=2.1)
     sracos.strategy_wr(iset, test_s1, 'pos')
     assert len(iset) == 5 and iset[0].get_value() == 0 and iset[1].get_value() == 1 and iset[2].get_value() == 2 \
         and iset[3].get_value() == 2.1 and iset[4].get_value() == 3
     iset2 = [s1, s3, s0, s2, s4]
     sracos.strategy_wr(iset2, test_s1, 'neg')
     assert len(iset2) == 5 and iset2[4].get_value() == 2.1
 def __init__(self):
     SRacos.__init__(self)
     return
Esempio n. 8
0
 def test_sracos_distance(self):
     a = [2, 4]
     b = [5, 8]
     assert SRacos.distance(a, b) == 5