HiroIshida / snippets

fraction of codes which may be grepped later
6 stars 2 forks source link

Use snopt with ifopt #55

Open HiroIshida opened 4 months ago

HiroIshida commented 4 months ago

動機

ETH の https://github.com/ethz-adrl/towr を試したい. 内部ではhttps://github.com/ethz-adrl/ifoptを使っている. ifoptはipoptとsnoptのどちらかを使えるようになっている. open sourceのipoptがデフォルトになっている. しかし, ipoptは内点法ベースなのでwarm-startがやりにくい. なのでsnopt (sqp)でwarm-startを試したい. 著者もそういっている: https://github.com/ethz-adrl/ifopt/issues/67

入手方法/ビルド

ここに連絡して試供版をもらえる. 昔試供版をダウンロードしたことがあったのだが、もう一度試したいとかいて送ったらまた送ってもらえた.
https://ccom.ucsd.edu/~optimizers/downloads/request/ メールではライセンスファイルsnopt7.licが送られてくる. ソフト自体は https://ccom.ucsd.edu/~optimizers/downloads/software/academic/?id=0a6f613de5da からダウンロードする(一番下のc++ version).

ライセンスファイルのありかは環境変数で指定する.

export SNOPT_LICENSE=/home/h-ishida/cpp/snopt/snopt7.lic

理由は不明 (もしかして古いsnoptバージョンを想定してる?)だが, snoptのためのcmakeは https://github.com/ethz-adrl/ifopt/blob/master/ifopt_snopt/cmake/FindSNOPT.cmake#L24 にあるように, lib, includeディレクトリがあることを想定している. しかし, downloadしてきたsnoptは次のようなフラットなディレクトリになっている.

h-ishida@azarashi:~/Downloads/tmp$ tree
.
├── libsnopt7_cpp.a
├── libsnopt7_cpp.so
├── snopt_cwrap.h
├── snopt.h
├── snoptProblem.hpp
├── sqopt_cwrap.h
├── sqopt.h
└── sqoptProblem.hpp

このままではビルドできないので, 次のように整形する:

h-ishida@azarashi:~/cpp/snopt$ tree
.
├── include
│   ├── snopt_cwrap.h
│   ├── snopt.h
│   ├── snoptProblem.hpp
│   ├── sqopt_cwrap.h
│   ├── sqopt.h
│   └── sqoptProblem.hpp
└── lib
    ├── libsnopt7_cpp.a
    ├── libsnopt7_cpp.so
    └── libsnopt7.so -> libsnopt7_cpp.so

また, ifoptの説明書にあるように, 次のように環境変数をexport

export SNOPT_DIR=/home/h-ishida/cpp/snopt

あと, snopt version7以降だとそのままでは使えないので, いろいろソースコードを弄る必要がある: https://github.com/ethz-adrl/ifopt/issues/31 具体的には次のコミットのdiffを参照: https://github.com/HiroIshida/ifopt/commit/dad19fab675cf3c9764c369dbe6633de19e0b31b

これでビルドできる.

HiroIshida commented 4 months ago

snopt で warm startする方法

https://ccom.ucsd.edu/~optimizers/static/pdfs/sndoc7.pdf Start = 2にする. 与える変数はxと, xstate. xstateはupper bound lower boundがactiveがどうかを示す値だと思う.

If there is no wish to provide special information, you may set xstate(j) = 0,

と書いているので, とりあえず, 何もいれずにおく.

ifoptでwarm start

次のpatchをあてる. https://github.com/HiroIshida/ifopt/commit/532830ba60b20e76baae0a7fe66583f6cc15254f

exampleコードを二回目に解くときはsolutionを再利用するように変更すると, iterationが3から1に減ったのでこれでよさそう.

 Problem name                    snopt
 No. of iterations                   3   Objective           -4.0000000000E+00
 No. of major iterations             3   Linear    obj. term  0.0000000000E+00
 Penalty parameter          -1.111E+04   Nonlinear obj. term -4.0000000000E+00
 User function calls (total)         8
 No. of degenerate steps             0   Percentage                       0.00
 Max x                       1 1.0E+00   Max pi                      1 4.0E+00
 Max Primal infeas           0 0.0E+00   Max Dual infeas             0 0.0E+00
 Nonlinear constraint violn    0.0E+00
 No. of iterations                   1   Objective           -4.0000000000E+00
 No. of major iterations             0   Linear    obj. term  0.0000000000E+00
 Penalty parameter          -1.111E+04   Nonlinear obj. term -4.0000000000E+00
 User function calls (total)         4
 No. of degenerate steps             1   Percentage                     100.00
 Max x                       1 1.0E+00   Max pi                      1 4.0E+00
 Max Primal infeas           0 0.0E+00   Max Dual infeas             0 0.0E+00
 Nonlinear constraint violn    0.0E+00
HiroIshida commented 1 month ago

このブランチを見よ. https://github.com/HiroIshida/ifopt/tree/snopt_compat

HiroIshida commented 3 weeks ago

商用ソルバはこちら http://www.sbsi-sol-optimize.com/asp/sol_products_snopt_desc.htm#