Exemplo n.º 1
0
Arquivo: pso.py Projeto: zyumons/NiaPy
    def init(self, task):
        r"""Initialize dynamic arguments of Particle Swarm Optimization algorithm.

		Args:
			task (Task): Optimization task.

		Returns:
			Dict[str, np.ndarray]:
			* vMin: Mininal velocity.
			* vMax: Maximal velocity.
			* V: Initial velocity of particle.
			* flag: Refresh gap counter.
		"""
        return {
            'vMin':
            fullArray(self.vMin, task.D),
            'vMax':
            fullArray(self.vMax, task.D),
            'V':
            np.full([self.NP, task.D], 0.0),
            'flag':
            np.full(self.NP, 0),
            'Pc':
            np.asarray([
                .05 + .45 * (np.exp(10 * (i - 1) / (self.NP - 1)) - 1) /
                (np.exp(10) - 1) for i in range(self.NP)
            ])
        }
Exemplo n.º 2
0
 def test_range(self):
     self.assertTrue(
         array_equal(
             fullArray(self.Upper, self.D) - fullArray(self.Lower, self.D),
             self.task.bRange))
     self.assertTrue(
         array_equal(
             fullArray(self.Upper, self.D) - fullArray(self.Lower, self.D),
             self.task.bcRange()))
Exemplo n.º 3
0
	def initWeights(self, task):
		r"""Initialize weights.

		Args:
			task (Task): Optimization task.

		Returns:
			Tuple[numpy.ndarray, numpy.ndarray]:
				1. Weights for neighborhood.
				2. Weights for foraging.
		"""
		return fullArray(self.W_n, task.D), fullArray(self.W_f, task.D)
Exemplo n.º 4
0
	def init(self, task):
		r"""Initialize dynamic parameters of algorithm.

		Args:
			task (Task): Optimization task.

		Returns:
			Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]
				1. Array of `self.alpha` propagated values
				2. Array of `self.gamma` propagated values
				3. Array of `self.theta` propagated values
		"""
		return fullArray(self.alpha, self.NP), fullArray(self.gamma, self.NP), fullArray(self.theta, self.NP)
Exemplo n.º 5
0
    def initRanges(self, task):
        r"""Initialize ranges.

        Args:
            task (Task): Optimization task.

        Returns:
            Tuple[numpy.ndarray[float], numpy.ndarray[float], numpy.ndarray[float]]:
                1. Initial amplitude values over dimensions.
                2. Final amplitude values over dimensions.
                3. uAmin.
        """
        Ainit, Afinal = fullArray(self.Ainit,
                                  task.D), fullArray(self.Afinal, task.D)
        return Ainit, Afinal, self.uAmin(Ainit, Afinal, task)
Exemplo n.º 6
0
    def initAmplitude(self, task):
        r"""Initialize amplitudes for dimensions.

        Args:
            task (Task): Optimization task.

        Returns:
            numpy.ndarray[float]: Starting amplitudes.
        """
        return fullArray(self.A, task.D)
Exemplo n.º 7
0
    def init(self, task):
        r"""Initialize dynamic arguments of Particle Swarm Optimization algorithm.

		Args:
			task (Task): Optimization task.

		Returns:
			Dict[str, Any]:
				* w (numpy.ndarray): Inertial weight.
				* vMin (numpy.ndarray): Mininal velocity.
				* vMax (numpy.ndarray): Maximal velocity.
				* V (numpy.ndarray): Initial velocity of particle.
		"""
        return {
            'w': fullArray(self.w, task.D),
            'vMin': fullArray(self.vMin, task.D),
            'vMax': fullArray(self.vMax, task.D),
            'V': full([self.NP, task.D], 0.0)
        }
Exemplo n.º 8
0
    def __init__(self,
                 D=0,
                 optType=OptimizationType.MINIMIZATION,
                 benchmark=None,
                 Lower=None,
                 Upper=None,
                 frepair=limit_repair,
                 **kwargs):
        r"""Initialize task class for optimization.

        Arguments:
                D (Optional[int]): Number of dimensions.
                optType (Optional[OptimizationType]): Set the type of optimization.
                benchmark (Union[str, Benchmark]): Problem to solve with optimization.
                Lower (Optional[numpy.ndarray]): Lower limits of the problem.
                Upper (Optional[numpy.ndarray]): Upper limits of the problem.
                frepair (Optional[Callable[[numpy.ndarray, numpy.ndarray, numpy.ndarray, Dict[str, Any]], numpy.ndarray]]): Function for reparing individuals components to desired limits.

        See Also:
                * `func`:NiaPy.util.Utility.__init__`
                * `func`:NiaPy.util.Utility.repair`

        """

        # dimension of the problem
        self.D = D
        # set optimization type
        self.optType = optType
        # set optimization function
        self.benchmark = Utility().get_benchmark(
            benchmark) if benchmark is not None else None

        if self.benchmark is not None:
            self.Fun = self.benchmark.function(
            ) if self.benchmark is not None else None

        # set Lower limits
        if Lower is not None:
            self.Lower = fullArray(Lower, self.D)
        elif Lower is None and benchmark is not None:
            self.Lower = fullArray(self.benchmark.Lower, self.D)
        else:
            self.Lower = fullArray(0, self.D)

        # set Upper limits
        if Upper is not None:
            self.Upper = fullArray(Upper, self.D)
        elif Upper is None and benchmark is not None:
            self.Upper = fullArray(self.benchmark.Upper, self.D)
        else:
            self.Upper = fullArray(0, self.D)

        # set range
        self.bRange = self.Upper - self.Lower
        # set repair function
        self.frepair = frepair
