飞扬6666 2024-09-16 17:33 采纳率: 75%
浏览 13
已结题

C#调用lingo怎么设置全局最优选项

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秒都是可以接受的,要求尽量全局最优

  • 写回答

2条回答 默认 最新

  • 月下独码 Java领域新星创作者 2024-09-16 17:40
    关注

    要在C#中调用Lingo并设置全局最优选项,你需要通过Lingo的API来设置Lingo的选项。Lingo提供了一系列的API函数来控制Lingo的行为,包括设置求解器的选项。

    具体步骤

    1. 设置全局求解器选项

      • 你需要使用 LSsetOptionLng 函数来设置Lingo的选项。具体来说,你需要设置 LS_PAR_SOLVER 选项为 LS_PAR_SOLVER_GLOBAL 来启用全局求解器。
    2. 设置求解时间

      • 你可以通过设置 LS_PAR_MAXTIME 选项来控制求解时间。将这个选项设置为你希望的最大求解时间(以秒为单位)。

    代码示例

    以下是如何在你的C#代码中设置这些选项的示例:

    using System;
    using System.Runtime.InteropServices;
    
    class Program
    {
        // 定义Lingo的API函数
        [DllImport("lingo15_x64.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int LScreateEnvLng(ref IntPtr pEnv, string pName);
    
        [DllImport("lingo15_x64.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int LSopenLogFileLng(IntPtr pEnv, string pFileName);
    
        [DllImport("lingo15_x64.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int LSexecuteScriptLng(IntPtr pEnv, string pScript);
    
        [DllImport("lingo15_x64.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int LScloseLogFileLng(IntPtr pEnv);
    
        [DllImport("lingo15_x64.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int LSsetPointerLng(IntPtr pEnv, IntPtr pPointer, ref int nPointersNow);
    
        [DllImport("lingo15_x64.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int LSsetOptionLng(IntPtr pEnv, int nOption, double dValue);
    
        [DllImport("lingo15_x64.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int LSgetPointerLng(IntPtr pEnv, int nPointer, ref IntPtr pPointer);
    
        const int LS_PAR_SOLVER = 1001;
        const int LS_PAR_SOLVER_GLOBAL = 2;
        const int LS_PAR_MAXTIME = 1003;
    
        static void Main(string[] args)
        {
            IntPtr pLingoEnv = IntPtr.Zero;
            int nError = LScreateEnvLng(ref pLingoEnv, "LingoEnv");
            if (nError != 0) goto ErrorExit;
    
            // 设置全局求解器选项
            nError = LSsetOptionLng(pLingoEnv, LS_PAR_SOLVER, LS_PAR_SOLVER_GLOBAL);
            if (nError != 0) goto ErrorExit;
    
            // 设置最大求解时间为20秒
            nError = LSsetOptionLng(pLingoEnv, LS_PAR_MAXTIME, 20);
            if (nError != 0) goto ErrorExit;
    
            // 你的其他代码...
    
            string cScript = "set echoin 1 \n take simple.lng \n go \n quit \n";
            nError = LSexecuteScriptLng(pLingoEnv, cScript);
            if (nError != 0) goto ErrorExit;
    
            lingo.LScloseLogFileLng(pLingoEnv);
    
            return;
    
        ErrorExit:
            Console.WriteLine("Error: " + nError);
        }
    }
    

    解释

    1. LSsetOptionLng

      • LS_PAR_SOLVER 是选项ID,表示求解器类型。
      • LS_PAR_SOLVER_GLOBAL 是全局求解器的值。
      • LS_PAR_MAXTIME 是选项ID,表示最大求解时间。
      • 20 是最大求解时间的值,单位为秒。
    2. LSexecuteScriptLng

      • 执行Lingo脚本,包括加载模型文件、运行求解和退出。

    通过以上代码,你可以确保Lingo使用全局求解器,并且在20秒内尝试找到全局最优解。如果你希望进一步调整求解器的参数,可以参考Lingo的API文档,了解更多可用的选项和参数。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月28日
  • 已采纳回答 9月20日
  • 创建了问题 9月16日

悬赏问题

  • ¥25 TABLEAU PREP无法打开
  • ¥15 关于#c语言#的问题:求完整代码条件好说
  • ¥15 (需要远程,AI不回)VB6二进制文件转换成功,但是C#转换总是失败
  • ¥15 关于#matlab#的问题:有没有什么其他办法能够保证不退出进程(相关搜索:matlab调用)
  • ¥15 依据报错在原代吗格式的基础上解决问题
  • ¥15 在虚拟机中安装flash code
  • ¥15 单片机stm32f10x编写光敏电阻调节3.3伏大功率灯亮度(光强越大灯越暗,白天正常光强灯不亮,使用ADC,PWM等模块)望各位找一下错误或者提供一个可实现功能的代码
  • ¥20 verilog状态机方法流水灯
  • ¥15 pandas代码实现不了意图
  • ¥15 GD32H7 从存储器到外设SPI传输数据无法重复启用DMA