> Discrete Optimization > Rates: Using Semi-Continuous Variables > Complete Program

You can see the entire program here or on-line in the standard distribution of ILOG CPLEX in the file /examples/src/rates.cpp. To run that example, you need a license for ILOG CPLEX.

// File: examples/src/rates.cpp
// Version 9.0    
// --------------------------------------------------------------------------
//  Copyright (C) 1999-2003 by ILOG.
//  All Rights Reserved.
//  Permission is expressly granted to use this example in the
//  course of developing applications that use ILOG products.
// --------------------------------------------------------------------------
//
// rates.cpp -- modeling with semi-continuous variables
//
// Problem Description:
// 
// Assume you run a power supply company.  You have several power generators
// available, each of which has a minimum and maximum production level 
// and a cost per unit output.  
// The question is which generators to use in order to
// minimize the overall operation cost while satisfying the demand.
// 
 
#include <ilcplex/ilocplex.h>
#include <assert.h>
#endif
ILOSTLBEGIN

int
main(int argc, char** argv)
{
   IloEnv env;
   try {
      IloNumArray minArray(env), maxArray(env), cost(env);
      IloNum demand;
      if ( argc < 2 ) {
         env.warning() 
           << "Default data file : ../../../examples/data/rates.dat" << endl;
         ifstream in("../../../examples/data/rates.dat");
         in >> minArray >> maxArray >> cost >> demand;
      } else {
         ifstream in(argv[1]);
         in >> minArray >> maxArray >> cost >> demand;
      }

      IloModel mdl(env);

      IloNumVarArray production(env);
      IloInt generators = minArray.getSize();
      for (IloInt j = 0; j < generators; ++j) {
         production.add(IloSemiContVar(env, minArray[j], maxArray[j]));
      }

      mdl.add(IloMinimize(env, IloScalProd(cost, production)));
      mdl.add(IloSum(production) >= demand);

      IloCplex cplex(mdl);
      cplex.exportModel("rates.lp");
      if (cplex.solve()) {
         for (IloInt j = 0; j < generators; ++j) {
            cplex.out() << "   generator " << j << ": "
                        << cplex.getValue(production[j]) << endl;
         }
         cplex.out() << "Total cost = " << cplex.getObjValue() << endl;
      }
      else cplex.out()<< "No solution" << endl;
      cplex.printTime();
   }
   catch (IloException& ex) {
      cerr << "Error: " << ex << endl;
   }
   catch (...) {
      cerr << "Error" << endl;
   }
   env.end();
   return 0;
}