Skip to content

Commit

Permalink
comments print statements
Browse files Browse the repository at this point in the history
  • Loading branch information
gAldeia committed Apr 23, 2024
1 parent b6476de commit e06cd7c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 44 deletions.
49 changes: 25 additions & 24 deletions pybrush/deap_api/nsga2.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def calculate_statistics(ind):
toolbox.update_current_gen(gen)

# Vary the population
print("--"*20)
print("pop before select")
for p in pop:
print(p.program.get_model())
# print("--"*20)
# print("pop before select")
# for p in pop:
# print(p.program.get_model())
# print(p.fitness.values)
# print(p.fitness.weights)
# print(p.fitness.wvalues)
Expand All @@ -60,39 +60,40 @@ def calculate_statistics(ind):

parents = toolbox.select(pop) # , len(pop) # select method from brush's cpp side will use the values in self.parameters_ to decide how many individuals it should select

print("--"*20)
print("pop after select")
for p in pop:
print(p.program.get_model())
# print("--"*20)
# print("pop after select")
# for p in pop:
# print(p.program.get_model())

print("--"*20)
print("selected parents")
for p in parents:
print(p.program.get_model())
# print("--"*20)
# print("selected parents")
# for p in parents:
# print(p.program.get_model())

offspring = toolbox.vary_pop(parents)
offspring = list(toolbox.map(toolbox.assign_fit, offspring))

print("--"*20)
print("offspring")
for p in offspring:
print(p.program.get_model())
# print("--"*20)
# print("offspring")
# for p in offspring:
# print(p.program.get_model())

# Select the next generation population (no sorting before this step, as
# survive==offspring will cut it in half)
pop = toolbox.survive(pop + offspring)

print("--"*20)
print("pop after survival")
for p in pop:
print(p.program.get_model())
# print("--"*20)
# print("pop after survival")
# for p in pop:
# print(p.program.get_model())

pop = toolbox.migrate(pop)

print("--"*20)
print("pop after migration")
for p in pop:
print(p.program.get_model())
# print("--"*20)
# print("pop after migration")
# for p in pop:
# print(p.program.get_model())

pop.sort(key=lambda x: x.fitness, reverse=True)

