Difference between revisions of "BotProgramming Basics"

From Gladiabots Wiki
Jump to: navigation, search
Line 20: Line 20:
 
|Connector node||The diamond-shaped connector node is always evaluated valid during AI check. It connects every node linked to the top of the connector node with every node linked from the bottom of the connector node. A connector node can be used to simplify the layout of the AI.
 
|Connector node||The diamond-shaped connector node is always evaluated valid during AI check. It connects every node linked to the top of the connector node with every node linked from the bottom of the connector node. A connector node can be used to simplify the layout of the AI.
 
|-
 
|-
|Sub-AI node  ||One can reuse an AI inside another one, by using a Sub-AI node. When evaluating this node the whole sub-AI is checked for a valid action. A sub-AI can again contain its own sub-AIs.
+
|Sub-AI node  ||One can reuse an AI inside another one, by using a Sub-AI node. When evaluating this node the whole sub-AI is checked for a valid action. A sub-AI can again contain its own sub-AIs. Dividing the AI in sub-AIs also improves performance when viewing or editing the AI.
 
|-
 
|-
 
|Link          ||Nodes can be connected with a link. A link is a directed connection from the bottom of {{Tooltip|one node|Could be a root node, a condition node or a connector node.}} to the top of {{Tooltip|another node|Could be an action node, a condition node, a connector node or a sub-AI node.}}.
 
|Link          ||Nodes can be connected with a link. A link is a directed connection from the bottom of {{Tooltip|one node|Could be a root node, a condition node or a connector node.}} to the top of {{Tooltip|another node|Could be an action node, a condition node, a connector node or a sub-AI node.}}.
Line 51: Line 51:
 
Some best coding practices from software development also apply to AIs in ''Gladiabots''. The following rules can help improve the quality of AIs, enhancing both the initial development and subsequent maintenance of the AI.
 
Some best coding practices from software development also apply to AIs in ''Gladiabots''. The following rules can help improve the quality of AIs, enhancing both the initial development and subsequent maintenance of the AI.
  
{{InformationCollapsed|1=Divide and conquer|2=Break down a problem into two or more sub-problems and solve each of them separately. Complex sub-problems can again be divided. In ''Gladiabots'' this could be done by creating sub-AIs for each sub-problem. The complexity is reduced leading to a better maintainability.}}
+
{{InformationCollapsed|1=Divide and conquer.|2=Break down a problem into two or more sub-problems and solve each of them separately. Complex sub-problems can again be divided. In ''Gladiabots'' this could be done by creating sub-AIs for each sub-problem. The complexity is reduced leading to a better maintainability.}}
  
