I am using the Genetic algorithm (Pymoo) to solve an optimization problem. However, I could not find a feasible solution so I demanded to print the least infeasible solution and I found out that the results violated my constraints. Can someone show me how to prevent this, please? Isn’t that kind of results should be rejected at the very first step?
My professor said that I may interfere with the GA package itself. But I think maybe there are some problems with my constraints and how I declared them (so that the package can not understand them)(code below).
This code is trying to declare an optimization function of a multi-machine layout that minimizes the distance between the center of machines while ensuring that none will be stacked on each other. However, I am not sure about how the package considers my constraints, not where my formula is wrong.
class MyProblem(ElementwiseProblem):
def __init__(self, **kwargs):
variables = dict()
for k in range(0, 16):
variables[f"x{k}"] = Integer(bounds=(0, 1000))
for k in range(16, 80):
variables[f"x{k}"] = Binary()
for k in range(80, 144):
variables[f"x{k}"] = Binary()
for k in range(144, 160):
variables[f"x{k}"] = Binary()
for k in range(160, 176):
variables[f"x{k}"] = Binary()
#The first 16 variables are integers, the rest are binary
super().__init__(vars = variables, n_obj=1, n_constr=328,
xl=np.concatenate((np.zeros(16), np.zeros(160))),
xu=np.concatenate((1000*np.ones(16), np.ones(160))), **kwargs)
self.material = material
self.yo = yo
self.y_low = y_low
self.y_upper = y_upper
self.l = l
self.lo = lo
self.w = w
self.wo = wo
self.xo = xo
def _evaluate(self, X, out, *args, **kwargs):
# Reshape the array into pairs of variables
x = np.array([X[k] for k in range(0, 16)]).reshape(-1, 2)
zx = np.array([X[k] for k in range(16, 80)]).reshape(-1, 8)
zy = np.array([X[k] for k in range(80, 144)]).reshape(-1, 8)
zox = np.array([X[k] for k in range(144, 160)]).reshape(-1, 2)
zoy = np.array([X[k] for k in range(160, 176)]).reshape(-1, 2)
# Calculate the objective function value based on each pair of variables
# MHC
f0 = np.sum(x[:, 0] + x[:, 1]) # Replace with your actual objective function
f = 0
for i in range(0, 8):
for j in range(0, 8):
f += np.sum(self.material[i, j] * 1 * (abs(x[i, 0] - x[j, 0])) + abs((x[i, 1] - x[j, 1])))
out["F"] = f
constraints = []
#32
for i in range(0, 8):
constraint1 = self.l[i] / 2 - x[i, 0]
constraint2 = x[i, 0] - (3000 - self.l[i] / 2)
constraints.append(constraint1)
constraints.append(constraint2)
constraint3 = self.w[i] / 2 - x[i, 1]
constraint4 = x[i, 1] - (2000 - self.w[i] / 2)
constraints.append(constraint3)
constraints.append(constraint4)
#256
for i in range(8):
for j in range(8):
if i != j:
constraint5 = zx[i, j] * (self.l[i] / 2 + self.l[j] / 2) - (x[j, 0] - x[i, 0])
constraint6 = (1 - zx[i, j]) * (self.l[i] / 2 + self.l[j] / 2) - (x[i, 0] - x[j, 0])
constraint7 = zy[i, j] * (self.w[i] / 2 + self.w[j] / 2) - (x[j, 1] - x[i, 1])
constraint8 = (1 - zy[i, j]) * (self.w[i] / 2 + self.w[j] / 2) - (x[i, 1] - x[j, 1])
constraints.append(constraint5)
constraints.append(constraint6)
constraints.append(constraint7)
constraints.append(constraint8)
#320
for i in range(8):
for v in range(2):
constraint9 = zox[i, v] * (self.lo[v] / 2 + self.l[i] / 2) - (x[i, 0] - self.xo[v])
constraint10 = (1 - zox[i, v]) * (self.lo[v] / 2 + self.l[i] / 2) - (self.xo[v] - x[i, 0])
constraint11 = zoy[i, v] * (self.wo[v] / 2 + self.w[i] / 2) - (x[i, 1] - self.yo[v])
constraint12 = (1 - zoy[i, v]) * (self.wo[v] / 2 + self.w[i] / 2) - (self.yo[v] - x[i, 1])
constraints.append(constraint9)
constraints.append(constraint10)
constraints.append(constraint11)
constraints.append(constraint12)
for i in range(8):
constraint13 = ((x[i,1] + self.w[i]/2) - self.y_low) * (self.y_upper - (x[i,1] - self.w[i]/2))
constraints.append(constraint13)
out["G"] = constraints
I tried many inputs and it turns out that not only one fixed constraint is violated but sometimes other constraints are violated too.
Please help me random strangers!
NVivallyH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.