/usr/share/pyshared/pebl/learner/base.py is in python-pebl 1.0.2-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | import numpy as N
from pebl import network, config, evaluator, data, prior
from pebl.taskcontroller.base import Task
#
# Exceptions
#
class CannotAlterNetworkException(Exception):
pass
#
# Module parameters
#
_plearnertype = config.StringParameter(
'learner.type',
"""Type of learner to use.
The following learners are included with pebl:
* greedy.GreedyLearner
* simanneal.SimulatedAnnealingLearner
* exhaustive.ListLearner
""",
default = 'greedy.GreedyLearner'
)
_ptasks = config.IntParameter(
'learner.numtasks',
"Number of learner tasks to run.",
config.atleast(0),
default=1
)
class Learner(Task):
def __init__(self, data_=None, prior_=None, **kw):
self.data = data_ or data.fromconfig()
self.prior = prior_ or prior.fromconfig()
self.__dict__.update(kw)
# parameters
self.numtasks = config.get('learner.numtasks')
# stats
self.reverse = 0
self.add = 0
self.remove = 0
def _alter_network_randomly_and_score(self):
net = self.evaluator.network
n_nodes = self.data.variables.size
max_attempts = n_nodes**2
# continue making changes and undoing them till we get an acyclic network
for i in xrange(max_attempts):
node1, node2 = N.random.random_integers(0, n_nodes-1, 2)
if (node1, node2) in net.edges:
# node1 -> node2 exists, so reverse it.
add,remove = [(node2, node1)], [(node1, node2)]
elif (node2, node1) in net.edges:
# node2 -> node1 exists, so remove it
add,remove = [], [(node2, node1)]
else:
# node1 and node2 unconnected, so connect them
add,remove = [(node1, node2)], []
try:
score = self.evaluator.alter_network(add=add, remove=remove)
except evaluator.CyclicNetworkError:
continue # let's try again!
else:
if add and remove:
self.reverse += 1
elif add:
self.add += 1
else:
self.remove += 1
return score
# Could not find a valid network
raise CannotAlterNetworkException()
def _all_changes(self):
net = self.evaluator.network
changes = []
# edge removals
changes.extend((None, edge) for edge in net.edges)
# edge reversals
reverse = lambda edge: (edge[1], edge[0])
changes.extend((reverse(edge), edge) for edge in net.edges)
# edge additions
nz = N.nonzero(invert(net.edges.adjacency_matrix))
changes.extend( ((src,dest), None) for src,dest in zip(*nz) )
return changes
|