Skip to content

Commit 7de62d6

Browse files
committed
Fixes made to both MATLAB and Python div and log implementations
* The tests are still giving bad results when using the Python evaluation of individuals. Look into the code and check that the problem related with the failure of tests is the well-known precision bug #27
1 parent 91e231b commit 7de62d6

File tree

8 files changed

+577
-557
lines changed

8 files changed

+577
-557
lines changed

MLC/Common/Lisp_Tree_Expr/Operation_Nodes.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def op_compute(self, arg_list):
9898

9999
class Division_Node(Internal_Node):
100100
PROTECTION = 0.001
101+
SIMPLIFY_PROTECTION = 0.01
101102

102103
def __init__(self):
103104
Internal_Node.__init__(self, "/", 1)
@@ -107,9 +108,8 @@ def formal(self):
107108

108109
def _process_division(self, dividend, divisor):
109110
if type(divisor) == np.ndarray:
110-
# Check if at least one element is below the protection value
111-
if [x for x in divisor if abs(x) < Division_Node.PROTECTION] != []:
112-
return np.sign(divisor) * dividend / np.repeat(Division_Node.PROTECTION, len(divisor))
111+
divisor = [Division_Node.PROTECTION if abs(x) < Division_Node.PROTECTION else x for x in divisor]
112+
return np.sign(divisor) * dividend / np.asarray(divisor)
113113
else:
114114
if abs(divisor) < Division_Node.PROTECTION:
115115
return dividend / Division_Node.PROTECTION
@@ -127,7 +127,7 @@ def op_simplify(self):
127127

128128
if not self._nodes[0].is_sensor() and not self._nodes[1].is_sensor():
129129
# FIXME: Harcoded number. Change it
130-
if abs(float(self._nodes[1].to_string())) < 0.01:
130+
if abs(float(self._nodes[1].to_string())) < Division_Node.SIMPLIFY_PROTECTION:
131131
return Leaf_Node(process_float(0))
132132
else:
133133
arg = float(self._nodes[0].to_string()) / float(self._nodes[1].to_string())
@@ -179,6 +179,7 @@ def op_compute(self, arg_list):
179179

180180
class Logarithm_Node(Internal_Node):
181181
PROTECTION = 0.00001
182+
SIMPLIFY_PROTECTION = 0.01
182183

183184
def __init__(self):
184185
Internal_Node.__init__(self, "log", 5)
@@ -188,9 +189,7 @@ def formal(self):
188189

189190
def _process_arg(self, arg):
190191
if type(arg) == np.ndarray:
191-
# Check if at least one element is below the protection value
192-
if [x for x in arg if abs(x) < Logarithm_Node.PROTECTION] != []:
193-
return np.repeat(Logarithm_Node.PROTECTION, len(arg))
192+
return [Logarithm_Node.PROTECTION if abs(x) < Logarithm_Node.PROTECTION else abs(x) for x in arg]
194193
else:
195194
if abs(arg) < Logarithm_Node.PROTECTION:
196195
return Logarithm_Node.PROTECTION
@@ -199,8 +198,8 @@ def _process_arg(self, arg):
199198

200199
def op_simplify(self):
201200
if not self._nodes[0].is_sensor():
202-
if float(self._nodes[0].to_string()) < 0.01:
203-
arg = math.log(0.01)
201+
if float(self._nodes[0].to_string()) < Logarithm_Node.SIMPLIFY_PROTECTION:
202+
arg = math.log(Logarithm_Node.SIMPLIFY_PROTECTION)
204203
else:
205204
arg = math.log(float(self._nodes[0].to_string()))
206205

MLC/individual/Individual.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,6 @@ def __change_const_tree(self, tree_expression, leaf_value_generator):
461461

462462
def __str__(self):
463463
return "value: %s\n" % self.get_value() + \
464-
"type: %s\n" % self.get_type() + \
465464
"cost_history: %s\n" % self.get_cost_history() + \
466465
"evaluation_time: %s\n" % self.get_evaluation_time() + \
467466
"appearences: %s\n" % self.get_appearences() + \

matlab_code/MLC_tools/my_div.m

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
function b=my_div(arg1,arg2)
2-
protec=0.001;
3-
if abs(arg2)>protec
4-
b=arg1./arg2;
2+
protection = 0.001;
3+
sign_array = sign(arg2);
4+
protect_array = abs(arg2) < protection;
5+
elements_to_modify = sum(protect_array);
6+
7+
if elements_to_modify == 0
8+
b=arg1 ./ arg2;
59
else
6-
b=sign(arg2).*arg1./protec;
10+
inverse_protect_array = ~protect_array;
11+
valid_elements = arg2 .* inverse_protect_array;
12+
non_valid_elements = protect_array .* protection;
13+
new_arg2 = valid_elements + non_valid_elements;
14+
b = sign_array .* arg1 ./ new_arg2;
715
end
8-
end
16+
17+
% function b=my_div(arg1,arg2)
18+
% protec=0.001;
19+
% if abs(arg2)>protec
20+
% b=arg1./arg2;
21+
% else
22+
% b=sign(arg2).*arg1./protec;
23+
% end
24+
% end

matlab_code/MLC_tools/my_log.m

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
function b=my_log(arg1)
2-
protec=0.00001;
3-
if abs(arg1)>protec
4-
b=log(abs(arg1));
2+
protection = 0.00001;
3+
sign_array = sign(arg1);
4+
protect_array = abs(arg1) < protection;
5+
elements_to_modify = sum(protect_array);
6+
7+
if elements_to_modify == 0
8+
b = log(abs(arg1));
59
else
6-
b=log(protec);
10+
inverse_protect_array = ~protect_array;
11+
valid_elements = arg1 .* inverse_protect_array;
12+
non_valid_elements = protect_array .* protection;
13+
new_arg1 = valid_elements + non_valid_elements;
14+
b = log(abs(new_arg1));
715
end
8-
end
16+
end
17+
18+
% function b=my_log(arg1)
19+
% protec=0.00001;
20+
% if abs(arg1)>protec
21+
% b=log(abs(arg1));
22+
% else
23+
% b=log(protec);
24+
% end
25+
% end

tests/integration_tests/integration_tests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys, os
2+
sys.path.append("../..")
13
import unittest
24
import matlab.engine
35
import numpy as np
@@ -8,7 +10,6 @@
810
from MLC.mlc_table.MLCTable import MLCTable
911
from MLC.db.mlc_repository import MLCRepository
1012
from MLC.Simulation import Simulation
11-
import sys, os
1213
import yaml
1314

1415
"""
@@ -239,4 +240,4 @@ def execute_integration_test(test_name, integration_test):
239240
print "'%s'? there is no integration test with that name!" % test_name
240241

241242
for test_dir, integration_test in test_to_run.iteritems():
242-
execute_integration_test(test_dir, integration_test)
243+
execute_integration_test(test_dir, integration_test)

tests/integration_tests/test_basic/configuration.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ cascade = 1,1
5151
# evaluation_method = standalone_function
5252
# evaluation_method = standalone_files
5353
evaluation_method = mfile_standalone
54-
evaluation_function = toy_problem
54+
# evaluation_function = toy_problem
55+
evaluation_function = toy_problem_python_ev
5556
indfile = ind.dat
5657
Jfile = J.dat
5758
# exchangedir = fullfile(pwd,evaluator0)

0 commit comments

Comments
 (0)