Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove support for calling graph directly #270

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 0 additions & 14 deletions cgp/cartesian_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,6 @@ def determine_active_regions(self) -> List[int]:

return active_regions

def __call__(self, x: List[float]) -> List[float]:
# store values of x in input nodes
for i, xi in enumerate(x):
assert isinstance(self._nodes[i], InputNode)
self._nodes[i]._output = xi

# evaluate active nodes in order
active_nodes_by_hidden_column_idx = self._determine_active_nodes()
for hidden_column_idx in sorted(active_nodes_by_hidden_column_idx):
for node in active_nodes_by_hidden_column_idx[hidden_column_idx]:
node(x, self)

return [node._output for node in self.output_nodes]

def __getitem__(self, key: int) -> Node:
return self._nodes[key]

Expand Down
16 changes: 0 additions & 16 deletions cgp/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ def __init_subclass__(cls: Type["Node"]) -> None:
super().__init_subclass__()
register(cls)

def __call__(self, x: List[float], graph: "CartesianGraph") -> None:
raise NotImplementedError

@property
def arity(self) -> int:
return self._arity
Expand Down Expand Up @@ -210,19 +207,6 @@ def initial_value(cls, parameter_name: str) -> float:
def parameter_str(self) -> List[str]:
return self._parameter_str

def __call__(self, x: List[float], graph: "CartesianGraph") -> None:
output_str = str(self._def_output)

if len(self._parameter_names) > 0:
raise RuntimeError("can not call a node that uses parameters")

for input_name in self._input_names:
idx = self._extract_index_from_input_name(input_name)
output_str = output_str.replace(input_name, f"{graph[self._input_nodes[idx]].output}")

exec_str = f"self._output = {output_str}"
exec(exec_str)

def _replace_input_names(self, output_str: str, graph: "CartesianGraph") -> str:
for input_name in self._input_names:
idx = self._extract_index_from_input_name(input_name)
Expand Down
22 changes: 0 additions & 22 deletions test/test_cartesian_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,6 @@
from cgp.genome import ID_INPUT_NODE, ID_NON_CODING_GENE, ID_OUTPUT_NODE


def test_direct_input_output():
params = {"n_inputs": 1, "n_outputs": 1, "n_columns": 3, "n_rows": 3, "levels_back": 2}
primitives = (cgp.Add, cgp.Sub)
genome = cgp.Genome(
params["n_inputs"],
params["n_outputs"],
params["n_columns"],
params["n_rows"],
params["levels_back"],
primitives,
)
genome.randomize(np.random)

genome[-2:] = [0, ID_NON_CODING_GENE] # set inputs for output node to input node
graph = cgp.CartesianGraph(genome)

x = [2.14159]
y = graph(x)

assert x[0] == pytest.approx(y[0])


def test_to_func_simple():
primitives = (cgp.Add,)
genome = cgp.Genome(2, 1, 1, 1, 1, primitives)
Expand Down
44 changes: 16 additions & 28 deletions test/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ def test_inputs_are_cut_to_match_arity():
assert node.input_nodes == input_nodes[:2]


