.iterator(+)
Memory Output Formatter method
Description
The .iterator(+) method provides you with an iterator instance allowing you to loop through and collect the results of a PLANit run.
When collecting the iterator, you must always provide:
- Mode (XML) id
- Time period (XML) id
- Iteration index
- OutputType.<enum> type
By providing these inputs, the memory outformatter can identify the result matrix on which the iterator operates.
Make sure that the mode, time period, iteration and output type exist and/or are activated in the results, otherwise the iterator cannot be instantiated.
Signature
.iterator(mode_xml_id, time_period_xml_id, iteration, output_type)
with
Parameter | Type | Unit | Compulsory | Description |
---|---|---|---|---|
mode_xml_id |
string |
None |
YES | Valid mode id related to the input, i.e., XML id |
time_period_xml_id |
string |
None |
YES | Valid time period id related to the input, i.e., XML id |
iteration |
integer |
None |
YES | Valid iteration index |
output_type |
OutputType.<enum> |
None |
YES | Output type you want to construct the iterator for |
Return type
Class Memory OutputFormatter Iterator
instance
Example 1
from planit import *
#prep
plan_it = Planit()
plan_it.project.set(TrafficAssignment.TRADITIONAL_STATIC)
plan_it.project.assignment.set(PhysicalCost.BPR)
plan_it.project.assignment.set(VirtualCost.FIXED)
plan_it.project.assignment.set(Smoothing.MSA)
#remove default formatter - activate memory output
plan_it.project.deactivate(OutputFormatter.PLANIT_IO)
plan_it.project.activate(OutputFormatter.MEMORY)
#run simulation
plan_it.project.run()
#collect iteration index of last recorded iteration
i = plan_it.project.memory.get_last_iteration()
#find position of flow result
flow_pos = plan_it.project.memory.get_position_of_output_value_property(OutputType.LINK, OutputProperty.FLOW)
#find position of key; we use link segment (XML) id
id_pos = plan_it.project.memory.get_position_of_output_key_property(OutputType.LINK, OutputProperty.LINK_SEGMENT_XML_ID)
# collect iterator for mode "1", time period "2", iteration i, and print results by looping over entries
modeXmlId = "1"
timePeriodXmlId = "2"
the_iterator = plan_it.project.memory.iterator(modeXmlId, timePeriodXmlId, i, OutputType.LINK)
while the_iterator.has_next():
the_iterator.next()
print "id: ", the_iterator.get_keys()[id_pos], " flow: ", the_iterator.get_values()[flow_pos]
See also
Memory OutputFormatter Iterator
for the iterator implementation
OutputType.<enum>
for available output types
Source code
Class MemoryOutputFormatterWrapper
in projectwrappers.py