anyoptimization / anyoptimization/pysamoo

Unable to use SSANSGA2 for my problem

Open
#6 15 comments 0 reactions 1 assignee View on GitHub

@blankjul is already working on this.

Since Feb 5, 2024.

Dominant language
Python
Stars
52
Forks
16
PR merge metrics
No merged PRs in 30d

Description

Hi,
I have a multiobjective (2 objectives) optimization problem that works with pymoo, I solve a 2 constraints with 5 variables to vary (some are floats, some are ints) with a MixedVariableGA() algorithm in pymoo, and it works fine.
I wanted to try the surrogate (pysamoo) approach to see the difference. My problem should be compatible with SSANSGA2 as far as I can see.
My tries lead to an error I haven't been able to fix.

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)

Running the code leads to:

==========================================================================================
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)'))

Any pointer to what's possibly wrong is welcome.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.