Table Of Contents

Previous topic

aggregationarg.py

Next topic

predictionarg.py

This Page

operationarg.py

>>> from simo.simulation.model.operationarg import OperationArg
>>> import numpy
>>> from minimock import Mock

class OperationArg(object):

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

def __init__(self, name, group, db_name, db_group, type, lang, sim):

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'

def set_targets(self, tind, torem):

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

def set_cash_flow(self, cashflow, i):

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)

def get_cash_flow(self, i=None):

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. ])

def get_errors(self, i):

Get error container for target object i:

>>> arg.get_errors(0)
[]

def set_vars(self, nvar, vals, loc, torem):

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.]])

def get_vars(self, i):

Get input variable values for target object i:

>>> arg.get_vars(0)
array([ 0.,  0.,  1.])

def set_params(self, param):

Set operation input parameter values. Create container for the parameters if not created yet:

>>> arg.set_params(1.0)
>>> arg.set_params(2.0)

def get_params(self):

Get input parameter values:

>>> arg.get_params()
deque([1.0, 2.0])

def init_cash_flow_tables(self, cash_flow_table):

Initialize containers for cash flow tables:

>>> cashflowtable = Mock('CashFlowTable')
>>> cashflowtable.name = 'tablename'
>>> cftables = [cashflowtable, cashflowtable, cashflowtable]
>>> arg.init_cash_flow_tables(cftables)

def set_cash_flow_table(self, table, target=None):

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]

def set_weights(self, values, torem):

Set operation result weight value(s):

>>> arg.set_weights(numpy.ones(5, dtype=float), set([]))

def get_weights(self, i=None):

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.])
def set_result_structure(self, restarget, reslevel, restype, nres, resvars,
results, torem):

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]])

def get_results(self, i=None):

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.]])

def set_param_table_values(self, values, obj, table, ntables):

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]], []], [[], [], []], [[], [], []]]

def get_param_table_vals(self, i):

Get parameter table values for object i:

>>> arg.get_param_table_vals(0)
[[], [], []]
>>> arg.get_param_table_vals(2)
[[], [[1, 2, 3]], []]

def set_data(self, data, index, level, attrs):

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]

def get_data(self, i):

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. ]])

def check_eval(self, evalres):

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]]))