API NeuralNetworkAPI: Easily create neural networks

Discussion in 'Resources' started by Zombie_Striker, Oct 20, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Zombie_Striker

    Links:
    https://github.com/ZombieStriker/NeuralNetworkAPI

    [Has multiple examples and includes source code]
    https://dev.bukkit.org/projects/neuralnetworkapi
    https://www.spigotmc.org/resources/neuralnetworkapi.48619/

    Description:
    NeuralNetworkAPI is a project I have been developing for over a year. The goal of this plugin is to allow everyone, from experienced developers to those just starting to make plugins, to be able to make neural networks.


    Uses:
    Neural networks can be used anywhere, here are a few ideas of what you can do with them:

    • Create swear filters able to detect certain words [Included example:SwearBot]
    • Detect if a user is most likely a bot based on their username [Included example:BotGuesser]
    • Detect what moves a player should make to win a game [Included example: Blackjack]
    • Create new music that the world was never heard.
    • Detect if a player is hacking based on their movements.
    • Create bots to simulate the movements of players.
    Example:
    This is an example of the simplest network you can create: an XOR gate:
    Code:java
    1.  
    2. /**
    3. Copyright (C) 2017 Zombie_Striker
    4.  
    5. This program is free software: you can redistribute it and/or modify
    6. it under the terms of the GNU General Public License as published by
    7. the Free Software Foundation, either version 3 of the License, or
    8. (at your option) any later version.
    9.  
    10. This program is distributed in the hope that it will be useful,
    11. but WITHOUT ANY WARRANTY; without even the implied warranty of
    12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13. GNU General Public License for more details.
    14.  
    15. You should have received a copy of the GNU General Public License
    16. along with this program. If not, see <[URL]https://www.gnu.org/licenses/>[/URL].
    17. **/
    18.  
    19. import java.util.HashMap;
    20. import java.util.Map;
    21. import java.util.concurrent.ThreadLocalRandom;
    22.  
    23. import org.bukkit.ChatColor;
    24. import org.bukkit.command.CommandSender;
    25.  
    26. import me.zombie_striker.neuralnetwork.*;
    27. import me.zombie_striker.neuralnetwork.neurons.*;
    28. import me.zombie_striker.neuralnetwork.neurons.input.InputBooleanNeuron;
    29. import me.zombie_striker.neuralnetwork.senses.Sensory2D_Booleans;
    30. import me.zombie_striker.neuralnetwork.util.DeepReinforcementUtil;
    31.  
    32. public class LogicalXOR extends NNBaseEntity implements Controler {
    33.  
    34. // This object stores all the inputs. The inputs for the constructors is how many values will be stored.
    35. //Currently, this will store all the inputs in a 1x2 array.
    36. public Sensory2D_Booleans binary = new Sensory2D_Booleans(1, 2);
    37.  
    38. public LogicalXOR(boolean createAI) {
    39. this.controler = this;
    40.  
    41. if (createAI) {
    42. /**
    43.   * If createAI is true, then generate the AI with 3 layers (i.e, 1
    44.   * hidden layer), 2 inputs, 5 neurons, and 2 bias neurons. After
    45.   * that, connect all the neurons.
    46.   *
    47.   * If you want to test using random inputs (what should be done, but
    48.   * makes replication and understanding a bit hander), add
    49.   * randomizeNeurons() after connecting then.
    50.   */
    51. this.ai = NNAI.generateAI(this, 1, 3, "output");
    52. for (int binaryIndex = 0; binaryIndex < 2; binaryIndex++) {
    53. InputBooleanNeuron.generateNeuronStatically(ai, 0, binaryIndex,
    54. this.binary);
    55. }
    56.  
    57. for (int neurons = 0; neurons < 5; neurons++)
    58. Neuron.generateNeuronStatically(ai, 1);
    59.  
    60. BiasNeuron.generateNeuronStatically(ai, 0);
    61. BiasNeuron.generateNeuronStatically(ai, 1);
    62.  
    63. connectNeurons();
    64. }
    65. }
    66.  
    67. @Override
    68. public String update() {
    69. /**
    70.   * Simple explanation of these steps:
    71.   *
    72.   * 1) If it is currently learning, change the inputs to either true or false.
    73.   *
    74.   * 2) Let the NN tick and think. This will return the outputs from the OutpuitNeurons
    75.   *
    76.   * 3) If it is not learning, just return the answer.
    77.   *
    78.   * 4) Else, do the logic and see if the answer it gave (thought[0]) was correct.
    79.   *
    80.   * 5) If it was not correct, use the DeepReinforcementUtil to improve it.
    81.   *
    82.   * 6) After inprovement, return a message with if it was correct, the accuracy, the inputs, and what it thought was the output,
    83.   */
    84. if (shouldLearn) {
    85. binary.changeValueAt(0, 0,
    86. ThreadLocalRandom.current().nextBoolean());
    87. binary.changeValueAt(0, 1,
    88. ThreadLocalRandom.current().nextBoolean());
    89. }
    90.  
    91. boolean[] thought = tickAndThink();
    92.  
    93. if (!shouldLearn)
    94. return ("|" + binary.getBooleanAt(0, 0) + " + "
    95. + binary.getBooleanAt(0, 1) + " ~~ " + thought[0]);
    96. boolean logic = (binary.getBooleanAt(0, 0) != binary.getBooleanAt(0, 1));
    97. boolean wasCorrect = (logic == thought[0]);
    98. this.getAccuracy().addEntry(wasCorrect);
    99.  
    100. // IMPROVE IT
    101. HashMap<Neuron, Double> map = new HashMap<>();
    102. for (int i = 0; i < thought.length; i++)
    103. map.put(ai.getNeuronFromId(i), logic ? 1.0 : -1.0);
    104. if (!wasCorrect)
    105. DeepReinforcementUtil.instantaneousReinforce(this, map,1);
    106.  
    107. return (wasCorrect ? ChatColor.GREEN : ChatColor.RED) + "acc "
    108. + getAccuracy().getAccuracyAsInt() + "|"
    109. + binary.getBooleanAt(0, 0) + " + " + binary.getBooleanAt(0, 1)
    110. + " ~~ " + thought[0];
    111.  
    112. }
    113. //Sets the inputs for the NN.
    114. @Override
    115. public void setInputs(CommandSender initiator, String[] args) {
    116. if (this.shouldLearn) {
    117. initiator
    118. .sendMessage("Stop the learning before testing. use /nn stoplearning");
    119. return;
    120. }
    121. if (args.length > 2) {
    122. boolean test = false;
    123. try {
    124. test = Boolean.parseBoolean(args[1]);
    125. } catch (Exception e) {
    126. }
    127. boolean test2 = false;
    128. try {
    129. test2 = Boolean.parseBoolean(args[2]);
    130. } catch (Exception e) {
    131. }
    132. binary.changeValueAt(0, 0, test);
    133. binary.changeValueAt(0, 1, test2);
    134.  
    135. } else {
    136. initiator.sendMessage("Provide two values (True or false)");
    137. }
    138. }
    139.  
    140. //Useful for cloning the class
    141. @Override
    142. public NNBaseEntity clone() {
    143. LogicalXOR thi = new LogicalXOR(false);
    144. thi.ai = this.ai;
    145. return thi;
    146. }
    147.  
    148. //Needed, but does not do anything, since this controler is also the base entity
    149. @Override
    150. public void setBase(NNBaseEntity t) {
    151. }
    152.  
    153. //Used for making sure the NN can be loaded from a config.
    154. public LogicalXOR(Map<String, Object> map) {
    155. super(map);
    156. }
    157.  
    158. }
    The constructor simply generates an NNAI object (which is the network's AI), generates the neurons that it needs, and then connects them.

    After that, in the update method, it has two modes: learning and not-learning

    1. For learning, what the network does is generate two inputs, either true or false. It then calls "tickAndThink", which updates the network to respond to the inputs. After that, it checks if the responce it gives is the same as what it should be, and if it is not, it will use the DeepReinforcementUtil to 'teach' the network.
    2. For not-learning mode, all it does is update the network and return the response.
     
    Last edited: Oct 20, 2017
    timtower, Kermit_23 and AlvinB like this.
  2. Offline

    mine2012craft

    Sounds real cool! The problem is is that my brain is too small to understand such things [sheep]
     
  3. Offline

    Zombie_Striker

    @mine2012craft
    Quite honestly, you can guess your way through most of the process. All you need to do is:
    1. Give it neurons
    2. Give it the ability to set inputs (i.e, create a sensory object (like binary in the example), and fill it with the data you want)
    3. Then in the update method, just tickAndThink. If you're learning, provide the answers you want and call the DeepReinforcementUtil. If you're not learning, return the values.
     
Thread Status:
Not open for further replies.

Share This Page