>>> from simo.simulation.model.operationarg import OperationArg
>>> import numpy
>>> from minimock import Mock
Container class for passing the operation model arguments to operation models
Attributes:
- name: model name as string
- type: model type as string
- language: model implementation language as string
- target_index: two-dimensional numpy array containing target object indices
- drop_target: boolean vector for indicating whether the object at the same place in target_index should be left out of the actual evaluation of the model
- num_of_targets
- num_of_res_objs
- num_of_res_vars
- _cash_flow
- _errors
- _data
- _variables
- _params
- _param_table_vals
- _results
- _weights
- cash_flow_handler
- cash_flow_model
- cash_flow_table
- cash_flow_table_name
- result_labels
- result_type
- result_level
- sim_effects
- aborted
- failed
Initialize operation model argument container
Parameters
name -- operation instance name
group -- operation instance group
db_name -- operation name for the result database
db_group -- operation group for the result database
type -- operation type
lang -- operation implementation language
sim -- simulator instance
>>> sim = Mock('simulator')
>>> arg = OperationArg('dummymodel', 'dummygroup', 'dummymodel', 'dummygroup',
... 'operation', 'Python', sim)
>>> arg.name
'dummymodel'
>>> arg.db_name
'dummymodel'
>>> arg.type
'operation'
>>> arg.language
'Python'
Set the operation model target index:
>>> tind = numpy.array([[0,0,4,0,0],
... [0,0,5,0,0],
... [0,0,6,0,0],
... [0,0,12,0,0],
... [0,0,15,0,0]], dtype=int)
>>> arg.set_targets(tind, set([2]))
>>> arg.target_index
array([[ 0, 0, 4, 0, 0],
[ 0, 0, 5, 0, 0],
[ 0, 0, 6, 0, 0],
[ 0, 0, 12, 0, 0],
[ 0, 0, 15, 0, 0]])
>>> arg.num_of_targets
5
Set cash flow values either for single target or for all targets:
>>> arg.set_cash_flow(5.0, 0)
>>> arg.set_cash_flow(3.0, 1)
>>> arg.set_cash_flow(10.1, 2)
>>> arg.set_cash_flow(1.0, 4)
Get cash flow container for target object i or for all target objects if i is None:
>>> arg.get_cash_flow(0)
array([ 5.])
>>> arg.get_cash_flow(2)
array([ 10.1])
>>> arg.get_cash_flow()
array([ 5. , 3. , 10.1, 0. , 1. ])
Set operation input variable values. Create container for the variables if not created yet:
>>> vals = numpy.ones(5, dtype=float)
>>> arg.set_vars(3, vals, 2, set([]))
>>> arg._variables
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 1., 1., 1., 1., 1.]])
Get input variable values for target object i:
>>> arg.get_vars(0)
array([ 0., 0., 1.])
Set operation input parameter values. Create container for the parameters if not created yet:
>>> arg.set_params(1.0)
>>> arg.set_params(2.0)
Initialize containers for cash flow tables:
>>> cashflowtable = Mock('CashFlowTable')
>>> cashflowtable.name = 'tablename'
>>> cftables = [cashflowtable, cashflowtable, cashflowtable]
>>> arg.init_cash_flow_tables(cftables)
Add a single cash flow table to operation arguments:
>>> arg.set_cash_flow_table(cashflowtable, 0)
Called simulator._data_db.store_timber_prices(
<Mock ... simulator>,
None,
<Mock ... CashFlowTable>)
>>> arg.set_cash_flow_table(cashflowtable, 2)
Called simulator._data_db.store_timber_prices(
<Mock ... simulator>,
None,
<Mock ... CashFlowTable>)
>>> arg.cash_flow_table_names
['tablename', None, 'tablename']
>>> arg.common_cash_flow_table
[<Mock ... CashFlowTable>, None, <Mock ... CashFlowTable>]
>>> arg.set_cash_flow_table(cashflowtable, 2, 2)
Called simulator._data_db.store_timber_prices(
<Mock ... simulator>,
2,
<Mock ... CashFlowTable>)
>>> for row in arg.cash_flow_tables: print row
[None, None, None]
[None, None, None]
[None, None, <Mock ... CashFlowTable>]
[None, None, None]
[None, None, None]
Set operation result weight value(s):
>>> arg.set_weights(numpy.ones(5, dtype=float), set([]))
Get operation weights for target object i or all targets
>>> arg.get_weights(1)
1.0
>>> arg.get_weights()
array([ 1., 1., 1., 1., 1.])
Create operation result structure with given number of rows and columns and the result labels:
>>> nres = numpy.ones((4, 5), dtype=int)
>>> resvars = [('volume', ('assortment', 'SP'))]
>>> results = numpy.zeros((5, 4), dtype=float)
>>> arg.set_result_structure('existing', 2, 'data', nres, resvars,
... results, set([]))
>>> arg.num_of_results
array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]])
Get result structure for target object i:
>>> arg.get_results(0)
array([ 0., 0., 0., 0.])
>>> arg.get_results()
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
Set parameter table values for a single object and target attribute:
>>> arg.set_param_table_values([1,2,3], 2, 1, 3)
>>> arg._param_table_vals
[[[], [], []], [[], [], []], [[], [[1, 2, 3]], []], [[], [], []], [[], [], []]]
Get parameter table values for object i:
>>> arg.get_param_table_vals(0)
[[], [], []]
>>> arg.get_param_table_vals(2)
[[], [[1, 2, 3]], []]
Store input data structure from single level to operation arguments:
>>> data = numpy.zeros([5,5], dtype=float)
>>> data[:,2] = range(5)
>>> data[:,3] = 1.1
>>> index = numpy.array([[0,1],[1,2],[2,3],[3,4],[4,5]], dtype=int)
>>> arg.set_data(data, index, 1, [3])
>>> for item in arg._data[1]: print item
[[ 0. 0. 0. 1.1 0. ]
[ 0. 0. 1. 1.1 0. ]
[ 0. 0. 2. 1.1 0. ]
[ 0. 0. 3. 1.1 0. ]
[ 0. 0. 4. 1.1 0. ]]
[[0 1]
[1 2]
[2 3]
[3 4]
[4 5]]
[3]
>>> data = numpy.zeros([10,6], dtype=float)
>>> data[:,2] = range(10)
>>> data[:,3] = [0,0,1,1,1,2,2,3,4,4]
>>> data[:,4] = 2.2
>>> index = numpy.array([[0,2],[2,5],[5,7],[7,8],[8,10]], dtype=int)
>>> arg.set_data(data, index, 2, [2])
>>> for item in arg._data[2]: print item
[[ 0. 0. 0. 0. 2.2 0. ]
[ 0. 0. 1. 0. 2.2 0. ]
[ 0. 0. 2. 1. 2.2 0. ]
[ 0. 0. 3. 1. 2.2 0. ]
[ 0. 0. 4. 1. 2.2 0. ]
[ 0. 0. 5. 2. 2.2 0. ]
[ 0. 0. 6. 2. 2.2 0. ]
[ 0. 0. 7. 3. 2.2 0. ]
[ 0. 0. 8. 4. 2.2 0. ]
[ 0. 0. 9. 4. 2.2 0. ]]
[[ 0 2]
[ 2 5]
[ 5 7]
[ 7 8]
[ 8 10]]
[2]
Get data structure for target object i:
>>> data = arg.get_data(0)
>>> data[1]
array([[ 0. , 0. , 0. , 1.1, 0. ]])
>>> data[2]
array([[ 0. , 0. , 0. , 0. , 2.2, 0. ],
[ 0. , 0. , 1. , 0. , 2.2, 0. ]])
>>> data = arg.get_data(1)
>>> data[1]
array([[ 0. , 0. , 1. , 1.1, 0. ]])
>>> data[2]
array([[ 0. , 0. , 2. , 1. , 2.2, 0. ],
[ 0. , 0. , 3. , 1. , 2.2, 0. ],
[ 0. , 0. , 4. , 1. , 2.2, 0. ]])
>>> data = arg.get_data(2)
>>> data[1]
array([[ 0. , 0. , 2. , 1.1, 0. ]])
>>> data[2]
array([[ 0. , 0. , 5. , 2. , 2.2, 0. ],
[ 0. , 0. , 6. , 2. , 2.2, 0. ]])
>>> data = arg.get_data(3)
>>> data[1]
array([[ 0. , 0. , 3. , 1.1, 0. ]])
>>> data[2]
array([[ 0. , 0. , 7. , 3. , 2.2, 0. ]])
>>> data = arg.get_data(4)
>>> data[1]
array([[ 0. , 0. , 4. , 1.1, 0. ]])
>>> data[2]
array([[ 0. , 0. , 8. , 4. , 2.2, 0. ],
[ 0. , 0. , 9. , 4. , 2.2, 0. ]])
Check operation model evaluation result
Check the evaluation result for all result targets. 1 = model successfully evaluated, 0 = warning(s), model evaluated, -1 = error(s), model not evaluated, -2 = model aborted
evalres – evaluation result vector
>>> arg.check_eval(numpy.array([1, 1, 1, 1, 1]))
>>> arg._errors[2]=['err1', 'err2']
>>> arg.check_eval(numpy.array([1, 0, -1, -2, 1]))
Called simulator.add_warning(
'Group dummygroup Model dummymodel evaluated with warning(s)!',
<Mock ... simulator.level>,
array([[0, 0, 5, 0, 0]]))
Called simulator.add_error(
'Group dummygroup Model dummymodel failed to evaluate due to error(s)!',
<Mock ... simulator.level>,
array([[0, 0, 6, 0, 0]]))
Called simulator.add_error(
'err1',
<Mock ... simulator.level>,
array([[0, 0, 6, 0, 0]]))
Called simulator.add_error(
'err2',
<Mock ... simulator.level>,
array([[0, 0, 6, 0, 0]]))
Called simulator.add_warning(
'Group dummygroup Model dummymodel aborted due to user set limits.',
<Mock ... simulator.level>,
array([[ 0, 0, 12, 0, 0]]))