Code: Select all
from functools import partial
import pysmile
import pysmile_license
def create_cpt_node(net, id, name, outcomes, x_pos, y_pos):
handle = net.add_node(pysmile.NodeType.CPT, id)
net.set_node_name(handle, name)
net.set_node_position(handle, x_pos, y_pos, 85, 55)
initial_outcome_count = net.get_outcome_count(handle)
for i in range(0, initial_outcome_count):
net.set_outcome_id(handle, i, outcomes[i])
for i in range(initial_outcome_count, len(outcomes)):
net.add_outcome(handle, outcomes[i])
return handle
def construct_SMILE_network_with_n_parents(n_parents):
"""
Construct SMILE predefined network and save it to output folder
Build a causal DAG:
P_1 -> T -> O
P_2 -> T -> O
...
P_n -> T -> O
"""
var_unique_val_dict = {}
var_unique_val_dict["T"] = ["T___1", "T___0"]
var_unique_val_dict["O"] = ["O___1", "O___0"]
for i in range(n_parents):
var_unique_val_dict[f"P_{i + 1}"] = [f"P_{i + 1}___1", f"P_{i + 1}___0"]
#endfor
predefined_hybrid_net = pysmile.Network()
# (1) add all nodes properly
x = 10
var_to_val_list_dict = {}
for var_name in var_unique_val_dict.keys():
#print(var_name)
var_val_list = var_unique_val_dict[var_name]
var_to_val_list_dict[var_name] = var_val_list
create_cpt_node(predefined_hybrid_net, var_name, var_name, var_val_list, 10, x)
x += 10
#endfor
print("SMILE var_to_val_list_dict:")
for var_n, var_val_list in var_to_val_list_dict.items():
print(f"{var_n}: {var_val_list}")
#endfor
# (2)
for i in range(n_parents):
parent = f"P_{i+1}"
predefined_hybrid_net.add_arc(parent, "T")
print(f"method-[construct_SMILE_network_with_n_parents] add_arc: {parent} -> T")
#endfor
predefined_hybrid_net.add_arc('T', "O")
return predefined_hybrid_net
SMILE_net = construct_SMILE_network_with_n_parents(37)
The error message is like below:
Code: Select all
Traceback (most recent call last):
File "20260430_SMILE_network_creation_error_-42.py", line 75, in <module>
SMILE_net = construct_SMILE_network_with_n_parents(37)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "20260430_SMILE_network_creation_error_-42.py", line 67, in construct_SMILE_network_with_n_parents
predefined_hybrid_net.add_arc(parent, "T")
pysmile.SMILEException: SMILE Error -42 in function Network.AddArc
Thanks.