3

NLOpt の Python インターフェイスを使用して最適化を実行しています。何度か繰り返した後、特定の時点で nlopt.RoundoffLimited 例外が発生します。ドキュメント ( http://ab-initio.mit.edu/wiki/index.php/NLopt_Reference#Error_codes_.28negative_return_values.29 ) によると、このような例外の後でも、「最適化は通常、有用な結果を返します」。実際に中間結果を表示するにはどうすればよいですか? 次のようなコードを実行しています:

opt = nlopt.opt(...)
# ... some optimization settings
try:
    opt_results = opt.optimize(guess)
except nlopt.RoundoffLimited:
    # How do I get the latest parameters from opt,
    # after the optimization has failed?

を使用して目的の値をうまく取得できopt.last_optimize_result()ますが、この目的の値になるパラメーターを取得するための API 呼び出しが見つかりません。

ありがとう!

4

1 に答える 1

0

私はまだ特にエレガントな解決策を見つけていませんが、誰かが同じ問題に遭遇した場合に備えて、これを投稿します. 最適化の例外が発生する前に、以前の有効な最適化パラメーターを回復する 1 つの方法を次に示します。

# globals
previous_args = None
current_args = None

# objective function
def objective_function(args, gradient):
    global previous_args
    global current_args

    previous_args = current_args
    current_args = args

    # ... the rest of your objective function
    return function_value

# optimization code using objective_function
opt = nlopt.opt(...)

try:
    args = opt.optimize(guess)
except nlopt.RoundoffLimited:
    args = previous_args

    # you should do some sanity checks on args.
    # for example, one reason you may see RoundoffLimited
    # is args on the order of 1e-300 or so.
于 2014-03-31T01:06:00.523 に答える