seallard / walker

NEAT
MIT License
0 stars 0 forks source link

add_non_recurrent_link can add recurrent links #14

Closed seallard closed 4 years ago

seallard commented 4 years ago

The add_non_recurrent_link actually can add recurrent links, so yeah... 👍 This is fine, but I should rename the method. And I need to detect whether the created link is recurrent and mark it as such. Would be nice to write a test that verifies it works. Maybe create a network where the only possible link addition is a recurrent link.

seallard commented 4 years ago

How should I detect whether a possible link is recurrent (back edge)?

  1. Store a height in each node, if the height of the from node is greater than the height of the to node, the link is recurrent. Set the recurrent boolean accordingly. How does it work when doing crossover?
  2. DFS
seallard commented 4 years ago
// This checks a POTENTIAL link between a potential in_node
// and potential out_node to see if it must be recurrent 
// Use count and thresh to jump out in the case of an infinite loop 
bool is_recur(NNode *potin_node,NNode *potout_node,int &count,int thresh); 

// This checks a POTENTIAL link between a potential in_node and potential out_node to see if it must be recurrent 
bool Network::is_recur(NNode *potin_node,NNode *potout_node,int &count,int thresh) {
    std::vector<Link*>::iterator curlink;

    ++count;  //Count the node as visited

    if (count>thresh) {
        //cout<<"returning false"<<endl;
        return false;  //Short out the whole thing- loop detected
    }

    if (potin_node==potout_node) return true;
    else {
        //Check back on all links...
        for(curlink=(potin_node->incoming).begin();curlink!=(potin_node->incoming).end();curlink++) {
            //But skip links that are already recurrent
            //(We want to check back through the forward flow of signals only
            if (!((*curlink)->is_recurrent)) {
                if (is_recur((*curlink)->in_node,potout_node,count,thresh)) return true;
            }
        }
        return false;
    }
}
seallard commented 4 years ago

I will stick to solution 1 since it seems more intuitive and matches my current representation of nodes. I don't think it is necessary to recalculate the heights after crossover. No matter what, it should be straight forward to do.

seallard commented 4 years ago

It might be necessary to extract the creation of recurrent connections to a separate method to control the rate at which they occur.

seallard commented 4 years ago

https://github.com/seallard/walker/commit/020106c795e65c8dc7397ca085b6b4e5b00d8240