I am using Constrained Minimize() in JSL and would like to build both the 目標函數 and the 動態設定約束集, based on which variables appear in the objective equation.
在簡單的情況下,我可以把所有內容都硬編碼進去,例如:
R_nom = 0.1;
L_nom = 0.5;
S_nom = 48.2;
B_nom = 0.05;
R_eq = 0.1;
L_eq = 0.5;
S_eq = 48.2;
B_eq = 0.05;
R_sd = :sd_R;
L_sd = :sd_L;
S_sd = :sd_S;
B_sd = :sd_B;
bias_const = 1.5;
R_bias = R_sd * bias_const;
L_bias = L_sd * bias_const;
S_bias = S_sd * bias_const;
B_bias = B_sd * bias_const;
minFun = Constrained Minimize(
(16.388 * Pi() * R_nom ^ 2 * L_nom - B_nom) * S_nom,
{R_nom( R_eq - R_Bias, R_eq + R_Bias ), L_nom( L_eq - L_Bias, L_eq + L_Bias ), S_nom( S_eq - S_Bias, S_eq + S_Bias ),
B_nom( B_eq - B_Bias, B_eq + B_Bias )},
<
<TOLERANCE></TOLERANCE>
<P>然而,<STRONG data-start="1086" data-end="1118">該等式可能會動態變化</STRONG>,例如:</P> <CODE class=" language-jsl">R_nom^2 * L_nom // instead of 16.388 * Pi() * R_nom ^ 2 * L_nom - B_nom) * S_nom
In that case, I only want constraints for R_nom and L_nom, not for B_nom or S_nom.
我想要達成的目標
-
Define the objective function as an expression (Expr()).
-
確定方程中使用的輸入變數(我已經為此創建了一個函數)。
varsUsed = {"R_nom","L_nom"}; //when equation is R_nom^2 * L_nom
-
自動僅為這些變數建立約束列表。
-
Pass the dynamically generated constraint list into Constrained Minimize().
目前方法
我將參數元資料(名目變數、平衡值、偏差)儲存在關聯數組中,例如:
paramInfo = Associative Array();
paramInfo["R_nom"] = {R_nom, R_eq, R_bias};
paramInfo["L_nom"] = {L_nom, L_eq, L_bias};
paramInfo["S_nom"] = {S_nom, S_eq, S_bias};
paramInfo["B_nom"] = {B_nom, B_eq, B_bias};
我被困住了
I am having trouble 動態建構有效的約束表達式 in JSL so that they are accepted by Constrained Minimize().
您很可能需要進行表達式求值,並混合使用名稱表達式。這或許能給您一些啟發。
Names Default To Here(1);
params = Associative Array();
params["R_nom"] = {R_nom, R_eq, R_bias};
params["L_nom"] = {L_nom, L_eq, L_bias};
params["S_nom"] = {S_nom, S_eq, S_bias};
params["B_nom"] = {B_nom, B_eq, B_bias};
vars = {"R_nom","L_nom"};
l = {};
For Each({var}, vars,
e = Substitute(Expr(nom(eq - bias, eq + bias)),
Expr(nom), Name Expr(params[var][1]),
Expr(eq), Name Expr(params[var][2]),
Expr(bias), Name Expr(params[var][3]),
);
Insert Into(l, Name Expr(e));
);
minFun = EvalExpr(
Constrained Minimize(
(16.388 * Pi() * R_nom ^ 2 * L_nom - B_nom) * S_nom,
Expr(l),
<
<TOLERANCE></TOLERANCE>