lingo的Simple.lng代码
model:
data:
xuanze_zongshu = @pointer( 4);
enddata
sets:
xuanze/1..xuanze_zongshu/: xuanze_shuliang, xuanze_N, xuanze_P2O5, xuanze_K2O, xuanze_youjizhi, xuanze_jiage ;
endsets
data:
xuanze_N = @pointer( 6);
xuanze_P2O5 = @pointer( 7);
xuanze_K2O = @pointer( 8);
xuanze_youjizhi = @pointer( 9);
xuanze_jiage = @pointer( 10);
no_xuanze_shuangliang = @pointer( 11);
no_xuanze_N = @pointer( 12);
no_xuanze_P2O5 = @pointer( 13);
no_xuanze_K2O = @pointer( 14);
no_xuanze_youjizhi = @pointer( 15);
no_xuanze_jiage = @pointer( 16);
shejiyangfen_N = @pointer( 17);
shejiyangfen_P2O5 = @pointer( 18);
shejiyangfen_K2O = @pointer( 19);
shejiyangfen_zong = @pointer( 20);
shejiyangfen_youjizhi = @pointer( 21);
shesunlv_N = @pointer( 22);
shesunlv_P2O5 = @pointer( 23);
shesunlv_K2O = @pointer( 24);
shesunlv_youjizhi = @pointer( 25);
zongliang = @pointer( 26);
zongliang_fanwei = @pointer( 27);
shifou_jiage = @pointer( 28);
enddata
shuliang=@sum(xuanze(i) : xuanze_shuliang(i) ) + no_xuanze_shuangliang;
@abs(shuliang-zongliang)<(zongliang_fanwei+0.03);
N = @sum (xuanze(i) : xuanze_shuliang(i) * xuanze_N(i)/100) + no_xuanze_N;
P2O5 = @sum ( xuanze(i) : xuanze_shuliang(i) * xuanze_P2O5(i)/100) + no_xuanze_P2O5;
K2O = @sum ( xuanze(i) : xuanze_shuliang(i) * xuanze_K2O(i)/100) + no_xuanze_K2O;
youjizhi = @sum ( xuanze(i) : xuanze_shuliang(i) * xuanze_youjizhi(i)/100) + no_xuanze_youjizhi;
jiage = @sum ( xuanze(i) : xuanze_shuliang(i) * xuanze_jiage(i)) + no_xuanze_jiage;
shiji_N=N/ (shuliang+0.01)*100;
shiji_P2O5=P2O5/ (shuliang+0.01)*100;
shiji_K2O=K2O/ (shuliang+0.01)*100;
shiji_youjizhi=youjizhi/ (shuliang+0.01)*100;
shiji_jiage=jiage/ (shuliang+0.01);
chengpin_N=shiji_N*(1+shesunlv_N/100);
chengpin_P2O5=shiji_P2O5*(1+shesunlv_P2O5/100);
chengpin_K2O=shiji_K2O*(1+shesunlv_K2O/100);
chengpin_zong=chengpin_N + chengpin_P2O5 + chengpin_K2O;
chengpin_youjizhi=shiji_youjizhi*(1+shesunlv_youjizhi/100);
!@for(xuanze(i) :@gin(xuanze_shuliang(i)));
!@for(xuanze(i) :xuanze_shuliang(i)<shuliang);
kong_yangfen=0.25;
kong_beishu=1000;
abs_N = @abs(chengpin_N - shejiyangfen_N) ;
lin_N=@if( shifou_jiage #eq# -1 , abs_N ^2 ,@if( abs_N #le# kong_yangfen , abs_N ^2, abs_N *kong_beishu));
abs_P2O5 = @abs(chengpin_P2O5 - shejiyangfen_P2O5) ;
lin_P2O5=@if( shifou_jiage #eq# -1 , abs_P2O5 ^2 ,@if( abs_P2O5 #le# kong_yangfen , abs_P2O5 ^2, abs_P2O5 *kong_beishu));
abs_K2O = @abs(chengpin_K2O - shejiyangfen_K2O) ;
lin_K2O=@if( shifou_jiage #eq# -1 , abs_K2O ^2 ,@if( abs_K2O #le# kong_yangfen , abs_K2O ^2, abs_K2O *kong_beishu));
abs_zong = @abs(chengpin_zong - shejiyangfen_zong) ;
lin_zong=@if( shifou_jiage #eq# -1 , abs_zong ^2*5 ,@if( abs_zong #le# kong_yangfen/5 , abs_zong ^2*5, abs_zong*5 *kong_beishu));
abs_youjizhi = @abs(chengpin_youjizhi - shejiyangfen_youjizhi) ;
lin_youjizhi=@if( shifou_jiage #eq# -1 , abs_youjizhi ^2 ,@if( abs_youjizhi #le# kong_yangfen , abs_youjizhi ^2, abs_youjizhi *kong_beishu));
[myzhi] min =lin_N + lin_P2O5 + lin_K2O + lin_zong + lin_youjizhi + @if( shifou_jiage #eq# -1 , 0 ,shiji_jiage) ;
data:
@pointer(2) = @status();
@pointer(3) = myzhi;
@pointer(5) = xuanze_shuliang;
enddata
end
C#调用的部分代码:
string strComputers = "STANDARD\nTURBO";
for (int i = 0; i < strComputers.Length; i++) pcComputers[i] = (byte)strComputers[i];
pcComputers[strComputers.Length] = 0;
//默认的行---1
nError = lingo.LSsetPointerLng(pLingoEnv, pcComputers, ref nPointersNow);if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
//默认的行---2
nError = lingo.LSsetPointerLng(pLingoEnv, &dStatus, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
//返回误差值---3
nError = lingo.LSsetPointerLng(pLingoEnv, &jisuan_wucha, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
//选择项总数---4
nError = lingo.LSsetPointerLng(pLingoEnv, &xuanze_zongshu, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
//需要求解的变量---5
nError = lingo.LSsetPointerLng(pLingoEnv, Lingo_xuanze_shuliang, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
//选择项---6~10
nError = lingo.LSsetPointerLng(pLingoEnv, Lingo_xuanze_N, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, Lingo_xuanze_P2O5, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, Lingo_xuanze_K2O, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, Lingo_xuanze_youjizhi, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, Lingo_xuanze_jiage, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
//没有选择的总量---11~16
nError = lingo.LSsetPointerLng(pLingoEnv, &no_xuanze_shuliang, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, &no_xuanze_N, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, &no_xuanze_P2O5, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, &no_xuanze_K2O, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, &no_xuanze_youjizhi, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, &no_xuanze_jiage, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
//设计含量---17~21
nError = lingo.LSsetPointerLng(pLingoEnv, &shejiyangfen_N, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, &shejiyangfen_P2O5, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, &shejiyangfen_K2O, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, &shejiyangfen_zong, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, &shejiyangfen_youjizhi, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
//升损率---22~25
nError = lingo.LSsetPointerLng(pLingoEnv, &shesunlv_N, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, &shesunlv_P2O5, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, &shesunlv_K2O, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
nError = lingo.LSsetPointerLng(pLingoEnv, &shesunlv_youjizhi, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
//数量总量---26
double zongliang = 1000;
nError = lingo.LSsetPointerLng(pLingoEnv, &zongliang, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
//数量总量控制范围---27
double zongliang_fanwei = 0;
nError = lingo.LSsetPointerLng(pLingoEnv, &zongliang_fanwei, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
//是否价格控制---28
double shifou_jiage = 0;
nError = lingo.LSsetPointerLng(pLingoEnv, &shifou_jiage, ref nPointersNow); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
string cScript = "set echoin 1 \n take simple.lng \n go \n quit \n";
nError = lingo.LSexecuteScriptLng(pLingoEnv, cScript); if (nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit;
lingo.LScloseLogFileLng(pLingoEnv);
这个编程目的是,肥料投入最优解问题,但是dStatus返回大多是:
4 --- Feasible(可行)
6 --- Local Optimum(局部最优)
有时候还会出现:9 --- Numeric Error(求解器因在某约束中遇到无定义的算术运算而停止)
这些求解大部分经过我验证并不是最优的,我想C#中设置一下lingo软件中的设置:
lingo->options->Global Solver, 勾上Use Global Options,应用
我C#代码怎么写,我这个lingo计算过程太快了,我并不要求2秒就完成,20秒都是可以接受的,要求尽量全局最优