Skip to content

Pass switchEval#25

Merged
MichaelStrik merged 20 commits intopublicfrom
pass-condition_value
Feb 24, 2025
Merged

Pass switchEval#25
MichaelStrik merged 20 commits intopublicfrom
pass-condition_value

Conversation

@MichaelStrik
Copy link
Copy Markdown
Collaborator

@MichaelStrik MichaelStrik commented Dec 16, 2024

In preprocessed right-hand-sides, the ctrlif receives a numerical value instead of a boolean value now.
In this way, the state of (implicit) switching functions can be observed in debugging.
A RHS could look like this now:

function dx = rhs_preprocessed_if(datahandle,t,x,p)
switchEval_if_1 = 10 - x;
condition_value = ctrlif( switchEval_if_1, false, true, 1, 0, datahandle );
if condition_value
dx = 5;
else
dx = 1;
end
end

The value of the switching function associated to this ctrlif can now easily be monitored by a debugger by monitoring switchEval_if_1.

The branch is tested with speedtracker, runtime is about 10% longer compared to the snapshot 'speedtracker-latest'.

The branch also includes refactoring of functions that are part of the preprocessing.

Closes #19

Copy link
Copy Markdown
Collaborator

@Schlevidon Schlevidon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work overall: the feature works and the code is much cleaner than before.
No critical bugs, but I would like to suggest some minor improvements. All of my review comments are adressed in this PR which you can just merge if you agree with my changes: #26

Comment thread mtree_checkForComparisonOperator.m Outdated
Comment on lines +1 to +3
function [kind, rIndex] = mtree_checkForComparisonOperator(mtreeobj, index)
% Searches the subtree of ´mtreeobj´ defined by ´index´ for nodes that are
% represent the following comparison operators: "<", ">", "<=" or ">=".
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment and function name don't match the actual behavior of the function: The function only returns the type of the first comparison operator node found among the children of the index node.
The implementation of the function in general is not very clear.

Comment thread mtree_addSwitchEvaluation.m Outdated
Comment on lines +93 to +96
otherwise
% operator is none of the allowed types
error(config.errors.infeasible_if_condition, ...
'"If" statement does not include on of the comparisons <, >, <=, >=.');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function is missing call to config = makeConfig()

Comment thread mtree_createAndAdd_NewNode.m Outdated
if nargin >= 5
to = varargin{4};
to_type = varargin{5};
if ~isempty(varargin) > 0
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The > 0 comparison is redundant. ~isempty(varargin) is sufficient.

Comment thread mtree_addSwitchEvaluation.m Outdated
Comment on lines +28 to +96
switch comparison_operator

% '<': a<b <=> not a-b>=0
case mtreeobj.K.LT
% a<b -> a-b (replace operator by minus)
indexRightChild = mtreeobj.T( comparison_operator_index, cIndex.indexRightchild);
stringTableIndexRightChild = mtreeobj.T(indexRightChild, cIndex.stringTableIndex);

if (stringTableIndexRightChild ~= 0)
% Right child has a string. Include parantheses.
mtreeobj = mtree_ctrlifNormFormTrafo_substituteComparison(mtreeobj, comparison_operator_index, 0);
else
mtreeobj = mtree_ctrlifNormFormTrafo_substituteComparison(mtreeobj, comparison_operator_index, 1);
end
% That's it for now.
% preprocess_setUpCtrlif(...) takes care of the negation operator.


% '>=': a>=b <=> a-b>=0
case mtreeobj.K.GE
% a>=b -> a-b
indexRightChild = mtreeobj.T( comparison_operator_index, cIndex.indexRightchild);
stringTableIndexRightChild = mtreeobj.T(indexRightChild, cIndex.stringTableIndex);
if ( stringTableIndexRightChild ~= 0)
% Right child has a string. Include parantheses.
mtreeobj = mtree_ctrlifNormFormTrafo_substituteComparison(mtreeobj, comparison_operator_index, 0);
else
mtreeobj = mtree_ctrlifNormFormTrafo_substituteComparison(mtreeobj, comparison_operator_index, 1);
end


