c#调用lingo 线性规划.doc_第1页
c#调用lingo 线性规划.doc_第2页
c#调用lingo 线性规划.doc_第3页
c#调用lingo 线性规划.doc_第4页
c#调用lingo 线性规划.doc_第5页
免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论