ContentsIndexHome
PreviousUpNext
C++ Views example

The views example in C++.

  1: #include <iostream>
  2: #include <time.h>
  3: #include <assert.h>
  4: #include "aimms/Include.h"
  5: 
  6: // This example demonstrates views on data, initializing set data in a single call and dense data input.  
  7: // The combination of the first and last one can be powerful, since the dense input functionality modifies the entire domain
  8: // and the views allow for flexible use of domain restrictions. 
  9: 
 10: void createViews(aimms::ISession* session)
 11: {
 12:     // It is possible to create a view on data by placing restrictions on the domains.
 13:     // This is a three step proces:
 14:     aimms::IMultiDimData* unitTransportCost = session->openMultiDim("unitTransportCost");
 15: 
 16:     // 1: Create an IFilter
 17:     aimms::IFilter* filter = unitTransportCost->createFilter(); 
 18: 
 19:     // 2: Add domain restrictions to the filter
 20:     filter->restrict(0,"Oslo");   // Restricts the 0-th dimension to the element value "Oslo". 
 21: 
 22:     // 3: Create an IDataView 
 23:     aimms::IDataView* utcFromOslo = unitTransportCost->openView(filter);
 24: 
 25:     // This view has a lower dimension than the unitTransportCost, since the data is fixed on the first level. 
 26:     int dim = utcFromOslo->getDimension();
 27:     assert(dim == 1);
 28: 
 29:     utcFromOslo->close();
 30: 
 31:     // It is possible to add restrictions on other levels or to change earlier restrictions.  
 32:     filter->restrict(1,"Berlin");   
 33:     filter->restrict(0,"London");  
 34:     aimms::IDataView* utcFromLondonToBerlin = unitTransportCost->openView(filter);
 35: 
 36:     // If the view has become scalar (all levels are fixed), 
 37:     // the IDataView can be cast to an IScalarDataView for some additional user friendly methods.
 38:     aimms::IScalarDataView* scalarview = dynamic_cast<aimms::IScalarDataView*>(utcFromLondonToBerlin);
 39:     double utcFromLondonToBerlin_Value = scalarview->asDouble();
 40: 
 41:     utcFromLondonToBerlin->close();
 42: 
 43:     filter->close();
 44:     unitTransportCost->close();
 45: }
 46: 
 47: 
 48: 
 49: void setLabels(aimms::ISession* session)
 50: {
 51:     // Assuming set data is stored in arrays, it can be easily sent to the Aimms model by using setLabels.
 52:     const char* depots[] = {"London","Oslo"};
 53:     const char* customers[] = {"Berlin","Prague","Oslo", "Rome"};
 54: 
 55:     aimms::ISetData* set;
 56:     set = session->openSet("Depots");
 57:     set->setLabels(depots,2);
 58:     set->close();
 59:     set = session->openSet("Customers");
 60:     set->setLabels(customers,4);
 61:     set->close();
 62: 
 63:     // It is also possible to increase the set using appendLabels.
 64:     const char* extradepots[] = {"London","Madrid"};
 65:     set = session->openSet("Depots");
 66:     set->appendLabels(extradepots,2);
 67:     // The set "Depots" is now {'London','Oslo','Madrid'}.
 68: }
 69: 
 70: void setValues(aimms::ISession* session)
 71: {
 72:     // Multidimensional data can be sent to the Aimms Model using setValues.
 73:     // The length of the array must be supplied; and must be the product of the cardinality of the domains.
 74:     const double supplyValues[] = {3.0, 5.0, 4.0};
 75:     const double demandValues[] = {2.7, 3.2, 3.0, 2.9};
 76: 
 77:     aimms::IMultiDimData* supply;
 78:     supply = session->openMultiDim("Supply");
 79:     supply->setValues(supplyValues,3);
 80:     supply->close();
 81: 
 82:     aimms::IMultiDimData* demand = session->openMultiDim("Demand");
 83:     demand->setValues(demandValues,4);
 84:     demand->close();
 85: 
 86:     // note: Higher dimensional data still uses a 1 dimensional array.
 87:     const double unitTransportValues[] = 
 88:     {23.7, 45.0, 23.2, 45.3, 
 89:     36.7, 38.8, 21.4, 12.6, 
 90:     66.2, 22.0, 37.1, 13.8};
 91: 
 92:     aimms::IMultiDimData* unitTransportCost = session->openMultiDim("unitTransportCost");
 93:     unitTransportCost->setValues(unitTransportValues,4*3);
 94:     unitTransportCost->close();
 95: }
 96: 
 97: 
 98: 
 99: void assignToViews(aimms::ISession* session)
100: {
101:     // Assigning data to a view has two (potential) advantages:
102:     //  - reduction of the dimension 
103:     //  - sparse use of the dense function setValues
104: 
105:     // create a view
106:     aimms::IMultiDimData* unitTransportCost = session->openMultiDim("unitTransportCost");
107:     aimms::IFilter* filter = unitTransportCost->createFilter(); 
108:     filter->restrict(0,"Oslo");  
109:     aimms::IDataView* utcFromOslo = unitTransportCost->openView(filter);
110:     // notice that utcFromOslo is only 1-dimensional
111: 
112:     // use setValue
113:     utcFromOslo->setValue(aimms::Tuple("Berlin"), 34.7);
114: 
115:     // use setValues
116:     double utcfromhilversum[] = {32.8,24.3,46.2,12.0};
117:     utcFromOslo->setValues(utcfromhilversum, 4);
118: 
119:     filter->close();
120:     unitTransportCost->close(); // Closing the IMultiDimData closes all the views as well.
121: 
122: }
123: 
124: void readFiltered(aimms::ISession* session)
125: {
126:     // The IDataView class allows the use of iterators, but the filtered iterator can also be created directly on the IMultiDimData.
127: 
128:     aimms::IMultiDimData* unitTransportCost = session->openMultiDim("unitTransportCost");
129: 
130:     // Construct a filter for the costs from Oslo.
131:     aimms::IFilter* filter = unitTransportCost->createFilter(); 
132:     filter->restrict(0,"Oslo");  
133: 
134:     // create a view...
135:     aimms::IDataView* utcFromOslo = unitTransportCost->openView(filter);
136:     // ... and an iterator on it
137:     aimms::IIterator* it1 = utcFromOslo->createIterator();
138:     //... or create an iterator with the filter
139:     aimms::IIterator* it2 = unitTransportCost->createIterator(filter);
140: 
141:     it1->close();
142:     it2->close();
143:     utcFromOslo->close();
144:     unitTransportCost->close();
145: }
146: 
147: int main(int argc, const char* argv[]) 
148: {
149:     if (argc != 3) {
150:         std::cerr << "Invalid number of arguments. usage: <location of AIMMS>  <location of project>" << std::endl; 
151:         return 1;
152:     }  
153: 
154:     aimms::ISession* session = 0;
155:     try {
156:         // the examples
157:         session = aimms::openSession(argv[1],argv[2]); 
158: 
159:         setLabels(session);
160:         setValues(session);
161:         createViews(session);
162:         assignToViews(session);
163:         readFiltered(session);
164: 
165:     } catch (std::exception& e){
166: 
167:         std::cerr << e.what();
168: 
169:         if (session) {
170:             session->close(); 
171:         }
172:         return 1;
173:     }
174: 
175:     try{
176:         session->close();
177:         return 0;
178:     }catch (std::exception& e){
179:         std::cerr << e.what();
180:         return 1;
181:     }
182: }
183: 
184: 
185: 
186: 
187: