mealpy icon indicating copy to clipboard operation
mealpy copied to clipboard

[BUG]:

Open zorka5 opened this issue 2 months ago • 1 comments

Description of the bug

I'm trying out different algorithms for optimization with constraints and I was trying to use Simulated Annealing for two variable function optimization. I set the bounds to 0-10 for both variables. Some of the generated values are out of contstraints (see the code below). Is it desired behavior? I can always write the additional function that punishes the out-of-bounds values, but was is the point of defining constraints if they are not restricted?

Steps To Reproduce

from mealpy import SA, FloatVar


def cost_function(x) -> float:
    first = x[0]
    second = x[1]

    if first < 0 or second < 0:
        print(first, second)

    cost = first + second

    return cost


problem = {
    "obj_func": cost_function,
    "bounds": FloatVar(lb=[0, 0], ub=[10, 10]),
    "minmax": "min",
    "verbose": True,
    "save_population": True,
}

model = SA.OriginalSA(epoch=20, pop_size=30)
g_best = model.solve(problem, seed=200)

Output: -0.07644477404921987 1.7267839946401735 -0.2708652232352955 1.8850930435665296 -0.19257903624969436 1.992746856547862 -0.09181381214228351 2.1347160559747373

Additional Information

No response

zorka5 avatar Apr 24 '24 16:04 zorka5

@zorka5,

SA is not supposed to be here in this library because it is a single-based solution and originally based on a discrete method. The amended solution does not exist in the original paper, so it can definitely go out of bounds. If you want to use a population-based SA, choose the SA.SwarmSA() class.

thieu1995 avatar Apr 25 '24 00:04 thieu1995