def _test_graph_call_and_to_x_compilations(
def _test_to_x_compilations(
genome,
x,
y_target,
*,
test_graph_call=True,
test_to_func=True,
test_to_numpy=True,
test_to_torch=True,
test_to_sympy=True,
):
if test_graph_call:
_test_graph_call(genome, x, y_target)
if test_to_func:
_test_to_func(genome, x, y_target)
if test_to_numpy:
Expand All @@ -47,11 +44,6 @@ def _test_graph_call_and_to_x_compilations(
_test_to_sympy(genome, x, y_target)


def _test_graph_call(genome, x, y_target):
graph = cgp.CartesianGraph(genome)
assert graph(x) == pytest.approx(y_target)


def _test_to_func(genome, x, y_target):
graph = cgp.CartesianGraph(genome)
assert graph.to_func()(x) == pytest.approx(y_target)
Expand Down Expand Up @@ -107,7 +99,7 @@ def test_add():
x = [5.0, 1.5]
y_target = [x[0] + x[1]]

_test_graph_call_and_to_x_compilations(genome, x, y_target)
_test_to_x_compilations(genome, x, y_target)


def test_sub():
Expand Down Expand Up @@ -141,7 +133,7 @@ def test_sub():
x = [5.0, 1.5]
y_target = [x[0] - x[1]]

_test_graph_call_and_to_x_compilations(genome, x, y_target)
_test_to_x_compilations(genome, x, y_target)


def test_mul():
Expand Down Expand Up @@ -175,7 +167,7 @@ def test_mul():
x = [5.0, 1.5]
y_target = [x[0] * x[1]]

_test_graph_call_and_to_x_compilations(genome, x, y_target)
_test_to_x_compilations(genome, x, y_target)


def test_div():
Expand Down Expand Up @@ -209,7 +201,7 @@ def test_div():
x = [5.0, 1.5]
y_target = [x[0] / x[1]]

_test_graph_call_and_to_x_compilations(genome, x, y_target)
_test_to_x_compilations(genome, x, y_target)


def test_pow():
Expand Down Expand Up @@ -243,7 +235,7 @@ def test_pow():
x = [5.0, 1.5]
y_target = [x[0] ** x[1]]

_test_graph_call_and_to_x_compilations(genome, x, y_target)
_test_to_x_compilations(genome, x, y_target)


def test_constant_float():
Expand Down Expand Up @@ -273,7 +265,7 @@ def test_constant_float():
x = [1.0, 2.0]
y_target = [1.0] # by default the output value of the ConstantFloat node is 1.0

_test_graph_call_and_to_x_compilations(genome, x, y_target)
_test_to_x_compilations(genome, x, y_target)


def test_parameter():
Expand All @@ -292,7 +284,7 @@ def test_parameter():
x = [1.0]
y_target = [1.0] # by default the output value of the Parameter node is 1.0

_test_graph_call_and_to_x_compilations(genome, x, y_target, test_graph_call=False)
_test_to_x_compilations(genome, x, y_target)


def test_parameter_w_custom_initial_value():
Expand All @@ -316,7 +308,7 @@ class CustomParameter(cgp.Parameter):
x = [1.0]
y_target = [initial_value]

_test_graph_call_and_to_x_compilations(genome, x, y_target, test_graph_call=False)
_test_to_x_compilations(genome, x, y_target)


def test_parameter_two_nodes():
Expand Down Expand Up @@ -353,7 +345,7 @@ def test_parameter_two_nodes():
# hence the sum of two Parameter nodes is 2.0
y_target = [2.0]

_test_graph_call_and_to_x_compilations(genome, x, y_target, test_graph_call=False)
_test_to_x_compilations(genome, x, y_target)


def test_parameter_w_random_initial_value(rng_seed):
Expand Down Expand Up @@ -486,15 +478,15 @@ def test_if_else_operator():

x_0 = [1.0, 10.0, -20.0]
y_target_0 = [10.0]
_test_graph_call_and_to_x_compilations(genome, x_0, y_target_0)
_test_to_x_compilations(genome, x_0, y_target_0)

x_1 = [0.0, 10.0, -20.0]
y_target_1 = [10.0]
_test_graph_call_and_to_x_compilations(genome, x_1, y_target_1)
_test_to_x_compilations(genome, x_1, y_target_1)

x_2 = [-1.0, 10.0, -20.0]
y_target_2 = [-20.0]
_test_graph_call_and_to_x_compilations(genome, x_2, y_target_2)
_test_to_x_compilations(genome, x_2, y_target_2)


def test_raise_broken_def_output():
Expand Down Expand Up @@ -591,7 +583,7 @@ class MyScaledAdd(cgp.node.OperatorNode):
x = [5.0, 1.5]
y_target = [2 * (x[0] + x[1])]

_test_graph_call_and_to_x_compilations(genome, x, y_target)
_test_to_x_compilations(genome, x, y_target)


def test_custom_node_with_custom_atomic_operator():
Expand Down Expand Up @@ -635,9 +627,7 @@ class MyScaledAdd(cgp.node.OperatorNode):
x = [5.0, 1.5]
y_target = [2 * (x[0] + x[1])]

_test_graph_call_and_to_x_compilations(
genome, x, y_target, test_graph_call=False, test_to_sympy=False
)
_test_to_x_compilations(genome, x, y_target, test_to_sympy=False)


def test_custom_node_with_custom_atomic_operator_with_external_library():
Expand Down Expand Up @@ -683,6 +673,4 @@ class MyScaledAdd(cgp.node.OperatorNode):
x = [5.0, 1.5]
y_target = [scipy_const.golden_ratio * (x[0] + x[1])]

_test_graph_call_and_to_x_compilations(
genome, x, y_target, test_graph_call=False, test_to_sympy=False
)
_test_to_x_compilations(genome, x, y_target, test_to_sympy=False)