% '<=': a<=b <=> b-a>=0
case mtreeobj.K.LE
% a<=b -> b<=a
mtreeobj = mtree_switchLeftRightChildren(mtreeobj, comparison_operator_index);

% b<=a -> b-a
indexRightChild = mtreeobj.T( comparison_operator_index, cIndex.indexRightchild);
stringTableIndexRightChild = mtreeobj.T(indexRightChild, cIndex.stringTableIndex);
if (stringTableIndexRightChild ~= 0)
% Right child has a string. Include parantheses.
mtreeobj = mtree_ctrlifNormFormTrafo_substituteComparison(mtreeobj, comparison_operator_index, 0);
else
mtreeobj = mtree_ctrlifNormFormTrafo_substituteComparison(mtreeobj, comparison_operator_index, 1);
end


% '>': a>b <=> not b-a>=0
case mtreeobj.K.GT
% a>=b -> b>=a
mtreeobj = mtree_switchLeftRightChildren(mtreeobj, comparison_operator_index);

% b>=a -> b-a
indexRightChild = mtreeobj.T( comparison_operator_index, cIndex.indexRightchild);
stringTableIndexRightChild = mtreeobj.T(indexRightChild, cIndex.stringTableIndex);

if (stringTableIndexRightChild ~= 0)
% Right child has a string. Include parantheses.
mtreeobj = mtree_ctrlifNormFormTrafo_substituteComparison(mtreeobj, comparison_operator_index, 0);
else
mtreeobj = mtree_ctrlifNormFormTrafo_substituteComparison(mtreeobj, comparison_operator_index, 1);
end
% Regarding the negation operator, see above.


otherwise
% operator is none of the allowed types
error(config.errors.infeasible_if_condition, ...
'"If" statement does not include on of the comparisons <, >, <=, >=.');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic is overly complicated/redundant and can be simplified.

Comment on lines +19 to +24
% if paren==1, add parantheses
if (nargin == 2)
prths = 0;
else
prths = paren;
end
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awkward interface. Should either use varargin or preferably just a fixed number of arguments with a boolean flag to determine whether parantheses should be added or not.
In any case, we don't actually need this functionality, since adding additional parantheses is always safe, so I would just remove this entirely for the sake of simplicity.

Comment thread mtree_replaceMinByCtrlif.m Outdated
@@ -1,26 +1,34 @@
function [mtreeobj, ctrlif_index] = mtree_replaceMinByCtrlif(mtreeobj, ctrlif_index, ignores)
% obj = replaceminByctrlif(mtreeobj)
% [mtreeobj, ctrlif_index] = mtree_replaceMinByCtrlif(mtreeobj, ctrlif_index)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment doesn't match function signature.

Comment thread mtree_replaceMinByCtrlif.m Outdated

% Author:
% ´mtreeobj´: Edited mtree.
% All min, min are replaced by ctrlif function calls.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment

Comment thread preprocess_setUpCtrlif.m Outdated
% 'operator_type' The type of operator that the switch contains
% in its original if-statement
% (or equivalent formulation as if-statement).
% TODO: Make optional?
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its fine to leave it as is. Remove this TODO.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general: I think the mtree_replace[Abs/If/Max/...]ByCtrlif functions have a lot of similarities, so it should be possible to simplify these a bit. However, this would most likely require some more substantial changes, so I would consider this to be out of scope for this PR. Still something to keep in mind for the future.

Comment thread mtree_findBeginOfLine.m Outdated
function output = mtree_findBeginOfLine(mtreeobj, start_node, node_index_to_find, varargin)
% Function to either find a expr node are any other beginning of an line
function output = mtree_findBeginOfLine(mtreeobj, start_node, node_index_to_find)
% Function to either find a expr node or any other beginning of an line
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment

@MichaelStrik MichaelStrik reopened this Feb 24, 2025
@MichaelStrik MichaelStrik merged commit 4589e94 into public Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pass actual condition value to ctrlif instead of a logical true/false

2 participants