Warm tip: This article is reproduced from serverfault.com, please click

Pymoo Optimization problem for binary variables and constraints

发布于 2020-11-29 01:11:52

I am very new to this nice optimization package pymoo. After going through the documents, I still can't implement this package for my specific problem. Here are the objective functions and constraints:

Objective:

       1) min(f1); 2) min(f2)

Constraints

      1)x =0 or 1; 2) sum(x)=9

Here is my current code:

class MyProblem(Problem):

  def __init__(self):
      super().__init__(n_var=13,
                     n_obj=2,
                     n_constr=1,
                     xl=0,
                     xu=1)
  def first_objective_function(self, x):
      y = ''my main function''
      return y
    
  def second_objective_function(self, x):
          y = ''my second main function''
          return y

  def _evaluate(self, X, out, *args, **kwargs):
          f1 = self.first_objective_function(X)
          f2 = self.second_objective_function(X)
          g = ???????
          out["F"] = np.column_stack([f1, f2])
          out["G"] = np.column_stack([g])

my_problem = MyProblem()

algorithm = NSGA2(
           pop_size=40,
           n_offsprings=10,
           sampling=get_sampling("bin_random"),
           crossover=get_crossover("bin_hux", prob=0.9, eta=15),
           mutation=get_mutation("bin_bitflip", eta=20),
           eliminate_duplicates=True)

res = minimize(my_problem,
           algorithm,
           ('n_gen', 100),
           verbose=False)

My question is how to define my constraint equation. The sum(X) equals a value, not less than a value.

The second question is, am I using the correct sampling, cross-over, and mutation algorithm for this problem? I hope the number of 1 in the x is fixed.

Questioner
Xudong
Viewed
0
Julian 2020-11-30 22:35:40

In general, to attempt to solve a subset selection problem. This can be done by customization of a genetic algorithm. I have already written up an example for Subset Selection. All evolutionary operators take into account the maximum number of 1's in the array. This means the constraint will never be violated. In general, you could also define a Repair operator which zeros out all ones violating the threshold.

However, the code might need some more customization for your specific problem but feel free to use it as a blueprint.