ContentsIndexHome
PreviousUpNext
Basic.java

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

 1: 
 2: package com.aimms.aimmssdk.examples.basic;
 3: 
 4: 
 5: import com.aimms.aimmssdk.*;
 6: import org.slf4j.Logger;
 7: import org.slf4j.LoggerFactory;
 8: 
 9: 
10: /**
11: This basic example demonstrates the code to 
12: - Set up the connection to a model, 
13: - Assign parameter data, 
14: - Solve the model,
15: - Retrieve variables
16: */
17: class Basic {
18:     static private Logger logger = LoggerFactory.getLogger(Basic.class);
19: 
20:     static void setParameters(ISession session) {
21:         // Use the insert method to add labels to the domain sets while adding data.
22: 
23:         // Set supply.
24:         IMultiDimData parameterSupply = session.openMultiDim("Supply");
25:         parameterSupply.insert(new Tuple("London"),20.0);
26:         parameterSupply.insert(new Tuple("Paris"),30.0);
27: 
28:         // Close data if it is no longer needed.
29:         parameterSupply.close();
30: 
31:         // Set demand.
32:         IMultiDimData parameterDemand = session.openMultiDim("Demand");
33:         parameterDemand.insert(new Tuple("Oslo"),10.0);
34:         parameterDemand.insert(new Tuple("Madrid"),15.0);
35:         parameterDemand.insert(new Tuple("Berlin"),20.0);
36:         parameterDemand.close();
37: 
38:         // Use the setValue method to add data without expanding the domain sets.
39:         // An error will be generated if a label does not exist in the domain.
40: 
41:         // Set transportcosts.
42:         IMultiDimData parameterUnitTransportCost = session.openMultiDim("UnitTransportCost");
43:         try{
44:             parameterUnitTransportCost.setValue(new Tuple("London","Oslo"),7.0);
45:             parameterUnitTransportCost.setValue(new Tuple("London","Madrid"),12.0);
46:             parameterUnitTransportCost.setValue(new Tuple("London","Berlin"),18.0);
47:             parameterUnitTransportCost.setValue(new Tuple("Paris","Oslo"),14.0);
48:             parameterUnitTransportCost.setValue(new Tuple("Paris","Madrid"),23.0);
49:             parameterUnitTransportCost.setValue(new Tuple("Paris","Berlin"),17.0);
50:         }catch(AimmsRuntimeException ex){
51:             logger.error("Catched a runtime exception: {}", ex.getMessage());
52:         }
53:         parameterUnitTransportCost.close();
54:     }
55: 
56:     static void runMainExecution(ISession session) {
57:         // Retrieve the procedure.
58:         IProcedure procedure = session.openProcedure("MainExecution");
59: 
60:         // Run the procedure.
61:         procedure.run();
62: 
63:         // Close it afterwards. 
64:         procedure.close();
65:     }
66: 
67:     static void retrieveVariables(ISession session) {
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.asDouble();
73: 
74:         logger.info("{} : {}", variableTotalCost.getName(), totalcost );
75: 
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:         logger.info("{}:", variableTransport.getName());
85:         while (it.next()) {  // iterate through all by calling next successively
86: 
87:             // The tuple method gives access to the current position. 
88:             // The asDouble method returns the value at the current position.
89:             logger.info("  {} : {} ", it.tuple(), it.asDouble() );
90:         }
91: 
92:         variableTransport.close(); // Closing  an IMultiDimData closes all its open iterators as well.
93:     }
94: }