ContentsIndexHome
PreviousUpNext
basic.cpp

Opening the AIMMS project, assigning data, running a procedure without arguments and reading data.

  1: #include "aimms/Include.h"
  2: 
  3: 
  4: // This basic example demonstrates the code to 
  5: // - Set up the connection to a model, 
  6: // - Assign parameter data, 
  7: // - Solve the model,
  8: // - Retrieve variables
  9: 
 10: 
 11: void setParametersBasic(aimms::ISession* session) 
 12: {
 13:     // Use the insert methods to add labels to the domains sets while adding data.
 14: 
 15:     // Set supply.
 16:     aimms::IMultiDimData* parameterSupply = session->openMultiDim("Supply");
 17:     parameterSupply->insert(aimms::Tuple("London"),20.0);
 18:     parameterSupply->insert(aimms::Tuple("Paris"),30.0);
 19: 
 20:     // Close data if it is not longer needed.
 21:     parameterSupply->close();
 22: 
 23:     // Set demand.
 24:     aimms::IMultiDimData* parameterDemand = session->openMultiDim("Demand"); 
 25:     parameterDemand->insert(aimms::Tuple("Oslo"),10.0);
 26:     parameterDemand->insert(aimms::Tuple("Madrid"),15.0);
 27:     parameterDemand->insert(aimms::Tuple("Berlin"),20.0);
 28:     parameterDemand->close();
 29: 
 30:     // Use the setValue methods to add data without expanding the domain sets.
 31:     // An aimms::RuntimeException will be thrown if a label does not exist in the index domain.
 32: 
 33:     // Set transportcosts.
 34:     aimms::IMultiDimData* parameterUnitTransportCost = session->openMultiDim("UnitTransportCost");
 35:     try{
 36:         parameterUnitTransportCost->setValue(aimms::Tuple("London","Oslo"),7.0);
 37:         parameterUnitTransportCost->setValue(aimms::Tuple("London","Madrid"),12.0);
 38:         parameterUnitTransportCost->setValue(aimms::Tuple("London","Berlin"),18.0);
 39:         parameterUnitTransportCost->setValue(aimms::Tuple("Paris","Oslo"),14.0);
 40:         parameterUnitTransportCost->setValue(aimms::Tuple("Paris","Madrid"),23.0);
 41:         parameterUnitTransportCost->setValue(aimms::Tuple("Paris","Berlin"),17.0);
 42:     }catch(aimms::RuntimeException& ex){
 43:         std::cerr << "Catched a runtime exception" << ex.what() << std::endl;
 44:     }
 45:     parameterUnitTransportCost->close();
 46: }
 47: 
 48: 
 49: void runMainExecution(aimms::ISession* session)
 50: {
 51:     // Retrieve the procedure.
 52:     aimms::IProcedure* procedure = session->openProcedure("MainExecution");
 53: 
 54:     // Run the procedure.
 55:     procedure->run();
 56: 
 57:     // Close it afterwards. 
 58:     procedure->close();
 59: }
 60: 
 61: void retrieveVariablesBasic(aimms::ISession* session,std::ostream& os) 
 62: {
 63: 
 64:     // Retrieve and display the scalar variable TotalCost.
 65:     // Scalars have their own interface, and are retrieved with the openScalar method.
 66:     aimms::IScalarData* variableTotalCost = session->openScalar("TotalCost");
 67: 
 68:     double totalcost = variableTotalCost->asDouble();   
 69:     os << variableTotalCost->getName() << ": " << totalcost << std::endl<< std::endl;
 70:     variableTotalCost->close(); 
 71: 
 72:     // Retrieve and display the two dimensional variable Transport.
 73:     aimms::IMultiDimData* variableTransport = session->openMultiDim("Transport");
 74: 
 75:     // To access multidimensional data, use an iterator.
 76:     aimms::IIterator* it = variableTransport->createIterator();
 77: 
 78:     os << variableTransport->getName() << ":" << std::endl;
 79:     while (it->next()) {  // iterate through all by calling next successively
 80: 
 81:         // The tuple method gives access to the current position. 
 82:         // The asDouble method returns the value at the current position.
 83:         os << it->tuple() <<" : " << it->asDouble() << std::endl;
 84:     }
 85: 
 86:     os << std::endl;
 87: 
 88:     variableTransport->close(); // Closing a multidimensional data closes all its open iterators as well.
 89: }
 90: 
 91: int main(int argc, const char* argv[]) 
 92: {
 93:     aimms::ISession* session = 0;
 94:     if (argc != 3) {
 95:         std::cerr << "Invalid number of arguments. usage: <location of AIMMS>  <location of project>" << std::endl; 
 96:         return 1;
 97:     }  
 98: 
 99:     try {
100:         session = aimms::openSession(argv[1],argv[2]);
101: 
102:         setParametersBasic(session);
103: 
104:         runMainExecution(session);
105: 
106:         retrieveVariablesBasic(session,std::cout);
107: 
108: 
109:     } catch (std::exception& e){
110:         std::cerr << e.what();
111: 
112:         if (session) {
113:             session->clearBuffers(); // discards all buffered modifications 
114:             session->close();
115:         }
116:         return 1;
117:     }
118: 
119:     try{
120:         session->close();
121:         return 0;
122:     } catch (std::exception& e){
123:         std::cerr << e.what();
124:         return 1;
125:     }
126: }