I've successfully built my model and it runs and plots a Pareto front. However, I don't know how to store X* (the optimum value of ALL the decision variables that resulted in the optimum F).
My model is MixedVariable and I have both Integer and Real variables as decision variables, but I don't know how to print or store the value of every single one of them.
I read the documentation on the Pymoo website but I couldn't materialize them, that is, when I do as documentation instructions, it only prints one value per each defined category of variables, instead of the values of all the decision variables belonging to that category (by category I mean set of the variables with the same type and range). I would place a down-scaled example of my script in the snippet below:
import numpy as np
import pandas as pd
from pymoo.core.problem import ElementwiseProblem
from pymoo.core.variable import Real, Integer
class MyProblem(ElementwiseProblem):
def __init__(self, I_, J_, d__ij, d__ji):
self.I = I_
self.J = J_
self.d_ij = d__ij
self.d_ji = d__ji
J, I = self.J, self.I
n_obj = 2
n_ieq_constr = I * J
n_eq_constr = J + 3*I
Vars = {
"y": Integer(bounds=(0, 100)),
"z": Integer(bounds=(0, 1)),
"w": Real(bounds=(0, 100)),
"x": Real(bounds=(0, 1))
}
super().__init__(vars=Vars,
n_obj=n_obj,
n_ieq_constr=n_ieq_constr,
n_eq_constr=n_eq_constr)
def _evaluate(self, X, out, *args, **kwargs):
J, I = self.J, self.I
x = []
for i in range(J*I):
xi = X["x"]
x.append(xi)
y = []
for i in range(2*I + J):
yi = X["y"]
y.append(yi)
z = []
for i in range(4*J + 1):
zi = X["z"]
z.append(zi)
w = []
for i in range(I + J):
wi = X["w"]
w.append(wi)
# Decision Variables:
Pr_i = np.array(w[:I]).reshape(I, 1)
IB_j = np.array(w[I:I+J]).reshape(J, 1)
...
CRM_ji = np.array(x[:J*I]).reshape(J, I)
...
R_ij = np.array(y[: I*J]).reshape(I, J)
S_ij = np.array(y[J*I : J*I+J*I]).reshape(I, J)
...
X_j = np.array(z[:J]).reshape(J, 1)
X_i = np.array(z[J:J+I]).reshape(I, 1)
...
# Objective functions
f1 = F(self.d_ji, R_ij, Pr_i, ...)
f2 = F(self.d_ij, X_j, CRM_ji, ...)
out["F"] = [f1, f2]
out["H"] = [h1, ..., hn]
out["G"] = [g1, ..., gm]
# Load data I, J, d_ij, d_ji
problem = MyProblem(I, J, d_ij, d_ji)
from pymoo.core.mixed import MixedVariableGA
from pymoo.optimize import minimize
from pymoo.algorithms.moo.nsga3 import ReferenceDirectionSurvival
from pymoo.util.ref_dirs import get_reference_directions
ref_dirs = get_reference_directions("das-dennis", 2, n_partitions=12)
algorithm = MixedVariableGA(pop_size=200, survival=ReferenceDirectionSurvival(ref_dirs))
res = minimize(problem,
algorithm,
('n_gen', 15),
seed=42,
verbose=True)
print("Best solution found: \nX = %s" % (res.X))
from pymoo.visualization.scatter import Scatter
plot = Scatter()
plot.add(problem.pareto_front(), plot_type="line", color="black", alpha=0.7)
plot.add(res.F, facecolor="red", edgecolor="none")
plot.show()
If I assign res.X to a variable, for example named X, it will be a Numpy array containing pop_size rows of dict objects each containing only four single key-value pairs like this {'y': 0, 'z': 1, 'w': 4.603468972784174, 'x': 0.031364685547833934}; however y, z, w, and x are not supposed to be single floats or integers, but list! as I have determined their type earlier in the _evaluate function.
How can I derive the values for all decision variables?