> Tutorials > Concert Technology Tutorial for .NET Users > Example: LPex1.cs

// --------------------------------------------------------------------------
// File: examples/src/LPex1.cs
// Version 9.0    
// --------------------------------------------------------------------------
//  Copyright (C) 2001-2003 by ILOG.
//  All Rights Reserved.
//  Permission is expressly granted to use this example in the
//  course of developing applications that use ILOG products.
// --------------------------------------------------------------------------
//
// LPex1.cs - Entering and optimizing an LP problem
// 
// Demonstrates different methods for creating a problem.  The user has to
// choose the method on the command line:
// 
//    LPex1  -r     generates the problem by adding constraints
//    LPex1  -c     generates the problem by adding variables
//    LPex1  -n     generates the problem by adding expressions
// 

using ILOG.CONCERT;
using ILOG.CPLEX;


public class LPex1 {
   internal static void Usage() {
      System.Console.WriteLine("usage:   LPex1 <option>");
      System.Console.WriteLine("options: -r build model row by row");
      System.Console.WriteLine("options: -c build model column by column");
      System.Console.WriteLine("options: -n build model nonzero by nonzero");
   }

   public static void Main(string[] args) {
      if ( args.Length != 1 || args[0].ToCharArray()[0] != `-' ) {
         Usage();
         return;
      }
    
      try {
         // Create the modeler/solver object
         Cplex cplex = new Cplex();
       
         INumVar[][] var = new INumVar[1][];
         IRange[][]  rng = new IRange[1][];
       
         // Evaluate command line option and call appropriate populate method.
         // The created ranges and variables are returned as element 0 of 
arrays
         // var and rng.
         switch ( args[0].ToCharArray()[1] ) {
         case `r': PopulateByRow(cplex, var, rng);
                   break;
         case `c': PopulateByColumn(cplex, var, rng);
                   break;
         case `n': PopulateByNonzero(cplex, var, rng);
                   break;
         default:  Usage();
                   return;
         }
       
         // write model to file
         cplex.ExportModel("lpex1.lp");
       
         // solve the model and display the solution if one was found
         if ( cplex.Solve() ) {
            double[] x     = cplex.GetValues(var[0]);
            double[] dj    = cplex.GetReducedCosts(var[0]);
            double[] pi    = cplex.GetDuals(rng[0]);
            double[] slack = cplex.GetSlacks(rng[0]);
          
            cplex.Output().WriteLine("Solution status = " + cplex.GetStatus());
            cplex.Output().WriteLine("Solution value  = " + cplex.ObjValue);
          
            int ncols = cplex.Ncols;
            for (int j = 0; j < ncols; ++j) {
               cplex.Output().WriteLine("Column: " 
                                         + j +" Value = " 
                                         + x[j] 
                                         +" Reduced cost = " 
                                         + dj[j]);
            }
          
            int nrows = cplex.Nrows;
            for (int i = 0; i < nrows; ++i) {
               cplex.Output().WriteLine("Row   : " + i 
                                         +" Slack = " 
                                         + slack[i] 
                                         +" Pi = " 
                                         + pi[i]);
            }
         }
         cplex.End();
      }
      catch (ILOG.CONCERT.Exception e) {
         System.Console.WriteLine("Concert exception `" + e + "` caught");
      }
   }


   // The following methods all populate the problem 
   // with data for the following linear program:
   //
   //    Maximize
   //     x1 + 2 x2 + 3 x3
   //    Subject To
   //     - x1 + x2 + x3 <= 20
   //     x1 - 3 x2 + x3 <= 30
   //    Bounds
   //     0 <= x1 <= 40
   //    End
   //
   // using the IMPModeler API

   internal static void PopulateByRow(IMPModeler model,
                             INumVar[][] var,
                             IRange[][] rng) {
      double[]    lb = {0.0, 0.0, 0.0};
      double[]    ub = {40.0, 
                        System.Double.MaxValue, 
                        System.Double.MaxValue};
      INumVar[] x  = model.NumVarArray(3, lb, ub);
      var[0] = x;
    
      double[] objvals = {1.0, 2.0, 3.0};
      model.AddMaximize(model.ScalProd(x, objvals));
    
      rng[0] = new IRange[2];
      rng[0][0] = model.AddLe(model.Sum(model.Prod(-1.0, x[0]),
                                        model.Prod( 1.0, x[1]),
                                        model.Prod( 1.0, x[2])), 20.0);
      rng[0][1] = model.AddLe(model.Sum(model.Prod( 1.0, x[0]),
                                        model.Prod(-3.0, x[1]),
                                        model.Prod( 1.0, x[2])), 30.0);
   }

   internal static void PopulateByColumn(IMPModeler model,
                                INumVar[][] var,
                                IRange[][] rng) {
      IObjective obj = model.AddMaximize();
    
      rng[0] = new IRange[2];
      rng[0][0] = model.AddRange(-System.Double.MaxValue, 20.0);
      rng[0][1] = model.AddRange(-System.Double.MaxValue, 30.0);
    
      IRange r0 = rng[0][0];
      IRange r1 = rng[0][1];
    
      var[0] = new INumVar[3];
      var[0][0] = model.NumVar(model.Column(obj,  1.0).And(
                               model.Column(r0,  -1.0).And(
                               model.Column(r1,   1.0))),
                               0.0, 40.0);
      var[0][1] = model.NumVar(model.Column(obj,  2.0).And(
                               model.Column(r0,   1.0).And(
                               model.Column(r1,  -3.0))),
                               0.0, System.Double.MaxValue);
      var[0][2] = model.NumVar(model.Column(obj,  3.0).And(
                               model.Column(r0,   1.0).And(
                               model.Column(r1,   1.0))),
                               0.0, System.Double.MaxValue);
   }

   internal static void PopulateByNonzero(IMPModeler model,
                                 INumVar[][] var,
                                 IRange[][] rng) {
      double[]    lb = {0.0, 0.0, 0.0};
      double[]    ub = {40.0, 
                        System.Double.MaxValue, 
                        System.Double.MaxValue};
      INumVar[] x  = model.NumVarArray(3, lb, ub);
      var[0] = x;
    
      double[] objvals = {1.0, 2.0, 3.0};
      model.Add(model.Maximize(model.ScalProd(x, objvals)));
    
      rng[0] = new IRange[2];
      rng[0][0] = model.AddRange(-System.Double.MaxValue, 20.0);
      rng[0][1] = model.AddRange(-System.Double.MaxValue, 30.0);
    
      rng[0][0].Expr = model.Sum(model.Prod(-1.0, x[0]),
                                 model.Prod( 1.0, x[1]),
                                 model.Prod( 1.0, x[2]));
      rng[0][1].Expr = model.Sum(model.Prod( 1.0, x[0]),
                                 model.Prod(-3.0, x[1]),
                                 model.Prod( 1.0, x[2]));
   }
}