Exemplo n.º 9
0
	def test_a_float_fine(self):
		A = fullArray(25.25, 10)
		self.assertTrue(array_equal(A, full(10, 25.25)))
Exemplo n.º 10
0
	def test_a_float_list2_fine(self):
		a = [25.25 + i for i in range(10)]
		A = fullArray(a, 13)
		a.extend([25.25 + i for i in range(3)])
		self.assertTrue(array_equal(A, asarray(a)))
Exemplo n.º 11
0
	def test_a_int_array2_fine(self):
		a = [25 + i for i in range(10)]
		A = fullArray(asarray(a), 13)
		a.extend([25 + i for i in range(3)])
		self.assertTrue(array_equal(A, asarray(a)))
Exemplo n.º 12
0
	def test_a_int_list1_fine(self):
		a = [25 + i for i in range(10)]
		A = fullArray(a, 15)
		a.extend([25 + i for i in range(5)])
		self.assertTrue(array_equal(A, asarray(a)))
Exemplo n.º 13
0
	def test_a_float_array1_fine(self):
		a = [25.25 + i for i in range(10)]
		A = fullArray(asarray(a), 15)
		a.extend([25.25 + i for i in range(5)])
		self.assertTrue(array_equal(A, asarray(a)))
Exemplo n.º 14
0
	def test_is_feasible(self):
		self.assertFalse(self.task.isFeasible(fullArray([1, 2, 3], self.D)))
Exemplo n.º 15
0
	def test_a_int_array_fine(self):
		a = asarray([25 for i in range(10)])
		A = fullArray(a, 10)
		self.assertTrue(array_equal(A, full(10, 25)))
Exemplo n.º 16
0
	def test_upper(self):
		self.assertTrue(array_equal(fullArray(self.Upper, self.D), self.task.Upper))
		self.assertTrue(array_equal(fullArray(self.Upper, self.D), self.task.bcUpper()))
Exemplo n.º 17
0
	def test_a_float_list_fine(self):
		a = [25.25 for i in range(10)]
		A = fullArray(a, 10)
		self.assertTrue(array_equal(A, full(10, 25.25)))
Exemplo n.º 18
0
	def init(self, task): return fullArray(self.alpha, self.NP), fullArray(self.gamma, self.NP), fullArray(self.theta, self.NP)

	def FI(self, x_f, xpb_f, xb_f, alpha):
Exemplo n.º 19
0
 def init(self, task):
     self.w, self.vMin, self.vMax = fullArray(self.w, task.D), fullArray(
         self.vMin, task.D), fullArray(self.vMax, task.D)
Exemplo n.º 20
0
	def test_a_int_list3_fine(self):
		a = [25 + i for i in range(10)]
		A = fullArray(a, 9)
		a.remove(34)
		self.assertTrue(array_equal(A, asarray(a)))
Exemplo n.º 21
0
 def generateIndividual(self, D, upper, lower):
     u, l = fullArray(upper, D), fullArray(lower, D)
     return l + rnd.rand(D) * (u - l)
Exemplo n.º 22
0
 def setUp(self):
     self.D = 10
     self.Upper, self.Lower = fullArray(10, self.D), fullArray(-10, self.D)
     self.met = limit_repair
Exemplo n.º 23
0
 def initAmplitude(self, task):
     return fullArray(self.A, task.D)
Exemplo n.º 24
0
 def initRanges(self, task):
     Ainit, Afinal = fullArray(self.Ainit,
                               task.D), fullArray(self.Afinal, task.D)
     return Ainit, Afinal, self.uAmin(Ainit, Afinal, task)
Exemplo n.º 25
0
	def test_a_float_array3_fine(self):
		a = [25.25 + i for i in range(10)]
		A = fullArray(asarray(a), 9)
		a.remove(34.25)
		self.assertTrue(array_equal(A, asarray(a)))
Exemplo n.º 26
0
 def initWeights(self, task):
     return fullArray(self.W_n, task.D), fullArray(self.W_f, task.D)
Exemplo n.º 27
0
 def setUp(self):
     self.D, self.NP, self.F, self.CR, self.p = 10, 100, 0.5, 0.5, 0.25
     self.Upper, self.Lower = fullArray(100,
                                        self.D), fullArray(-100, self.D)
     self.evalFun = MyBenchmark().function()
Exemplo n.º 28
0
	def test_lower(self):
		self.assertTrue(array_equal(fullArray(self.Lower, self.D), self.task.Lower))
		self.assertTrue(array_equal(fullArray(self.Lower, self.D), self.task.bcLower()))