Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

• Composition #1250

Merged
merged 4 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .idea/runConfigurations/Scratch_Pad.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 24 additions & 6 deletions psyneulink/core/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@
* **reinitialize_when** - the `reinitialize_when <Component.reinitialize_when>` attribute contains a `Condition`. When
this condition is satisfied, the Component calls its `reinitialize <Component.reinitialize>` method. The
`reinitialize <Component.reinitialize>` method is executed without arguments, meaning that the relevant function's
`initializer<IntegratorFunction.initializer>` attribute (or equivalent -- initialization attributes vary among functions) is
used for reinitialization. Keep in mind that the `reinitialize <Component.reinitialize>` method and `reinitialize_when
<Component.reinitialize_when>` attribute only exist on stateful Mechanisms.
`initializer<IntegratorFunction.initializer>` attribute (or equivalent -- initialization attributes vary among
functions) is used for reinitialization. Keep in mind that the `reinitialize <Component.reinitialize>` method and
`reinitialize_when <Component.reinitialize_when>` attribute only exist for Mechanisms that have `stateful
<Parameter.stateful>` Parameters, or that have a `function <Mechanism.function>` with `stateful <Parameter.stateful>`
Parameters.

.. note::

Expand Down Expand Up @@ -416,7 +418,11 @@ class `UserList <https://docs.python.org/3.6/library/collections.html?highlight=

from psyneulink.core import llvm as pnlvm
from psyneulink.core.globals.context import Context, ContextFlags, _get_time
from psyneulink.core.globals.keywords import COMPONENT_INIT, CONTEXT, CONTROL_PROJECTION, DEFERRED_INITIALIZATION, FUNCTION, FUNCTION_CHECK_ARGS, FUNCTION_PARAMS, INITIALIZING, INIT_FULL_EXECUTE_METHOD, INPUT_STATES, LEARNING, LEARNING_PROJECTION, LOG_ENTRIES, MATRIX, MODULATORY_SPEC_KEYWORDS, NAME, OUTPUT_STATES, PARAMS, PARAMS_CURRENT, PREFS_ARG, SIZE, USER_PARAMS, VALUE, VARIABLE, kwComponentCategory
from psyneulink.core.globals.keywords import \
COMPONENT_INIT, CONTEXT, CONTROL_PROJECTION, DEFERRED_INITIALIZATION, \
FUNCTION, FUNCTION_CHECK_ARGS, FUNCTION_PARAMS, INITIALIZING, INIT_FULL_EXECUTE_METHOD, INPUT_STATES, \
LEARNING, LEARNING_PROJECTION, LOG_ENTRIES, MATRIX, MODULATORY_SPEC_KEYWORDS, NAME, OUTPUT_STATES, \
PARAMS, PARAMS_CURRENT, PREFS_ARG, REINITIALIZE_WHEN, SIZE, USER_PARAMS, VALUE, VARIABLE, kwComponentCategory
from psyneulink.core.globals.log import LogCondition
from psyneulink.core.globals.parameters import Defaults, Parameter, ParameterAlias, ParameterError, ParametersBase
from psyneulink.core.globals.preferences.componentpreferenceset import ComponentPreferenceSet, kpVerbosePref
Expand Down Expand Up @@ -850,6 +856,8 @@ class Component(object, metaclass=ComponentsMeta):
componentCategory = None
componentType = None

standard_constructor_args = [REINITIALIZE_WHEN]

class Parameters(ParametersBase):
"""
The `Parameters` that are associated with all `Components`
Expand Down Expand Up @@ -935,7 +943,8 @@ def __init__(self,
size=NotImplemented, # 7/5/17 CW: this is a hack to check whether the user has passed in a size arg
function=None,
name=None,
prefs=None):
prefs=None,
**kwargs):
"""Assign default preferences; enforce required params; validate and instantiate params and execute method

Initialization arguments:
Expand All @@ -946,6 +955,11 @@ def __init__(self,

"""

for arg in kwargs.keys():
if arg not in self.standard_constructor_args:
raise ComponentError(f"Unrecognized argument in constructor for {self.name} "
f"(type: {self.__class__.__name__}): {repr(arg)}")

self.parameters = self.Parameters(owner=self, parent=self.class_parameters)

context = ContextFlags.COMPONENT
Expand Down Expand Up @@ -1005,7 +1019,11 @@ def __init__(self,
self.paramInstanceDefaults = {}

self.has_initializers = False
self.reinitialize_when = Never()

if kwargs and REINITIALIZE_WHEN in kwargs:
self.reinitialize_when = kwargs[REINITIALIZE_WHEN]
else:
self.reinitialize_when = Never()

self._role = None

Expand Down
4 changes: 3 additions & 1 deletion psyneulink/core/components/mechanisms/mechanism.py
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,7 @@ def __init__(self,
name=None,
prefs=None,
context=None,
**kwargs
):
"""Assign name, category-level preferences, and variable; register Mechanism; and enforce category methods

Expand Down Expand Up @@ -1487,7 +1488,8 @@ def __init__(self,
function=function,
param_defaults=params,
prefs=prefs,
name=name)
name=name,
**kwargs)

# FIX: 10/3/17 - IS THIS CORRECT? SHOULD IT BE INITIALIZED??
self._status = INITIALIZING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@ def __init__(self,
default_variable=None,
size=None,
input_states=None,
function=None,
output_states=None,
params=None,
name=None,
prefs=None,
context=None,
function=None,
**kwargs
):
"""Abstract class for processing mechanisms

Expand All @@ -162,12 +163,13 @@ def __init__(self,
super().__init__(default_variable=default_variable,
size=size,
input_states=input_states,
function=function,
output_states=output_states,
params=params,
name=name,
prefs=prefs,
context=context,
function=function,
**kwargs
)

def _validate_inputs(self, inputs=None):
Expand Down
6 changes: 4 additions & 2 deletions psyneulink/core/components/shellclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ def __init__(self,
function=None,
param_defaults=None,
name=None,
prefs=None):
prefs=None,
**kwargs):
super().__init__(default_variable=default_variable,
size=size,
function=function,
param_defaults=param_defaults,
name=name,
prefs=prefs)
prefs=prefs,
**kwargs)

def _validate_params(self, request_set, target_set=None, context=None):
raise ShellClassError("Must implement _validate_params in {0}".format(self))
Expand Down
6 changes: 6 additions & 0 deletions psyneulink/core/compositions/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4888,6 +4888,12 @@ def run(
else:
execution_autodiff_stimuli[node] = autodiff_stimuli[node]

for node in self.nodes:
if hasattr(node, "reinitialize_when") and node.parameters.has_initializers._get(execution_id):
if node.reinitialize_when.is_satisfied(scheduler=self.scheduler_processing,
execution_context=execution_id):
node.reinitialize(None, execution_context=execution_id)

# execute processing
# pass along the stimuli for this trial
trial_output = self.execute(inputs=execution_stimuli,
Expand Down
2 changes: 1 addition & 1 deletion psyneulink/core/globals/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ def run(obj,
if call_before_time_step:
call_with_pruned_args(call_before_time_step, execution_context=execution_id)

# Reset any mechanisms whose 'reinitialize_when' conditions are satisfied
# Reinitialize any mechanisms that has a 'reinitialize_when' condition and it is satisfied
for mechanism in obj.mechanisms:
if hasattr(mechanism, "reinitialize_when") and mechanism.parameters.has_initializers._get(execution_id):
if mechanism.reinitialize_when.is_satisfied(scheduler=obj.scheduler_processing, execution_context=execution_id):
Expand Down
5 changes: 3 additions & 2 deletions psyneulink/core/globals/keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@
'PREDICTION_MECHANISM_TYPE', 'PREFS_ARG', 'PREVIOUS_VALUE', 'PRIMARY', 'PROB', 'PROB_INDICATOR',
'PROCESS', 'PROCESSING', 'PROCESS_INIT', 'PROCESSES', 'PROCESSES_DIM', 'PROCESSING_MECHANISM', 'PRODUCT',
'PROJECTION', 'PROJECTION_DIRECTION', 'PROJECTION_PARAMS', 'PROJECTION_SENDER', 'PROJECTION_TYPE', 'PROJECTIONS',
'QUOTIENT', 'RANDOM', 'RANDOM_CONNECTIVITY_MATRIX', 'RATE', 'RATIO', 'REARRANGE_FUNCTION',
'RECEIVER', 'RECURRENT_TRANSFER_MECHANISM', 'REDUCE_FUNCTION', 'REFERENCE_VALUE', 'REINITIALIZE',
'QUOTIENT', 'RANDOM', 'RANDOM_CONNECTIVITY_MATRIX', 'RATE', 'RATIO', 'REARRANGE_FUNCTION', 'RECEIVER',
'RECURRENT_TRANSFER_MECHANISM', 'REDUCE_FUNCTION', 'REFERENCE_VALUE', 'REINITIALIZE', 'REINITIALIZE_WHEN',
'RELU_FUNCTION', 'REST', 'RESULT', 'RESULTS', 'ROLES', 'RL_FUNCTION', 'RUN',
'SAMPLE', 'SAVE_ALL_VALUES_AND_POLICIES', 'SCALAR', 'SCALE', 'SCHEDULER', 'SELF', 'SENDER',
'SEPARATOR_BAR', 'SIMPLE', 'SIMPLE_INTEGRATOR_FUNCTION', 'SINGLETON', 'SIZE', 'SLOPE', 'SOFT_CLAMP',
Expand Down Expand Up @@ -914,6 +914,7 @@ def _is_metric(metric):
OUTPUT_TYPE = 'output'
OVERWRITE = 'overwrite'
REINITIALIZE = "reinitialize"
REINITIALIZE_WHEN = "reinitialize_when"

LOW = 'low'
HIGH = 'high'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,8 @@ def __init__(self,
output_states:tc.optional(tc.any(str, Iterable))=(DECISION_VARIABLE, RESPONSE_TIME),
params=None,
name=None,
prefs: is_pref_set = None):
prefs: is_pref_set = None,
**kwargs):

self.standard_output_states = StandardOutputStates(self,
DDM_standard_output_states,
Expand Down Expand Up @@ -923,7 +924,8 @@ def __init__(self,
name=name,
prefs=prefs,
size=size,
context=ContextFlags.CONSTRUCTOR)
context=ContextFlags.CONSTRUCTOR,
**kwargs),
self._instantiate_plotting_functions()
# # TEST PRINT
# print("\n{} user_params:".format(self.name))
Expand Down