ContentsIndexHome
PreviousUpNext
C++ Pitfalls example

The pitfall example in C++.

  1: #include "aimms/Include.h"
  2: #include <iostream>
  3: #include <assert.h>
  4: 
  5: // This example demonstrates some easily made mistakes using the SDK.
  6: 
  7: // Using set ordering. 
  8: // The SDK uses only the 'natural' Aimms ordering, which is the creation order of the labels.
  9: // See also the section on "Set element ordering" in the Execution Efficiency Cookbook chapter in the Aimms Language Reference.
 10: void useOrdinals(aimms::ISession* session)
 11: {
 12:     aimms::ISetData* setCustomers = session->openSet("Customers");
 13:     setCustomers->add("Oslo"); 
 14:     setCustomers->add("Madrid"); 
 15: 
 16:     // If the root set was completely empty until now, the order will be as expected.
 17:     aimms::ISetIterator* itCustomers = setCustomers->createIterator();
 18:     itCustomers->next();
 19:     assert( std::string("Oslo") == itCustomers->element()->getLabel());
 20:     itCustomers->next();
 21:     assert( std::string("Madrid") == itCustomers->element()->getLabel());
 22: 
 23: 
 24:     aimms::ISetData* setDepots = session->openSet("Depots");
 25:     setDepots->add("London"); 
 26:     setDepots->add("Oslo"); 
 27: 
 28:     // Since Depots and Customers share a rootset (Locations), 
 29:     // "Oslo" was added to the rootset before "London".
 30:     // It will be the first Depot!
 31:     aimms::ISetIterator* itDepots = setDepots->createIterator();
 32:     itDepots->next();
 33:     assert( std::string("Oslo") == itDepots->element()->getLabel());
 34:     itDepots->next();
 35:     assert( std::string("London") == itDepots->element()->getLabel());
 36: }
 37: 
 38: // Writing sets.
 39: // Set write actions are recursive:
 40: // - adding a label to a subset will also add it to the superset
 41: // - deleting a label from a set will also delete it from its subsets, 
 42: // - emptying a set will empty all its subsets.
 43: // - setLabels() has an implicit empty, therefore setLabels empties all the subsets
 44: // 
 45: void initializeSets(aimms::ISession* session) 
 46: {
 47:     // the data:
 48:     const char* locations[] = {"London", "Oslo" , "Madrid" };
 49:     const char* customers[] = {"Oslo", "Madrid" };
 50:     const char* depots[] = {"London", "Oslo" };
 51: 
 52:     // adding the data incorrectly:
 53:     {
 54:         aimms::ISetData* setCustomers = session->openSet("Customers");
 55:         setCustomers->setLabels(customers,2); // This will also add Oslo and Madrid to the superset Locations.
 56:         aimms::ISetData* setDepots = session->openSet("Depots");
 57:         setDepots->setLabels(depots,2); // This will also add London to the superset Locations, the others were already present.
 58:         aimms::ISetData* setLocations = session->openSet("Locations");
 59:         setLocations->setLabels(locations,3); // This will empty the set and its subsets, and then adds London and Oslo to only itself.
 60: 
 61:         assert(setDepots->getCardinality() == 0); // Probably not intended.
 62:     }
 63: }
 64: 
 65: 
 66: int main(int argc, const char* argv[]) 
 67: {
 68:     if (argc != 3) {
 69:         std::cerr << "Invalid number of arguments. usage: <location of AIMMS>  <location of project>" << std::endl; 
 70:         return 1;
 71:     }  
 72: 
 73:     aimms::ISession* session = 0;
 74: 
 75:     try {
 76: 
 77:         // the examples
 78:         session = aimms::openSession(argv[1],argv[2]); 
 79: 
 80:         useOrdinals(session);
 81:         initializeSets(session);
 82: 
 83:     } catch (std::exception& e){
 84: 
 85:         std::cerr << e.what();
 86: 
 87:         if (session) {
 88:             session->close(); 
 89:         }
 90:         return 1;
 91:     }
 92: 
 93:     try{
 94:         session->close();
 95:         return 0;
 96:     } catch (std::exception& e){
 97:         std::cerr << e.what();
 98:         return 1;
 99:     }
100: }