record = stats.compile(pop)
Expand Down
4 changes: 2 additions & 2 deletions src/program/tree_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ int TreeNode::get_complexity() const
// include the `w` and `*` if the node is weighted (and it is not a constant or mean label)
if (data.get_is_weighted()
&& !(Is<NodeType::Constant>(data.node_type)
|| Is<NodeType::MeanLabel>(data.node_type)
|| Is<NodeType::OffsetSum>(data.node_type))
|| (Is<NodeType::MeanLabel>(data.node_type)
|| Is<NodeType::OffsetSum>(data.node_type)) )
)
return operator_complexities.at(NodeType::Mul)*(
operator_complexities.at(NodeType::Constant) +
Expand Down
36 changes: 18 additions & 18 deletions src/search_space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ tree<Node>& SearchSpace::PTC2(tree<Node>& Tree,

// auto Tree = tree<Node>();

fmt::print("building program with max size {}, max depth {}",max_size,max_d);
// fmt::print("building program with max size {}, max depth {}",max_size,max_d);

// Queue of nodes that need children
vector<tuple<TreeIter, DataType, int>> queue;
Expand All @@ -289,7 +289,7 @@ tree<Node>& SearchSpace::PTC2(tree<Node>& Tree,

Node root = spot.node->data;

cout << "root " << root.name << endl;
// cout << "root " << root.name << endl;
// auto spot = Tree.set_head(n);

// updating size accordingly to root node
Expand All @@ -305,7 +305,7 @@ tree<Node>& SearchSpace::PTC2(tree<Node>& Tree,
//For each argument position a of n, Enqueue(a; g)
for (auto a : root.arg_types)
{
cout << "queing a node of type " << DataTypeName[a] << endl;
// cout << "queing a node of type " << DataTypeName[a] << endl;
auto child_spot = Tree.append_child(spot);
queue.push_back(make_tuple(child_spot, a, d));
}
Expand All @@ -314,8 +314,8 @@ tree<Node>& SearchSpace::PTC2(tree<Node>& Tree,

Node n;
// Now we actually start the PTC2 procedure to create the program tree
cout << "queue size: " << queue.size() << endl;
cout << "entering first while loop...\n";
// cout << "queue size: " << queue.size() << endl;
// cout << "entering first while loop...\n";
while ( queue.size() + s < max_size && queue.size() > 0)
{
// including the queue size in the max_size, since each element in queue
Expand All @@ -327,14 +327,14 @@ tree<Node>& SearchSpace::PTC2(tree<Node>& Tree,
// always insert a non terminal (which by default has weights off).
// this way, we can have PTC2 working properly.

cout << "queue size: " << queue.size() << endl;
// cout << "queue size: " << queue.size() << endl;
auto [qspot, t, d] = RandomDequeue(queue);

cout << "current depth: " << d << endl;
// cout << "current depth: " << d << endl;
if (d >= max_d || s >= max_size)
{
// choose terminal of matching type
cout << "getting " << DataTypeName[t] << " terminal\n";
// cout << "getting " << DataTypeName[t] << " terminal\n";
// qspot = sample_terminal(t);
// Tree.replace(qspot, sample_terminal(t));
// Tree.append_child(qspot, sample_terminal(t));
Expand All @@ -354,9 +354,9 @@ tree<Node>& SearchSpace::PTC2(tree<Node>& Tree,
else
{
//choose a nonterminal of matching type
cout << "getting op of type " << DataTypeName[t] << endl;
// cout << "getting op of type " << DataTypeName[t] << endl;
auto opt = sample_op(t);
cout << "chose " << n.name << endl;
// cout << "chose " << n.name << endl;
// TreeIter new_spot = Tree.append_child(qspot, n);
// qspot = n;

Expand All @@ -379,7 +379,7 @@ tree<Node>& SearchSpace::PTC2(tree<Node>& Tree,
// For each arg of n, add to queue
for (auto a : n.arg_types)
{
cout << "queing a node of type " << DataTypeName[a] << endl;
// cout << "queing a node of type " << DataTypeName[a] << endl;
// queue.push_back(make_tuple(new_spot, a, d+1));
auto child_spot = Tree.append_child(newspot);

Expand All @@ -399,20 +399,20 @@ tree<Node>& SearchSpace::PTC2(tree<Node>& Tree,
&& Isnt<NodeType::Constant, NodeType::MeanLabel>(n.node_type) )
s += 2;

cout << "current tree size: " << s << endl;
// cout << "current tree size: " << s << endl;
}

cout << "entering second while loop...\n";
// cout << "entering second while loop...\n";
while (queue.size() > 0)
{
if (queue.size() == 0)
break;

cout << "queue size: " << queue.size() << endl;
// cout << "queue size: " << queue.size() << endl;

auto [qspot, t, d] = RandomDequeue(queue);

cout << "getting " << DataTypeName[t] << " terminal\n";
// cout << "getting " << DataTypeName[t] << " terminal\n";
// Tree.append_child(qspot, sample_terminal(t));
// qspot = sample_terminal(t);
// auto newspot = Tree.replace(qspot, sample_terminal(t));
Expand All @@ -426,9 +426,9 @@ tree<Node>& SearchSpace::PTC2(tree<Node>& Tree,
auto newspot = Tree.replace(qspot, n);
}

cout << "final tree:\n"
<< Tree.begin().node->get_model() << "\n"
<< Tree.begin().node->get_tree_model(true) << endl;
// cout << "final tree:\n"
// << Tree.begin().node->get_model() << "\n"
// << Tree.begin().node->get_tree_model(true) << endl;

return Tree;
};
Expand Down

0 comments on commit e06cd7c

Please sign in to comment.