# How do you implement NEAT by taking into account the loops?

I'm working on my own implementation of NEAT algorithm based on the original 2002 paper called "Efficient Reinforcement Learning through Evolving Neural Network Topologies" (by Kenneth O. Stanley and Risto Miikkulainen). The way the algorithm is designed it may generate loops in connection of hidden layer. Which obviously will cause difficulties in calculating the output.

I have searched and came across two types of approaches. One set like this example claim that the value should be calculated like a time series usually seen in RNNs and the circular nodes should use "old" values as their "current" output. But, this seems wrong since the training data is not always ordered and the previous value has nothing to do with current one.

A second group like this example claim that the structure should be pruned with some method to avoid loops and cycles. This approach apart from being really expensive to do, is also against the core idea of the algorithm. Deleting connections like this may cause later structural changes.

I my self have so far tried setting the unknown forward values as 0 and this hides the connection (as whatever weight it has will have no effect on the result) but have failed also for two reasons. One is my networks get big quickly destroying the "smallest network required" idea and also not good results.

What is the correct approach?

NEAT does not employ a feed forward concept and also it does not take any special action to avoid loops.

Network is evaluated in non-recursive model. The only non-deterministic loop, the evaluation has is the loop for activating all the outputs. Pseudo code is something like this,

Until all the outputs are active
for all non-sensor nodes
activate node
sum the input
for all non-sensor and active nodes
calculate the output


NOTE1: You can use a defensive mechanism (like a counter) to avoid a infinite loop

NOTE2: When summing the input, outputs of nodes which were at least evaluated/calculated once are considered, otherwise their outputs are assumed to be zero.

This is the note from the author of NEAT about identifying loops,

Note that checking networks for loops in general in not necessary and therefore I stopped writing this function

bool Network::integrity()

• I don't think that we are on the same page. If node A has a connection to node B and node B has a connection to node A you can't get the input of node A because it depends on it self. You added a note to stop infinite loops but how would you calculate the initial iteration?
Dec 16, 2019 at 11:22
• @Emad, I have added another note regarding how NEAT handles the dependent value issue. That is: When summing the input, outputs of nodes which were at least evaluated/calculated once are considered, otherwise their outputs are assumed to be zero. Dec 18, 2019 at 3:41
• For example, suppose both A and B has links from sensors (actual input layer), in the first iteration of outer loop, A's input summing disregards B's output since B is not evaluated yet. And B does the same since A's output is not evaluated yet. Then both A and B calculate their outputs in the second inner loop. Then in the second iteration of outer loop, both A and B consider each other's outputs from first iteration. So you see this continues without an issue. Let me know if you need more clarification Dec 18, 2019 at 4:02
• Ok great. Now I understand. Can you please cite any reference for this. This is same as the first approach that I mentioned in the OP. But I fail to verify this in literature.
Dec 18, 2019 at 4:55
• Literature fails to express all these implementation details I guess. However next valid reference we can take is the original source code, which is nn.cs.utexas.edu/?neat-c Dec 18, 2019 at 13:24

You can use a feed forward style network, so that every node outputs to a higher node except output nodes. This will eliminate connection loops.

• "higher" in what sense? Innovation number? or something else?
{"ft_lang_label":"__label__en","ft_lang_prob":0.9602995,"math_prob":0.776851,"size":1367,"snap":"2022-27-2022-33","text_gpt3_token_len":272,"char_repetition_ratio":0.09464417,"word_repetition_ratio":0.017777778,"special_character_ratio":0.19092904,"punctuation_ratio":0.06147541,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9595508,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-29T10:54:57Z\",\"WARC-Record-ID\":\"<urn:uuid:e9219854-6e3c-4cee-897d-40db02378a58>\",\"Content-Length\":\"230660\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bfdf12ae-e8ee-41ad-844c-5445a5ecaf23>\",\"WARC-Concurrent-To\":\"<urn:uuid:0f85b665-3886-457e-9910-0d4e18d8516b>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://ai.stackexchange.com/questions/10641/how-do-you-implement-neat-by-taking-into-account-the-loops\",\"WARC-Payload-Digest\":\"sha1:S45KP5JZLHKRTLSM7567A6CHG55IXAQU\",\"WARC-Block-Digest\":\"sha1:U42DIFYJO6AEDZVQFATKWQ4OIJAI7CVK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103626162.35_warc_CC-MAIN-20220629084939-20220629114939-00099.warc.gz\"}"}