免费预览已结束,剩余2页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#调用lingo第一个cs文件using System;using System.Text;using System.Runtime.InteropServices;public class lingo /* * Macro Definitions * */ public static int LSERR_NO_ERROR_LNG = 0; public static int LSERR_OUT_OF_MEMORY_LNG = 1; public static int LSERR_UNABLE_TO_OPEN_LOG_FILE_LNG = 2; public static int LSERR_INVALID_NULL_POINTER_LNG = 3; public static int LSERR_INVALID_INPUT_LNG = 4; public static int LSERR_INFO_NOT_AVAILABLE_LNG = 5; public static int LS_IINFO_VARIABLES_LNG = 0; public static int LS_IINFO_VARIABLES_INTEGER_LNG = 1; public static int LS_IINFO_VARIABLES_NONLINEAR_LNG = 2; public static int LS_IINFO_CONSTRAINTS_LNG = 3; public static int LS_IINFO_CONSTRAINTS_NONLINEAR_LNG = 4; public static int LS_IINFO_NONZEROS_LNG = 5; public static int LS_IINFO_NONZEROS_NONLINEAR_LNG = 6; public static int LS_IINFO_ITERATIONS_LNG = 7; public static int LS_IINFO_BRANCHES_LNG = 8; public static int LS_DINFO_SUMINF_LNG = 9; public static int LS_DINFO_OBJECTIVE_LNG =10; public static int LS_DINFO_MIP_BOUND_LNG =11; public static int LS_DINFO_MIP_BEST_OBJECTIVE_LNG =12; public static int LS_STATUS_GLOBAL_LNG = 0; public static int LS_STATUS_INFEASIBLE_LNG = 1; public static int LS_STATUS_UNBOUNDED_LNG = 2; public static int LS_STATUS_UNDETERMINED_LNG = 3; public static int LS_STATUS_INFORUNB_LNG = 5; public static int LS_STATUS_LOCAL_LNG = 6; public static int LS_STATUS_LOCAL_INFEASIBLE_LNG = 7; public static int LS_STATUS_CUTOFF_LNG = 8; public static int LS_STATUS_NUMERIC_ERROR_LNG = 9;/* * * * Function Prototypes * * * */ DllImport( lingd90.dll, EntryPoint = LSclearPointersLng) public static extern int LSclearPointersLng( int pLingoEnv); DllImport( lingd90.dll, EntryPoint = LScloseLogFileLng) public static extern int LScloseLogFileLng( int pLingoEnv); DllImport( lingd90.dll, EntryPoint = LScreateEnvLng) public static extern int LScreateEnvLng( ); DllImport( lingd90.dll, EntryPoint = LSdeleteEnvLng) public static extern int LSdeleteEnvLng( int pLingoEnv); DllImport( lingd90.dll, EntryPoint = LSexecuteScriptLng) public static extern int LSexecuteScriptLng( int pLingoEnv, string pcScript); DllImport( lingd90.dll, EntryPoint = LSgetCallbackInfoLng) public static extern int LSgetCallbackInfoLng( int pLingoEnv, int nObject, ref int pnResult); DllImport( lingd90.dll, EntryPoint = LSopenLogFileLng) public static extern int LSopenLogFileLng( int pLingoEnv, string pcLogFile); DllImport( lingd90.dll, EntryPoint = LSsetCallbackSolverLng) public static extern int LSsetCallbackSolverLng( int pLingoEnv, lingo.typCallback pSolverCallbackFunction, MarshalAs(UnmanagedType.AsAny) object pMyData); DllImport( lingd90.dll, EntryPoint = LSsetPointerLng) public static extern int LSsetPointerLng( int pLingoEnv, ref double pdPointer, ref int pnPointersNow); public delegate int typCallback( int pLingoEnv, int nReserved, IntPtr pUserData); 第二个cs文件-程序入口using System;using System.Text;using System.IO;using System.Runtime.InteropServices;using System.Runtime.Remoting;namespace Simple StructLayout( LayoutKind.Sequential) public class CallbackData public int nIterations; / Constructor: public CallbackData() nIterations = 0; class Class1/ Illustrates calling the Lingo DLL from C#.NET to/ solve the simple product mix model:/ Max 100 Standard + 150 Turbo;/ S.T./ (X)Standard = 100/ (Y)Turbo = 120/ Standard + 2 * Turbo 160;/ / Note: The model template file, lingo9samplessimple.lng,/ must be present in order to run this example./ STAThreadstatic void Main(string args) int pLingoEnv; int nError=-1, nPointersNow=-1; double dObjective=-1, dStatus=-1; / Get a pointer to a Lingo environment pLingoEnv = lingo.LScreateEnvLng(); if ( pLingoEnv = 0) Console.WriteLine( Unable to create Lingo environment.n); goto FinalExit; / Open LINGOs log file nError = lingo.LSopenLogFileLng( pLingoEnv, lingo.log); if ( nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit; / Let Lingo know we have a callback function CallbackData cbd = new CallbackData(); lingo.typCallback cb = new lingo.typCallback( LngCallback.MyCallback); /回调函数 nError = lingo.LSsetCallbackSolverLng( pLingoEnv, cb, cbd); if ( nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit; / must pin lingos transfer areas in memory unsafe fixed( / Model data that gets referenced by the / template model, simple.lng double* dProfit = new double 2, dLimit = new double 2, dLabor = new double 2, dProduce = new double 2) dProfit0 = 100; dProfit1 = 150;/对应两种设备的单价 dLimit0 = 100; dLimit1 = 120; /两种设备数量限制 dLabor0 = 1; dLabor1 = 2; /两种设备的工时 / Pass Lingo the pointer to the objective coefficients (refer / to the template model, simple.lng) nError = lingo.LSsetPointerLng( pLingoEnv, ref dProfit0, ref nPointersNow); if ( nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit; / Pass a pointer to the production limits nError = lingo.LSsetPointerLng( pLingoEnv, ref dLimit0, ref nPointersNow); if ( nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit; / Pointer to the labor utilization coefficients nError = lingo.LSsetPointerLng( pLingoEnv, ref dLabor0, ref nPointersNow); if ( nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit; / Point to dObjective, where Lingo will return the objective value nError = lingo.LSsetPointerLng( pLingoEnv, ref dObjective, ref nPointersNow); if ( nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit; / Pointer to the solution status code nError = lingo.LSsetPointerLng( pLingoEnv, ref dStatus, ref nPointersNow); if ( nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit; / Point to the variable value array nError = lingo.LSsetPointerLng( pLingoEnv, ref dProduce0, ref nPointersNow); if ( nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit; / Here is the script we want LINGO to run. string cScript = set echoin 1 n take lingo9samplessimple.lng n go n quit n; / Run the script nError = lingo.LSexecuteScriptLng( pLingoEnv, cScript); if ( nError != lingo.LSERR_NO_ERROR_LNG) goto ErrorExit; / Close the log file lingo.LScloseLogFileLng( pLingoEnv); / Any problems? if ( nError != 0 | dStatus != lingo.LS_STATUS_GLOBAL_LNG) / Had a problem Console.WriteLine( Unable to solve!); else / Everything went OK . print results Console.WriteLine( nStandards: 0 nTurbos: 1 nnProfit: 2 n, dProduce0, dProduce1, dObjective); goto NormalExit; ErrorExit: Console.WriteLine( LINGO Error Code: 0n, nError); NormalExit: / Free Lingos envvironment to avoid a memory leak lingo.LSdeleteEnvLng( pLingoEnv); FinalExit: Console.Writ
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 湖北襄阳市第七中学2026-2027学年八年级上学期11月第二次学情检测道德与法治试题(无答案)
- 优酷运营编辑面试题及详细答案
- 广西南宁市宏德高级中学2027届高三上学期11月自主练习语文试卷(含答案)
- 安徽省合肥市部分学校2027届高三上学期12月教学质量检测历史试卷(含答案)
- 京都大学入学考试难度评析与习题详解
- 产业投资集团面试题目及参考答题要点
- 乾隆十九年甲戌科殿试策题及对策详解
- 高一政治必修2第二单元检测试卷教学设计
- 初中数学应用题方案评估技巧训练试题及答案
- 第三单元-《我们的生活充满阳光》教学设计-2026-2027学年湘艺版(新教材)初中音乐八年级上册
- 产品设计开发控制管理规程
- 2026中国舞蹈艺术行业市场考察及产业发展与政策影响分析报告
- 2026新教材语文 五年级上册第7单元习作 我最爱的季节 教学教学课件
- 新版2026秋统编版(新版)小学道德与法治五年级上册(全册)知识点清单梳理
- 药物合成反应第五章重排反应
- 2026茯苓种植经济效益与可持续发展策略咨询报告
- 口服抗栓药物消化道损伤防治共识2026
- (高级三级)互联网营销师(直播销售员)职业技能鉴定理论考试题库及答案
- 村级报账员队伍管理
- 成立分公司开展新业务可行性研究报告
- 生产经理入职工作方案
评论
0/150
提交评论