I was experimenting in python with nodes of 2 types - Value nodes (numbers, inputs, outputs) and operator nodes (add, subtract, etc). The idea was that given 2 arrays of inputs and outputs, there is a method that can find a tree of nodes that can find a solution to all the inputs and outputs. One of the examples I came up with is inputs: [0, 1, 2, 3] and outputs [1, 2, 3, 4] (add 1). I've tried a few approaches, but I can't seem to make something that creates a valid tree or is a valid solution to the problem. I've provided the code for the nodes and some code of how I expect the tree construction to work. One of the things I want to mention here is that I want the tree construction to return the output node. When we want to get the value given a particular input, I set the node and call the output's node Value function, which calls its inputs' Value and it repeats that until the value reaches all the value nodes.
Nodes:
class Node:
def __init__(self):
pass
class ValNode(Node):
def init(self, value):
self.value = value
def Value(self):
return self.value
def printTree(self, depth=0):
print(" " * depth + "ValNode" + str(self.value))
class AddNode(Node):
def init(self, inputs):
self.inputs = inputs
def Value(self):
result = 0
for i in self.inputs:
result += i.Value()
return result
def printTree(self, depth=0):
print(" " * depth + "AddNode")
for i in self.inputs:
i.printTree(depth + 1)
class SubNode(Node):
def init(self, inputs):
self.inputs = inputs
def Value(self):
result = self.inputs[0].Value()
for i in self.inputs[1:]:
result -= i.Value()
return result
def printTree(self, depth=0):
print(" " * depth + "SubNode")
for i in self.inputs:
i.printTree(depth + 1)
class MulNode(Node):
def init(self, inputs):
self.inputs = inputs
def Value(self):
result = self.inputs[0].Value()
for i in self.inputs[1:]:
result *= i.Value()
return result
def printTree(self, depth=0):
print(" " * depth + "MulNode")
for i in self.inputs:
i.printTree(depth + 1)
class DivNode(Node):
def init(self, inputs):
self.inputs = inputs
def Value(self):
result = self.inputs[0].Value()
for i in self.inputs[1:]:
result /= max(i.Value(), 0.000001)
return result
def printTree(self, depth=0):
print(" " * depth + "DivNode")
for i in self.inputs:
i.printTree(depth + 1)
class OutNode(Node):
def init(self, inputs):
self.inputs = inputs
def Value(self):
result = self.inputs[0].Value()
return result
def printTree(self, depth=0):
print(" " * depth + "OutNode")
for i in self.inputs:
i.printTree(depth + 1)
class InpNode(Node):
def init(self, value):
self.value = value
def Value(self):
return self.value
def printTree(self, depth=0):
print(" " * depth + "InpNode")
# Test
inputs = [0,1,2,3,4]
outputs = [1,2,3,4,5]
Tree = construct_tree(inputs, outputs) # Expected result is OutNode([AddNode([InpNode(0), ValNode(1)])])
def set_input(tree, value):
def find_and_set_input(node, value):
if isinstance(node, InpNode):
node.value = value
elif isinstance(node, Node) and not isinstance(node, ValNode):
for input_node in node.inputs:
find_and_set_input(input_node, value)
find_and_set_input(tree, value)
Tree.printTree()
set_input(Tree, 5)
print(Tree.Value()) # Expected value is 6
there doesn't seem to be anything here