Example #1
0
   public static void Main(string[] args) {
      if ( args.Length != 2 ) {
         Usage();
         return;
      }
      try {
         bool useLoggingCallback = false;
         bool useTimeLimitCallback = false;
         bool useAborter = false;

         Cplex.Aborter myAborter;

         switch ( args[1].ToCharArray()[0] ) {
         case 't':
            useTimeLimitCallback = true;
            break;
         case 'l':
            useLoggingCallback = true;
            break;
         case 'a':
            useAborter = true;
            break;
         default:
            Usage();
            return;
         }

         Cplex cplex = new Cplex();

         cplex.ImportModel(args[0]);
         IEnumerator matrixEnum = cplex.GetLPMatrixEnumerator();
         matrixEnum.MoveNext();
         ILPMatrix lp = (ILPMatrix)matrixEnum.Current;
         IObjective obj = cplex.GetObjective();

         // Set an overall node limit in case callback conditions
         // are not met.
         cplex.SetParam(Cplex.Param.MIP.Limits.Nodes, 5000);

         if ( useLoggingCallback ) {
            double lastObjVal =
               (obj.Sense == ObjectiveSense.Minimize ) ?
                  System.Double.MaxValue : -System.Double.MaxValue;

            cplex.Use(new LogCallback(lp.NumVars, -100000, lastObjVal));
            // Turn off CPLEX logging
            cplex.SetParam(Cplex.Param.MIP.Display, 0);
         }
         else if ( useTimeLimitCallback ) {
            cplex.Use(new TimeLimitCallback(cplex, false, cplex.CplexTime,
                                            1.0, 10.0));
         }
         else if ( useAborter ) {
            myAborter =  new Cplex.Aborter();
            cplex.Use(myAborter);
            // Typically, you would pass the Aborter object to
            // another thread or pass it to an interrupt handler,
            // and  monitor for some event to occur.  When it does,
            // call the Aborter's abort method.
            //
            // To illustrate its use without creating a thread or
            // an interrupt handler, abort immediately by calling
            // abort before the solve.
            //
            myAborter.Abort();
         }

         cplex.Solve();

         System.Console.WriteLine("Solution status = " + cplex.GetStatus());
         System.Console.WriteLine("Cplex status = " + cplex.GetCplexStatus());

         cplex.End();
      }
      catch (ILOG.Concert.Exception e) {
         System.Console.WriteLine("Concert exception caught: " + e);
      }
   }
Example #2
0
    public static void Main(string[] args)
    {
        if (args.Length != 2)
        {
            Usage();
            return;
        }
        try {
            bool useLoggingCallback   = false;
            bool useTimeLimitCallback = false;
            bool useAborter           = false;

            Cplex.Aborter myAborter;

            switch (args[1].ToCharArray()[0])
            {
            case 't':
                useTimeLimitCallback = true;
                break;

            case 'l':
                useLoggingCallback = true;
                break;

            case 'a':
                useAborter = true;
                break;

            default:
                Usage();
                return;
            }

            Cplex cplex = new Cplex();

            cplex.ImportModel(args[0]);
            IEnumerator matrixEnum = cplex.GetLPMatrixEnumerator();
            matrixEnum.MoveNext();
            ILPMatrix  lp  = (ILPMatrix)matrixEnum.Current;
            IObjective obj = cplex.GetObjective();

            // Set an overall node limit in case callback conditions
            // are not met.
            cplex.SetParam(Cplex.Param.MIP.Limits.Nodes, 5000);

            if (useLoggingCallback)
            {
                double lastObjVal =
                    (obj.Sense == ObjectiveSense.Minimize) ?
                    System.Double.MaxValue : -System.Double.MaxValue;

                cplex.Use(new LogCallback(lp.NumVars, -100000, lastObjVal));
                // Turn off CPLEX logging
                cplex.SetParam(Cplex.Param.MIP.Display, 0);
            }
            else if (useTimeLimitCallback)
            {
                cplex.Use(new TimeLimitCallback(cplex, false, cplex.CplexTime,
                                                1.0, 10.0));
            }
            else if (useAborter)
            {
                myAborter = new Cplex.Aborter();
                cplex.Use(myAborter);
                // Typically, you would pass the Aborter object to
                // another thread or pass it to an interrupt handler,
                // and  monitor for some event to occur.  When it does,
                // call the Aborter's abort method.
                //
                // To illustrate its use without creating a thread or
                // an interrupt handler, abort immediately by calling
                // abort before the solve.
                //
                myAborter.Abort();
            }

            cplex.Solve();

            System.Console.WriteLine("Solution status = " + cplex.GetStatus());
            System.Console.WriteLine("Cplex status = " + cplex.GetCplexStatus());

            cplex.End();
        }
        catch (ILOG.Concert.Exception e) {
            System.Console.WriteLine("Concert exception caught: " + e);
        }
    }