1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Platypus(遺伝的アルゴリズム)で整数と実数を一緒に使う魔法の呪文

Last updated at Posted at 2020-11-30

Platypusで整数と実数を同時に使うと

Platypusで、整数を使う時はInteger、実数を使う時はRealを使って設定をしますが、両方を一緒に使うと次のエラーが出ます。

PlatypusError: must define variator for mixed types

なんか、variatorを定義しなさいと言うのですが、日本語のサイトには情報がありません。皆さんどちらかだけを使った例を書いています。
でも、実際には、整数と実数が混ざって使いたいことはよくあると思うのですが、エラーが出てどうしようもありません。

Platypusに書く魔法の呪文

いろいろ探していたら元々のGithubの英語のサイトに答えがありました。

いろいろ書いてあるのですが、たぶんこれをalgorithmの中に書くだけで動くようです。
variator=CompoundOperator(SBX(), HUX(), PM(), BitFlip()

これで、整数と実数が使えるようになります。

このプログラムは、そこのサイトにあったプログラムです。


from platypus import *

def mixed_type(x):
    print("Evaluating", x)
    return [x[0], x[1]]

problem = Problem(2, 2)
problem.types[0] = Real(0, 10)
problem.types[1] = Integer(0, 10)
problem.function = mixed_type

algorithm = NSGAII(problem, variator=CompoundOperator(SBX(), HUX(), PM(), BitFlip()))
algorithm.run(10000)

print("Final solutions:")
for solution in unique(nondominated(algorithm.result)):
    print(solution.variables, solution.objectives)
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?