ContentsIndexHome
PreviousUpNext
Basic.cs

Implementations of assigning data, running a procedure without arguments and reading data.

 1: using System;
 2: using System.Text;
 3: using System.IO;
 4: using Aimms;
 5: 
 6: namespace example
 7: {
 8:    
 9:     /// <summary>
10:     ///  This basic example demonstrates the code to 
11:     ///  * Set up the connection to a model, 
12:     ///  * Assign parameter data, 
13:     ///  * Solve the model,
14:     ///  * Retrieve variables
15:     /// </summary>
16:     class Basic
17:     {
18:         static public void setParameters(ISession session)
19:         {
20:             // Use the insert method to add labels to the domains sets while adding data.
21: 
22:             // Set supply.
23:             IMultiDimData parameterSupply = session.openMultiDim("Supply");
24:             parameterSupply.insert(new Tuple("London"), 20.0);
25:             parameterSupply.insert(new Tuple("Paris"), 30.0);
26: 
27:             // Close data if it is not longer needed.
28:             parameterSupply.close();
29: 
30:             // Set demand.
31:             IMultiDimData parameterDemand = session.openMultiDim("Demand");
32:             parameterDemand.insert(new Tuple("Oslo"), 10.0);
33:             parameterDemand.insert(new Tuple("Madrid"), 15.0);
34:             parameterDemand.insert(new Tuple("Berlin"), 20.0);
35:             parameterDemand.close();
36: 
37:             // Use the setValue method to add data without expanding the domain sets.
38:             // An AimmsRuntimeException will be thrown if a label does not exist in the index domain.
39: 
40:             // Set transportcosts.
41:             IMultiDimData parameterUnitTransportCost = session.openMultiDim("UnitTransportCost");
42:             try {
43:                 parameterUnitTransportCost.setValue(new Tuple("London", "Oslo"), 7.0);
44:                 parameterUnitTransportCost.setValue(new Tuple("London", "Madrid"), 12.0);
45:                 parameterUnitTransportCost.setValue(new Tuple("London", "Berlin"), 18.0);
46:                 parameterUnitTransportCost.setValue(new Tuple("Paris", "Oslo"), 14.0);
47:                 parameterUnitTransportCost.setValue(new Tuple("Paris", "Madrid"), 23.0);
48:                 parameterUnitTransportCost.setValue(new Tuple("Paris", "Berlin"), 17.0);
49:             } catch(AimmsRuntimeException ex){ 
50:                 Console.WriteLine(String.Format("Catched a runtime exception: {0}", ex.Message)); 
51:             }
52:             parameterUnitTransportCost.close();
53:         }
54:         static public void runMainExecution(ISession session)
55:         {
56:             // Retrieve the procedure.
57:             IProcedure procedure = session.openProcedure("MainExecution");
58: 
59:             // Run the procedure.
60:             procedure.run();
61: 
62:             // Close it afterwards. 
63:             procedure.close();
64:         }
65: 
66:         static public void retrieveVariables(ISession session, TextWriter os)
67:         {
68:             // Retrieve and display the scalar variable TotalCost.
69:             // Scalars have their own interface, and are retrieved with the openScalar method.
70:             IScalarData variableTotalCost = session.openScalar("TotalCost");
71: 
72:             double totalcost = variableTotalCost.Double;
73: 
74:             os.WriteLine(variableTotalCost.Name + ": " + totalcost);
75:             os.WriteLine();
76:             variableTotalCost.close();
77: 
78:             // Retrieve and display the two dimensional variable Transport.
79:             IMultiDimData variableTransport = session.openMultiDim("Transport");
80: 
81:             // To access multidimensional data, use an iterator.
82:             IIterator it = variableTransport.createIterator();
83: 
84:             os.WriteLine(variableTransport.Name + ":");
85:             while (it.next())
86:             {  // iterate through all by calling next successively
87: 
88:                 // The Tuple property gives access to the current position. 
89:                 // The Double property returns the value at the current position.
90:                 os.WriteLine(it.Tuple + " : " + it.Double);
91:             }
92: 
93:             os.WriteLine();
94:             it.close();
95:             variableTransport.close();
96:         }
97:     }
98: }