ContentsIndexHome
PreviousUpNext
Views.cs

The views example in C#.

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