{{InformationCollapsed|1=Readability|2=AIs are written once, but read many times. Overlapping nodes and crossing lines should be avoided.}}
+
{{InformationCollapsed|1=Readability first.|2=AIs are written once, but read many times. Overlapping nodes, links crossing each other and great distances between linked nodes are difficult to read and should be avoided. AIs and sub-AIs should have meaningful names revealing the purpose of the AI. One should consider to align nodes to the grid.}}
  
 
{{InformationCollapsed|1=Don't repeat yourself.|2=Avoid using the exact same nodes in several places. Sometimes duplicate nodes can be prevented by using bot specific filters or additional conditions. If its not possible to prevent duplicate nodes in the first place one can try to move them to sub AIs.}}
 
{{InformationCollapsed|1=Don't repeat yourself.|2=Avoid using the exact same nodes in several places. Sometimes duplicate nodes can be prevented by using bot specific filters or additional conditions. If its not possible to prevent duplicate nodes in the first place one can try to move them to sub AIs.}}
  
<!--
+
{{InformationCollapsed|1=Keep it simple.|2=The size of an AI has a significant effect on the error rate. A simple solution should be preferred over a complex one leading to the same or very similar results. One should ask the question: "Has this AI been implemented with the least amount of nodes necessary?". The more complex the AI is the more likely it is to be buggy.}}
  
 +
{{InformationCollapsed|1=Test, test, test.|2="If debugging is the process of removing bugs, then programming must be the process of putting them in." (Edsger Wybe Dijkstra) As no developer is perfect, new nodes should be tested and debugged right after they were added.
  
> Comment it with empty sub-AIs
+
== Debugging AIs ==
  
 
+
{{Warning|The rest of the page is a work in progress.}}
Test new code
+
 
+
 
+
The size of a project or program has a significant effect on error rates,
+
 
+
How adaptable is the program to cope with changing requirements?
+
 
+
"What happens before one gets to the coding stage is often of crucial importance to the success of the project.
+
 
+
 
+
 
+
Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.
+
 
+
Complexity is the enemy of security.[3]
+
 
+
The management of complexity is very important. There is a very basic principle - during the project development ask the question - "has this project been implemented with the least amount of code necessary ?". If it hasn't then unnecessary work has been undertaken and unnecessary cost - both upfront and downstream - has been incurred. This is the "Keep it Simple" rule - simple but effective.
+
 
+
The more complex the code is the more likely it is to be buggy, the more difficult the bugs are to find and the more likely there are to be hidden bugs. Safe coding is very important.
+
 
+
Formating code
+
 
+
Connectors are the GOTOs of AIs. And just like any programming language, GOTOs here too are considered "bad" code. Still, they can save you a lot of work sometimes.
+
Ever have a group of Nodes that you regularly use, and the same thing is present in your AI again and again? Sub-AIs are used like Macros. You can just make an AI with the commonly used group of Nodes, and then add this as a Sub-AI in your main AI. Sub-AIs tend to improve game performance (more Nodes make the game laggier)
+
You can also use Sub-AIs to make Hybrids out of existing AIs. Remember, Sub-AIs are essentially AIs linking other AIs.
+
 
+
 
+
 
+
{{Warning|Rest of the page is a work in progress.}}
+
 
+
== Debugging AIs ==
+
  
 
== Combining condition nodes ==
 
== Combining condition nodes ==
Line 99: Line 70:
  
  
== Best practices for AI design ==
+
<!--
  
 
== Debugging AIs ==
 
== Debugging AIs ==

Revision as of 17:50, 21 October 2017

Information.png This page needs improvement, you are welcome to contribute.

Basics

The robots in Gladiabots are autonomous and obey their Artificial Intelligence (AI). Bots can only execute one of the possible actions at a time. The AIs task is to determine what action to use in the current situation. Each bot checks its AI counterclockwise, searching for the first valid branch leading to an action.

Example AI.png

The AI of a bot is shaped like an upside down tree. It can be composed of the following elements:

Element Description
Root node Each AI contains exactly one root node that defines the starting point of the AI check. Nodes linked from the bottom of the root node are evaluated counterclockwise. The root node indicates the name of the AI.
Action node If a rectangular-shaped action node is evaluated valid during AI check, the described action is executed for the current tick. An action node is considered invalid if the bot cannot perform it.
Condition node If an oval-shaped condition node is evaluated valid during AI check, the nodes linked from the bottom of the condition node are evaluated counterclockwise. A condition node is considered invalid if the current situation doesn't fit it.
Connector node The diamond-shaped connector node is always evaluated valid during AI check. It connects every node linked to the top of the connector node with every node linked from the bottom of the connector node. A connector node can be used to simplify the layout of the AI.
Sub-AI node One can reuse an AI inside another one, by using a Sub-AI node. When evaluating this node the whole sub-AI is checked for a valid action. A sub-AI can again contain its own sub-AIs. Dividing the AI in sub-AIs also improves performance when viewing or editing the AI.
Link Nodes can be connected with a link. A link is a directed connection from the bottom of one node to the top of another node.

AI checking process

Nodes are evaluated counterclockwise
Branches are explored with depth-first search

To determine the action a bot should use in the current situation, the valid nodes are traversed by a depth-first search algorithm beginning with the root node:

Depth-First-Search.gif

Information.png
Details of the AI checking process 

Use the root node as reference node and start at step (1):

  • 1) Get a list of all subnodes linked from the bottom of the reference node. Traverse this list of subnodes ordered counterclockwise:
    • 1a) If the current subnode is an action node and evaluated valid, then the AI checking is discontinued as a valid action is found.
    • 1b) If the current subnode is a sub-AI, then this sub-AI is evaluated. If a valid action is found in it the AI checking is discontinued.
    • 1c) If the current subnode is a condition or connector node and evaluated as valid, then use this subnode as reference node and start a recursive evaluation at step (1). If no valid action is found there, continue at step (1d) afterwards.
    • 1d) If there are further subnodes in step (1) continue with the next subnode at step (1a).
  • 2) If there are no further subnodes in step (1), try to backtrack to the previous evaluation in step (1c). If there is no previous evaluation no valid action could be found. The bot idles instead.
Information.png Branches are not executed counterclockwise but checked counterclockwise.
Information.png The evaluation of nodes is instant and is not wasting any time from the current tick.

Best practices for AI design

Some best coding practices from software development also apply to AIs in Gladiabots. The following rules can help improve the quality of AIs, enhancing both the initial development and subsequent maintenance of the AI.

Information.png
Divide and conquer. 

Break down a problem into two or more sub-problems and solve each of them separately. Complex sub-problems can again be divided. In Gladiabots this could be done by creating sub-AIs for each sub-problem. The complexity is reduced leading to a better maintainability.

Information.png
Readability first. 

AIs are written once, but read many times. Overlapping nodes, links crossing each other and great distances between linked nodes are difficult to read and should be avoided. AIs and sub-AIs should have meaningful names revealing the purpose of the AI. One should consider to align nodes to the grid.

Information.png
Don't repeat yourself. 

Avoid using the exact same nodes in several places. Sometimes duplicate nodes can be prevented by using bot specific filters or additional conditions. If its not possible to prevent duplicate nodes in the first place one can try to move them to sub AIs.

Information.png
Keep it simple. 

The size of an AI has a significant effect on the error rate. A simple solution should be preferred over a complex one leading to the same or very similar results. One should ask the question: "Has this AI been implemented with the least amount of nodes necessary?". The more complex the AI is the more likely it is to be buggy.

{{InformationCollapsed|1=Test, test, test.|2="If debugging is the process of removing bugs, then programming must be the process of putting them in." (Edsger Wybe Dijkstra) As no developer is perfect, new nodes should be tested and debugged right after they were added.

Debugging AIs

Warning.png The rest of the page is a work in progress.

Combining condition nodes

Naming AIs