Util
– utility module¶
This is the utility module, with some utility functions of general use, like list item swap, random utilities and etc.
-
class
Util.
ErrorAccumulator
¶ An accumulator for the Root Mean Square Error (RMSE) and the Mean Square Error (MSE)
-
append
(target, evaluated)¶ Add value to the accumulator
Parameters: - target – the target value
- evaluated – the evaluated value
-
getAdjusted
()¶ Returns the adjusted fitness This fitness is calculated as 1 / (1 + standardized fitness)
-
getMSE
()¶ Return the mean square error
Return type: float MSE
-
getMean
()¶ Return the mean of the non-squared accumulator
-
getNonSquared
()¶ Returns the non-squared accumulator
-
getRMSE
()¶ Return the root mean square error
Return type: float RMSE
-
getSquared
()¶ Returns the squared accumulator
-
reset
()¶ Reset the accumulator
-
-
Util.
G1DListGetEdges
(individual)¶ Get the edges of a G1DList individual
Parameters: individual – the G1DList individual Return type: the edges dictionary
-
Util.
G1DListGetEdgesComposite
(mom, dad)¶ Get the edges and the merge between the edges of two G1DList individuals
Parameters: - mom – the mom G1DList individual
- dad – the dad G1DList individual
Return type: a tuple (mom edges, dad edges, merge)
-
Util.
G1DListMergeEdges
(eda, edb)¶ Get the merge between the two individual edges
Parameters: - eda – the edges of the first G1DList genome
- edb – the edges of the second G1DList genome
Return type: the merged dictionary
-
class
Util.
Graph
¶ The Graph class
- Example:
>>> g = Graph() >>> g.addEdge("a", "b") >>> g.addEdge("b", "c") >>> for node in g: ... print node a b c
New in version 0.6: The Graph class.
-
addEdge
(a, b)¶ Add an edge between two nodes, if the nodes doesn’t exists, they will be created
Parameters: - a – the first node
- b – the second node
-
addNode
(node)¶ Add the node
Parameters: node – the node to add
-
getNeighbors
(node)¶ Returns the neighbors of the node
Parameters: node – the node
-
getNodes
()¶ Returns all the current nodes on the graph
Return type: the list of nodes
-
reset
()¶ Deletes all nodes of the graph
-
Util.
cmp_individual_raw
(a, b)¶ Compares two individual raw scores
- Example:
>>> GPopulation.cmp_individual_raw(a, b)
Parameters: - a – the A individual instance
- b – the B individual instance
Return type: 0 if the two individuals raw score are the same, -1 if the B individual raw score is greater than A and 1 if the A individual raw score is greater than B.
Note
this function is used to sorte the population individuals
-
Util.
cmp_individual_scaled
(a, b)¶ Compares two individual fitness scores, used for sorting population
- Example:
>>> GPopulation.cmp_individual_scaled(a, b)
Parameters: - a – the A individual instance
- b – the B individual instance
Return type: 0 if the two individuals fitness score are the same, -1 if the B individual fitness score is greater than A and 1 if the A individual fitness score is greater than B.
Note
this function is used to sorte the population individuals
-
Util.
importSpecial
(name)¶ This function will import the name module, if fails, it will raise an ImportError exception and a message
Parameters: name – the module name Return type: the module object New in version 0.6: The import_special function
-
Util.
list2DSwapElement
(lst, indexa, indexb)¶ Swaps elements A and B in a 2D list (matrix).
- Example:
>>> l = [ [1,2,3], [4,5,6] ] >>> Util.list2DSwapElement(l, (0,1), (1,1) ) >>> l [[1, 5, 3], [4, 2, 6]]
Parameters: - lst – the list
- indexa – the swap element A
- indexb – the swap element B
Return type: None
-
Util.
listSwapElement
(lst, indexa, indexb)¶ Swaps elements A and B in a list.
- Example:
>>> l = [1, 2, 3] >>> Util.listSwapElement(l, 1, 2) >>> l [1, 3, 2]
Parameters: - lst – the list
- indexa – the swap element A
- indexb – the swap element B
Return type: None
-
Util.
raiseException
(message, expt=None)¶ Raise an exception and logs the message.
- Example:
>>> Util.raiseException('The value is not an integer', ValueError)
Parameters: - message – the message of exception
- expt – the exception class
Return type: None
-
Util.
rand_random
()¶ random() -> x in the interval [0, 1).
-
Util.
randomFlipCoin
(p)¶ Returns True with the p probability. If p is 1, the function will always return True. If p is 0, the function will return always False.
- Example:
>>> Util.randomFlipCoin(1.0) True
Parameters: p – probability, between 0.0 and 1.0 Return type: True or False