Commit 8679ad44 authored by Hendrik Buschmeier's avatar Hendrik Buschmeier
Browse files

Refactoring: Moved individual decision node modules to nodes.py.

parent a6d76f4c
# -*- coding: utf-8 -*-
from primo.core import Node
class DecisionNode(Node):
"""Handles a DecisionNode which contains a list of actions and has a state"""
def __init__(self, name, value_range):
"""
Initialize a DecisionNode
Keyword arguments:
name -- Name of this DecisionNode
value_range -- A list of actions
"""
super(DecisionNode, self).__init__(name)
self.value_range = value_range
self.state = None
def get_value_range(self):
"""returns a list of actions"""
return self.value_range
def set_value_range(self, value_range):
"""
Sets the value range
Keyword arguments:
value_range -- List of actions
"""
self.value_range = value_range
def announce_parent(self, node):
pass
def set_state(self, decision):
"""
Sets the state of this Decision Node
Keyword arguments:
decision -- The decision that has been made
"""
if decision in self.value_range:
self.state = decision
else:
raise Exception("Could not set the state, given decision is not in value range")
def get_state(self):
"""
Getter for the state
"""
return self.state
def __str__(self):
return self.name + "\n" + str(self.value_range) + "\n" + str(self.state)
# -*- coding: utf-8 -*-
from primo.core import Node
from primo.decision.UtilityTable import UtilityTable
class UtilityNode(Node):
"""Handles an UtilityNode"""
def __init__(self, name):
"""
Construktor for the Utility Node
Keyword arguments:
name -- The name of this node
"""
super(UtilityNode, self).__init__(name)
self.ut = UtilityTable()
def announce_parent(self, node):
"""
Gets called automatically when this node gets a new parent
Keyword arguments:
node -- the parent node of this utility node
"""
self.ut.add_variable(node)
def set_utility_table(self, table, nodes):
"""
Sets the utility table
keyword arguments:
table -- the utility table
nodes -- a list of nodes which are the parents of this utility node
"""
self.ut.set_utility_table(table, nodes)
def set_utility(self, value, assignment):
"""
Sets one utility in the utility table of this node
keyword arguments:
value -- the utlity value
assignment -- a list of assignments of node value pairs
"""
self.ut.set_utility(value, assignment)
def get_utility_table(self):
"""
Getter for the utility table
"""
return self.ut
def get_utility(self, node_value_pairs):
"""
Getter for the utility stored in the utility table
keyword arguments:
node_value_pairs -- list of node,value pairs
"""
return self.ut.get_utility(node_value_pairs)
def __str__(self):
return self.name + "\n" + str(self.ut)
\ No newline at end of file
......@@ -4,11 +4,13 @@ import re
import scipy
from primo.decision.UtilityTable import UtilityTable
from primo.reasoning.density import Beta
from primo.reasoning.density import Exponential
from primo.reasoning.density import Gauss
from primo.reasoning.density import ProbabilityTable
class Node(object):
__metaclass__ = abc.ABCMeta
......@@ -308,3 +310,120 @@ class ContinuousNodeFactory(object):
'''
return ContinuousNode(name,value_range,DensityClass)
class DecisionNode(Node):
"""Handles a DecisionNode which contains a list of actions and has a state"""
def __init__(self, name, value_range):
"""
Initialize a DecisionNode
Keyword arguments:
name -- Name of this DecisionNode
value_range -- A list of actions
"""
super(DecisionNode, self).__init__(name)
self.value_range = value_range
self.state = None
def get_value_range(self):
"""returns a list of actions"""
return self.value_range
def set_value_range(self, value_range):
"""
Sets the value range
Keyword arguments:
value_range -- List of actions
"""
self.value_range = value_range
def announce_parent(self, node):
pass
def set_state(self, decision):
"""
Sets the state of this Decision Node
Keyword arguments:
decision -- The decision that has been made
"""
if decision in self.value_range:
self.state = decision
else:
raise Exception("Could not set the state, given decision is not in value range")
def get_state(self):
"""
Getter for the state
"""
return self.state
def __str__(self):
return self.name + "\n" + str(self.value_range) + "\n" + str(self.state)
class UtilityNode(Node):
"""Handles an UtilityNode"""
def __init__(self, name):
"""
Construktor for the Utility Node
Keyword arguments:
name -- The name of this node
"""
super(UtilityNode, self).__init__(name)
self.ut = UtilityTable()
def announce_parent(self, node):
"""
Gets called automatically when this node gets a new parent
Keyword arguments:
node -- the parent node of this utility node
"""
self.ut.add_variable(node)
def set_utility_table(self, table, nodes):
"""
Sets the utility table
keyword arguments:
table -- the utility table
nodes -- a list of nodes which are the parents of this utility node
"""
self.ut.set_utility_table(table, nodes)
def set_utility(self, value, assignment):
"""
Sets one utility in the utility table of this node
keyword arguments:
value -- the utlity value
assignment -- a list of assignments of node value pairs
"""
self.ut.set_utility(value, assignment)
def get_utility_table(self):
"""
Getter for the utility table
"""
return self.ut
def get_utility(self, node_value_pairs):
"""
Getter for the utility stored in the utility table
keyword arguments:
node_value_pairs -- list of node,value pairs
"""
return self.ut.get_utility(node_value_pairs)
def __str__(self):
return self.name + "\n" + str(self.ut)
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment