tiling_maganestuff
tiling_maganestuff

Reputation: 1

Unable to use pysamoo for a surrogate multiobjective (2) optimization with constraints (2)

I have already reported my issue on Github (here).

In short, my code is

class MyProblem(ElementwiseProblem):
    def __init__(self, **kwargs):
        in_vars = {
            "cl_h": Real(bounds=(6.0, 16.0)),
            "cr_h": Real(bounds=(0.57, 0.85)),
            "g_n_div_by_2": Integer(bounds=(1, 5)),
            "cl_n": Integer(bounds=(1, 11)),
            "g_l": Real(bounds=(0.1, 0.3)),
        }
        super().__init__(vars = in_vars, n_obj = 2, n_ieq_constr = 2, xl=np.array([6.0, 0.57, 1, 1, 0.1]), xu=np.array([16.0, 0.85, 5, 11, 0.3]), **kwargs)

    def _evaluate(self, X, out, *args, **kwargs):
        cr_w = X[1]
        input_params = InputParameters(cr_h = X[1],
                                cr_w = cr_w,
                                cl_n = int(X[3]),
                                cl_h = X[0],
                                g_n = 2 * int(X[2]),
                                g_L = X[4])

        idr = Idr(input_params)
        total_mass = float(idr.core_m + idr.cl_w_m + idr.pot_m + idr.cov_m)
        total_loss = float(idr.j_losses + idr.m_losses)
        final_id = idr.final_ind

        constraint_1 = abs(final_id - 55) / 55 - 0.05
        constraint_2 = input_params.i_m_p - 1.0

        out["F"] = [total_mass, total_loss]
        out["G"] = [constraint_1, constraint_2]

problem = MyProblem()

algorithm = SSANSGA2(n_initial_doe=5,
                     n_infills=10,
                     surr_pop_size=100,
                     surr_n_gen=500)

res = minimize(
    problem,
    algorithm,
    ('n_evals', 200),
    seed=1,
    verbose=True)

But I get the error:

==========================================================================================
n_gen  |  n_eval  | n_nds  |     cv_min    |     cv_avg    |      eps      |   indicator
==========================================================================================
     1 |        5 |      3 |  0.000000E+00 |  3.028206E+01 |             - |             -
Traceback (most recent call last):
  File "C:\Python3.10\lib\site-packages\pymoo\core\problem.py", line 355, in _format_dict
    v = v.reshape(shape[name])
ValueError: cannot reshape array of size 1000 into shape (100,2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "\git\Idr\pysamoo_test.py", line 78, in <module>
    res = minimize(
  File "C:\Python3.10\lib\site-packages\pymoo\optimize.py", line 67, in minimize
    res = algorithm.run()
  File "C:\Python3.10\lib\site-packages\pymoo\core\algorithm.py", line 138, in run
    self.next()
  File "C:\Python3.10\lib\site-packages\pymoo\core\algorithm.py", line 154, in next
    infills = self.infill()
  File "C:\Python3.10\lib\site-packages\pymoo\core\algorithm.py", line 190, in infill
    infills = self._infill()
  File "C:\Python3.10\lib\site-packages\pysamoo\algorithms\ssansga2.py", line 54, in _infill
    res = minimize(problem,
  File "C:\Python3.10\lib\site-packages\pymoo\optimize.py", line 67, in minimize
    res = algorithm.run()
  File "C:\Python3.10\lib\site-packages\pymoo\core\algorithm.py", line 138, in run
    self.next()
  File "C:\Python3.10\lib\site-packages\pymoo\core\algorithm.py", line 158, in next
    self.evaluator.eval(self.problem, infills, algorithm=self)
  File "C:\Python3.10\lib\site-packages\pymoo\core\evaluator.py", line 69, in eval
    self._eval(problem, pop[I], evaluate_values_of, **kwargs)
  File "C:\Python3.10\lib\site-packages\pymoo\core\evaluator.py", line 90, in _eval
    out = problem.evaluate(X, return_values_of=evaluate_values_of, return_as_dictionary=True, **kwargs)
  File "C:\Python3.10\lib\site-packages\pymoo\core\problem.py", line 257, in evaluate
    _out = self.do(X, return_values_of, *args, **kwargs)
  File "C:\Python3.10\lib\site-packages\pymoo\core\problem.py", line 302, in do
    out = self._format_dict(out, len(X), return_values_of)
  File "C:\Python3.10\lib\site-packages\pymoo\core\problem.py", line 357, in _format_dict
    raise Exception(
Exception: ('Problem Error: F can not be set, expected shape (100, 2) but provided (100, 5, 2)', ValueError('cannot reshape array of size 1000 into shape (100,2)'))

I have added many print statements in both pymoo and pysamoo source codes. I haven't been able to figure out, yet, where the library modifies the shape of the object out["F"]. So I haven't been able to debug out my error.

I suspect this comes from a bug in either pysamoo or pymoo, rather than my code, but I may be wrong. Any pointer in debugging my code is welcome.

Upvotes: 0

Views: 73

Answers (0)

Related Questions