Help us understand the problem. What is going on with this article?

# Modelicaで最適化 - OpenModelica編

More than 1 year has passed since last update.

by UedaShigenori
1 / 9

# Modelicaで最適化を試す

Modelicaで最適化(動的計画法)を行う場合いくつか選択肢がある

https://qiita.com/UedaShigenori/items/aae444326b28ccc8b20f

# Optimizationクラスを使用する

## 実行方法

このUser's Guideの内容を実施する
https://www.openmodelica.org/doc/OpenModelicaUsersGuide/latest/optimization.html

まずはOptimizationオプションを有効にする

```setCommandLineOptions("-g=Optimica")
```

inputが値を振りたい変数
optimizationクラスのobjectiveが目的関数

BatchReactor.mo
```model BatchReactor
Real x1(start =1, fixed=true, min=0, max=1);
Real x2(start =0, fixed=true, min=0, max=1);
input Real u(min=0, max=5);
equation
der(x1) = -(u+u^2/2)*x1;
der(x2) = u*x1;
end BatchReactor;
```
nmpcBatchReactor.mo
```optimization nmpcBatchReactor(objective=-x2)
extends BatchReactor;
end nmpcBatchReactor;
```

```//moファイルがあるパスをいれる
cd("C:\hogehoge")
//最適化実行
optimize(nmpcBatchReactor, numberOfIntervals=16, stopTime=1, tolerance=1e-8)
```

matファイルが出力されるのでOMEditなどで読み込む

## 分かりやすい問題作成

y=-x^2(-1<x<1)で一番yが大きくなるxの値を探す

test.mo
```model test
Real y;
input Real x(min=-1, max=1);
equation
y=-x^2;
end test;
```
optim.mo
```optimization optim(objective=y)
extends test;
end optim;
```

## 少しだけ難しい問題作成

test.mo
```model test
Real y;
input Real x(min = -1, max = 1);
equation
y=(x-10)^2;
end test;
```

とりあえずOpenModelicaのせいと考えてJModelicaで試してみる
-> ToBeContinue

CAEの普及と活用に興味があります。 プロフィールは以下から https://connpass.com/user/uedashige/
https://github.com/UedaShigenori
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away