From 2a991e2c5951857616683167db8b8c65132eab2f Mon Sep 17 00:00:00 2001 From: SevgiAkten Date: Wed, 14 Aug 2024 10:28:22 +0300 Subject: [PATCH] (#57) fix overflow error --- requirements.txt | 1 + src/problems/single_objective/continuous/bentcigar.py | 2 +- .../single_objective/continuous/bohachevsky.py | 2 +- src/problems/single_objective/continuous/holzman.py | 2 +- src/problems/single_objective/continuous/levy.py | 2 +- src/problems/single_objective/continuous/matyas.py | 2 +- src/problems/single_objective/continuous/pow.py | 2 +- src/problems/single_objective/continuous/powell.py | 11 ++++++----- .../single_objective/continuous/rosenbrock.py | 2 +- .../single_objective/continuous/rothellipsoid.py | 4 ++-- src/problems/single_objective/continuous/schaffer.py | 2 +- .../single_objective/continuous/styblinskitang.py | 2 +- .../continuous/sumofdifferentpowers.py | 3 ++- .../single_objective/continuous/threehumps.py | 2 +- src/problems/single_objective/continuous/zakharov.py | 2 +- src/problems/single_objective/continuous/zettle.py | 2 +- src/tests/test_optimizer_alpha_cga.py | 4 ++-- src/tests/test_optimizer_ccga.py | 1 - src/tests/test_optimizer_cga.py | 4 ++-- src/tests/test_optimizer_mccga.py | 4 ++-- src/tests/test_optimizer_sync_cga.py | 4 ++-- 21 files changed, 31 insertions(+), 29 deletions(-) diff --git a/requirements.txt b/requirements.txt index 2da88a7..80ef690 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,7 @@ grip==4.6.2 iniconfig==2.0.0 Markdown==3.6 Markdown-Editor==1.0.7 +mpmath==1.3.0 numpy==1.26.2 path-and-address==2.0.1 pluggy==1.5.0 diff --git a/src/problems/single_objective/continuous/bentcigar.py b/src/problems/single_objective/continuous/bentcigar.py index 28287cc..23d4c10 100644 --- a/src/problems/single_objective/continuous/bentcigar.py +++ b/src/problems/single_objective/continuous/bentcigar.py @@ -1,5 +1,5 @@ from problems.abstract_problem import AbstractProblem -from numpy import power as pw +from mpmath import power as pw class Bentcigar(AbstractProblem): """ diff --git a/src/problems/single_objective/continuous/bohachevsky.py b/src/problems/single_objective/continuous/bohachevsky.py index 0d4b432..0d1cc63 100644 --- a/src/problems/single_objective/continuous/bohachevsky.py +++ b/src/problems/single_objective/continuous/bohachevsky.py @@ -1,6 +1,6 @@ from numpy import cos from numpy import pi -from numpy import power as pw +from mpmath import power as pw from problems.abstract_problem import AbstractProblem class Bohachevsky(AbstractProblem): diff --git a/src/problems/single_objective/continuous/holzman.py b/src/problems/single_objective/continuous/holzman.py index 91cb408..691259f 100644 --- a/src/problems/single_objective/continuous/holzman.py +++ b/src/problems/single_objective/continuous/holzman.py @@ -1,5 +1,5 @@ from problems.abstract_problem import AbstractProblem -from numpy import power as pw +from mpmath import power as pw class Holzman(AbstractProblem): """ diff --git a/src/problems/single_objective/continuous/levy.py b/src/problems/single_objective/continuous/levy.py index 8ba3d1a..5bc5dda 100644 --- a/src/problems/single_objective/continuous/levy.py +++ b/src/problems/single_objective/continuous/levy.py @@ -1,6 +1,6 @@ from problems.abstract_problem import AbstractProblem import math -from numpy import power as pw +from mpmath import power as pw class Levy(AbstractProblem): """ diff --git a/src/problems/single_objective/continuous/matyas.py b/src/problems/single_objective/continuous/matyas.py index 4e2d53b..1f18f01 100644 --- a/src/problems/single_objective/continuous/matyas.py +++ b/src/problems/single_objective/continuous/matyas.py @@ -1,5 +1,5 @@ from problems.abstract_problem import AbstractProblem -from numpy import power as pw +from mpmath import power as pw class Matyas(AbstractProblem): """ diff --git a/src/problems/single_objective/continuous/pow.py b/src/problems/single_objective/continuous/pow.py index 85a533c..79f80ca 100644 --- a/src/problems/single_objective/continuous/pow.py +++ b/src/problems/single_objective/continuous/pow.py @@ -1,5 +1,5 @@ from problems.abstract_problem import AbstractProblem -from numpy import power as pw +from mpmath import power as pw class Pow(AbstractProblem): """ diff --git a/src/problems/single_objective/continuous/powell.py b/src/problems/single_objective/continuous/powell.py index 95a6198..55f1718 100644 --- a/src/problems/single_objective/continuous/powell.py +++ b/src/problems/single_objective/continuous/powell.py @@ -1,5 +1,6 @@ from problems.abstract_problem import AbstractProblem -import numpy as np +from mpmath import power as pw + class Powell(AbstractProblem): """ @@ -42,10 +43,10 @@ def f(self, x: list) -> float: d = n // 4 for i in range(d): - a = np.power(x[4*i] + 10 * x[4*i + 1], 2) - b = np.power(x[4*i + 2] - x[4*i + 3], 2) - c = np.power(x[4*i + 1] - 2 * x[4*i + 2], 4) - e = np.power(x[4*i] - x[4*i + 3], 4) + a = pw(x[4*i] + 10 * x[4*i + 1], 2) + b = pw(x[4*i + 2] - x[4*i + 3], 2) + c = pw(x[4*i + 1] - 2 * x[4*i + 2], 4) + e = pw(x[4*i] - x[4*i + 3], 4) fitness += a + 5 * b + c + 10 * e return round(fitness, 1) diff --git a/src/problems/single_objective/continuous/rosenbrock.py b/src/problems/single_objective/continuous/rosenbrock.py index 9c0a96b..5243aed 100644 --- a/src/problems/single_objective/continuous/rosenbrock.py +++ b/src/problems/single_objective/continuous/rosenbrock.py @@ -1,5 +1,5 @@ from problems.abstract_problem import AbstractProblem -from numpy import power as pw +from mpmath import power as pw class Rosenbrock(AbstractProblem): """ Rosenbrock function implementation for optimization problems. diff --git a/src/problems/single_objective/continuous/rothellipsoid.py b/src/problems/single_objective/continuous/rothellipsoid.py index 39f2ba8..c9af24f 100644 --- a/src/problems/single_objective/continuous/rothellipsoid.py +++ b/src/problems/single_objective/continuous/rothellipsoid.py @@ -1,5 +1,5 @@ from problems.abstract_problem import AbstractProblem -import numpy as np +from mpmath import power as pw class Rothellipsoid(AbstractProblem): """ @@ -41,6 +41,6 @@ def f(self, x: list) -> float: n = len(x) for i in range(n): - fitness += (i + 2) * np.power(x[i], 2) + fitness += (i + 2) * pw(x[i], 2) return round(fitness, 3) diff --git a/src/problems/single_objective/continuous/schaffer.py b/src/problems/single_objective/continuous/schaffer.py index d533bf9..5507bad 100644 --- a/src/problems/single_objective/continuous/schaffer.py +++ b/src/problems/single_objective/continuous/schaffer.py @@ -1,5 +1,5 @@ import numpy as np -from numpy import power as pw +from mpmath import power as pw from problems.abstract_problem import AbstractProblem diff --git a/src/problems/single_objective/continuous/styblinskitang.py b/src/problems/single_objective/continuous/styblinskitang.py index dcc8fcf..84ee49b 100644 --- a/src/problems/single_objective/continuous/styblinskitang.py +++ b/src/problems/single_objective/continuous/styblinskitang.py @@ -1,5 +1,5 @@ from problems.abstract_problem import AbstractProblem -from numpy import power as pw +from mpmath import power as pw class StyblinskiTang(AbstractProblem): """ diff --git a/src/problems/single_objective/continuous/sumofdifferentpowers.py b/src/problems/single_objective/continuous/sumofdifferentpowers.py index 4a82ebf..5026c3a 100644 --- a/src/problems/single_objective/continuous/sumofdifferentpowers.py +++ b/src/problems/single_objective/continuous/sumofdifferentpowers.py @@ -1,4 +1,5 @@ import numpy as np +from mpmath import power as pw from problems.abstract_problem import AbstractProblem class Sumofdifferentpowers(AbstractProblem): @@ -25,6 +26,6 @@ def f(self, x: list) -> float: for i in range(len(x)): a = np.abs(x[i]) b = i + 1 - fitness += np.power(a, b) + fitness += pw(a, b) return round(fitness, 3) diff --git a/src/problems/single_objective/continuous/threehumps.py b/src/problems/single_objective/continuous/threehumps.py index 5cff4d0..ad8b25f 100644 --- a/src/problems/single_objective/continuous/threehumps.py +++ b/src/problems/single_objective/continuous/threehumps.py @@ -1,5 +1,5 @@ from problems.abstract_problem import AbstractProblem -from numpy import power as pw +from mpmath import power as pw class Threehumps(AbstractProblem): """ diff --git a/src/problems/single_objective/continuous/zakharov.py b/src/problems/single_objective/continuous/zakharov.py index a1b1871..fd642c1 100644 --- a/src/problems/single_objective/continuous/zakharov.py +++ b/src/problems/single_objective/continuous/zakharov.py @@ -1,5 +1,5 @@ from problems.abstract_problem import AbstractProblem -from numpy import power as pw +from mpmath import power as pw class Zakharov(AbstractProblem): """ Zakharov function implementation for optimization problems. diff --git a/src/problems/single_objective/continuous/zettle.py b/src/problems/single_objective/continuous/zettle.py index f982621..1e55deb 100644 --- a/src/problems/single_objective/continuous/zettle.py +++ b/src/problems/single_objective/continuous/zettle.py @@ -1,5 +1,5 @@ from problems.abstract_problem import AbstractProblem -from numpy import power as pw +from mpmath import power as pw class Zettle(AbstractProblem): """ diff --git a/src/tests/test_optimizer_alpha_cga.py b/src/tests/test_optimizer_alpha_cga.py index 04e8083..8adfaf1 100644 --- a/src/tests/test_optimizer_alpha_cga.py +++ b/src/tests/test_optimizer_alpha_cga.py @@ -1,6 +1,6 @@ import pytest from optimizer import alpha_cga, GeneType, TournamentSelection, ByteOnePointCrossover, ByteMutationRandom, OnePointCrossover, BitFlipMutation, PMXCrossover, SwapMutation -import numpy as np +import mpmath as mp from typing import List class RealProblem: @@ -30,7 +30,7 @@ def f(self, x): float The computed value of the function given the input x. """ - return sum(np.power(xi, 2) for xi in x) + return sum(mp.power(xi, 2) for xi in x) def test_optimizer_alpha_cga_real(): """Test alpha_cga on a real-valued sum of squares problem.""" diff --git a/src/tests/test_optimizer_ccga.py b/src/tests/test_optimizer_ccga.py index 9e7cf90..5b1db47 100644 --- a/src/tests/test_optimizer_ccga.py +++ b/src/tests/test_optimizer_ccga.py @@ -1,6 +1,5 @@ import pytest from optimizer import ccga, GeneType, TournamentSelection, ByteOnePointCrossover, ByteMutationRandom, OnePointCrossover, BitFlipMutation, PMXCrossover, SwapMutation -import numpy as np from typing import List class BinaryProblem: diff --git a/src/tests/test_optimizer_cga.py b/src/tests/test_optimizer_cga.py index 5f241d7..7d184da 100644 --- a/src/tests/test_optimizer_cga.py +++ b/src/tests/test_optimizer_cga.py @@ -1,6 +1,6 @@ import pytest from optimizer import cga, GeneType, TournamentSelection, ByteOnePointCrossover, ByteMutationRandom, OnePointCrossover, BitFlipMutation, PMXCrossover, SwapMutation -import numpy as np +import mpmath as mp from typing import List class RealProblem: @@ -30,7 +30,7 @@ def f(self, x): float The computed value of the function given the input x. """ - return sum(np.power(xi, 2) for xi in x) + return sum(mp.power(xi, 2) for xi in x) def test_optimizer_cga_real(): """Test CGA on a real-valued sum of squares problem.""" diff --git a/src/tests/test_optimizer_mccga.py b/src/tests/test_optimizer_mccga.py index f9de4a2..469b230 100644 --- a/src/tests/test_optimizer_mccga.py +++ b/src/tests/test_optimizer_mccga.py @@ -1,6 +1,6 @@ import pytest from optimizer import mcccga, GeneType, TournamentSelection, ByteOnePointCrossover, ByteMutationRandom, OnePointCrossover, BitFlipMutation, PMXCrossover, SwapMutation -import numpy as np +import mpmath as mp from typing import List class RealProblem: @@ -30,7 +30,7 @@ def f(self, x): float The computed value of the function given the input x. """ - return sum(np.power(xi, 2) for xi in x) + return sum(mp.power(xi, 2) for xi in x) def test_optimizer_mcccga_binary(): """Test mcccga on a binary OneMax problem.""" diff --git a/src/tests/test_optimizer_sync_cga.py b/src/tests/test_optimizer_sync_cga.py index 854eade..b87e9f7 100644 --- a/src/tests/test_optimizer_sync_cga.py +++ b/src/tests/test_optimizer_sync_cga.py @@ -1,6 +1,6 @@ import pytest from optimizer import sync_cga, GeneType, TournamentSelection, ByteOnePointCrossover, ByteMutationRandom, OnePointCrossover, BitFlipMutation, PMXCrossover, SwapMutation -import numpy as np +import mpmath as mp from typing import List class RealProblem: @@ -30,7 +30,7 @@ def f(self, x): float The computed value of the function given the input x. """ - return sum(np.power(xi, 2) for xi in x) + return sum(mp.power(xi, 2) for xi in x) def test_optimizer_sync_cga_real(): """Test sync_cga on a real-valued sum of squares problem."""