ContentsIndexHome
PreviousUpNext
Views.java

The views example in Java.

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