2424import numpy as np
2525import MLC .Log .log as lg
2626import matplotlib .pyplot as plt
27+ import random
2728import sys
2829import time
2930
3233
3334
3435def individual_data (indiv ):
35- x = np .linspace (- 10.0 , 10.0 , num = 201 )
36+ SAMPLES = 201
37+ x = np .linspace (- 10.0 , 10.0 , num = SAMPLES )
3638 y = np .tanh (x ** 3 - x ** 2 - 1 )
3739
38- # FIXME: Added timeout to visualize
39- # time.sleep(0.5)
40-
4140 config = Config .get_instance ()
42- # artificial_noise = self._config.getint('EVALUATOR', 'artificialnoise')
43-
44- # In this test we have no noise by config file. But, if this were the
45- # case, we would a have problem because the random of MATLAB is not
46- # the random of Python :(
47- # WORKAROUND: Generate the noise in matlab and process it in python
48-
49- # MAKE SOME NOISE!!!
50- # noise = eng.eval('rand(length(zeros(1, ' + str(len(x)) + ')))-0.5')
51- # np_noise = np.array([s for s in noise[0]])
52- # y2 = y + np_noise * 500 * artificial_noise
53-
54- y2 = y
41+ artificial_noise = config .getint ('EVALUATOR' , 'artificialnoise' )
42+ y_with_noise = y + [random .random () - 0.5 for _ in xrange (SAMPLES )] + artificial_noise * 500
5543
5644 if isinstance (indiv .get_formal (), str ):
5745 formal = indiv .get_formal ().replace ('S0' , 'x' )
@@ -61,35 +49,33 @@ def individual_data(indiv):
6149
6250 # Calculate J like the sum of the square difference of the
6351 # functions in every point
64-
6552 lg .logger_ .debug ('[POP][TOY_PROBLEM] Individual Formal: ' + formal )
66- mlc_y3 = indiv .get_tree ().calculate_expression ([x ])
53+ b = indiv .get_tree ().calculate_expression ([x ])
6754
6855 # If the expression doesn't have the term 'x',
6956 # the eval returns a value (float) instead of an array.
7057 # In that case transform it to an array
71- if type (mlc_y3 ) == float :
72- mlc_y3 = np .repeat (mlc_y3 , len ( x ) )
58+ if type (b ) == float :
59+ b = np .repeat (b , SAMPLES )
7360
74- return x , y , y2 , mlc_y3
61+ return x , y , y_with_noise , b
7562
7663
7764def cost (indiv ):
78- x , y , y2 , mlc_y3 = individual_data (indiv )
65+ x , y , y_with_noise , b = individual_data (indiv )
7966
80- # Deactivate the numpy warnings, because this sum raise an overflow
67+ # Deactivate the numpy warnings, because this sum could raise an overflow
8168 # Runtime warning from time to time
8269 np .seterr (all = 'ignore' )
83- cost_mlc_y3 = float (np .sum ((mlc_y3 - y2 )** 2 ))
70+ cost_value = float (np .sum ((b - y_with_noise )** 2 ))
8471 np .seterr (all = 'warn' )
8572
86- return cost_mlc_y3
73+ return cost_value
8774
8875
8976def show_best (index , generation , indiv , cost , block = True ):
90- x , y , y2 , mlc_y3 = individual_data (indiv )
91- # FIXME: Absolute only makes sense if we're working with complex numbers. It's not the case...
92- y4 = np .sqrt ((y - mlc_y3 )** 2 / (1 + np .absolute (x ** 2 )))
77+ x , y , y_with_noise , b = individual_data (indiv )
78+ cuadratic_error = np .sqrt ((y_with_noise - b )** 2 / (1 + np .absolute (x ** 2 )))
9379
9480 fig = plt .figure ()
9581 # Put figure window on top of all other windows
@@ -101,9 +87,9 @@ def show_best(index, generation, indiv, cost, block=True):
10187 cost ,
10288 indiv .get_formal ()))
10389 plt .subplot (2 , 1 , 1 )
104- plt .plot (x , y , x , y2 , '*' , x , mlc_y3 )
90+ plt .plot (x , y , x , y_with_noise , '*' , x , b )
10591
10692 plt .subplot (2 , 1 , 2 )
107- plt .plot (x , y4 , '*r' )
93+ plt .plot (x , cuadratic_error , '*r' )
10894 plt .yscale ('log' )
10995 plt .show (block = block )
0 commit comments