⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 Concert c++ example of facility location problem. C++ concert technology is a registered trademark o
💻 CPP
字号:
/*
  Date: January 27, 2007.
  author : Gopalakrishnan Easwaran.
 
  main.cpp
*/
 
#include "parameters.h" 
#include "fSpecs.h" 
 
int main(int argc, char **argv){
  IloEnv env;
  try{
     
    /* Indices */
    IloInt cust, 
           fac;
     
    /* Max Values */
    IloInt M, 
           N;
     
    /* Parameters */
     
    IloNumArray1 demand(env), 
                 fixedCost(env);
     
    IloNumArray2 transCost(env);
     
    /* Generate Data */
    // generateData()
     
    const char* filename  = "input.txt" ;
    if (argc > 1)
      filename = argv[1];
    ifstream f_in(filename);
    if (!f_in){
      cerr << "ERROR: could not open file '" 
           << filename 
           << "' for reading" 
           << endl ;
      cerr << "usage:   " 
           << argv[0] 
           << " <f_in>" 
           << endl;
      throw(-1);
    }
    else
      cout << "File read OK!" 
           << endl;

      cout << endl << endl << endl << endl;

    f_in >> M 
         >> N 
         >> demand 
         >> fixedCost 
         >> transCost;
     
    /* Decision Variables */
     
    IloNumVarArray1 Y(env, 
                      N, 
                      0, 
                      1, 
                      ILOINT);
     
    IloNumVarArray2 X(env, 
                      N);
    for(fac=0; fac<N; fac++)
      X[fac] = IloNumVarArray1(env, 
                               M, 
                               0, 
                               IloInfinity, 
                               ILOFLOAT);
     
    IloModel model(env);
     
    /* Constraints */ 
     
    // Constraint 1  
    for(cust=0; cust<M; cust++){
      IloExpr c10(env);
      for(fac=0; fac<N; fac++)
        c10+=X[fac][cust];
      model.add(c10==1);
      c10.end();
    }
     
    // Constraint 2  
    for(fac=0; fac<N; fac++)
      for(cust=0; cust<M; cust++)
        model.add(X[fac][cust]-Y[fac]<=0);
     
    /* Objective Function */
    IloExpr obj(env);
      
    // term 1
    for(fac=0; fac<N; fac++)
      obj+=fixedCost[fac]*Y[fac];
      
    // term 2
    for(fac=0; fac<N; fac++)
      for(cust=0; cust<M; cust++)
        obj+=demand[cust]*transCost[fac][cust]*X[fac][cust];
      
    model.add(IloMinimize(env, 
                          obj));
    obj.end();
      
    /* Cplex Environment */
    IloCplex cplex(env);
    cplex.extract(model);
    cplex.solve();
      
    /* Output Values */
    cout << endl << endl << endl << endl;

    IloNum tolerance = cplex.getParam(IloCplex::EpInt);
    cplex.out() << "Optimal value: " 
                << cplex.getObjValue() 
                << endl;


     cout << "Open Facilities: " << endl;
     for(fac=0; fac<N; fac++){
          if(cplex.getValue(Y[fac])>0)
			    cout << fac+1 << endl;
        }

     cout << "Customer Assignments: " << endl;
    for(fac=0; fac<N; fac++)
      for(cust=0; cust<M; cust++){
          if(cplex.getValue(X[fac][cust])>0)
			    cout << fac+1 << "<-" << cust+1 << endl;

       }
  }
  catch(IloException& e){
    cerr << "ERROR: " 
         << e 
         << endl ;
  }
  catch(...){
    cerr << "Unknown Error had Occurred with CPLEX!" 
         << endl;
  }
    
  env.end();
  getchar();
    
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -