Generates neural net instances through genetic algorithm for use in task learning by m3pi. Reward determined by detection of black areas on arena floor, or by amount of area explored in limited time. Due to memory size, limited # of nets.

Dependencies:   Ping m3pimaze mbed

Committer:
parkbanks
Date:
Thu Apr 23 20:16:51 2015 +0000
Revision:
0:ac93d9db8dbd
Initial commit, bot fully working.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
parkbanks 0:ac93d9db8dbd 1 /*
parkbanks 0:ac93d9db8dbd 2 This file is part of mbeddedNets.
parkbanks 0:ac93d9db8dbd 3
parkbanks 0:ac93d9db8dbd 4 mbeddedNets is free software: you can redistribute it and/or modify
parkbanks 0:ac93d9db8dbd 5 it under the terms of the GNU General Public License as published by
parkbanks 0:ac93d9db8dbd 6 the Free Software Foundation, either version 3 of the License, or
parkbanks 0:ac93d9db8dbd 7 (at your option) any later version.
parkbanks 0:ac93d9db8dbd 8
parkbanks 0:ac93d9db8dbd 9 mbeddedNets is distributed in the hope that it will be useful,
parkbanks 0:ac93d9db8dbd 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
parkbanks 0:ac93d9db8dbd 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
parkbanks 0:ac93d9db8dbd 12 GNU General Public License for more details.
parkbanks 0:ac93d9db8dbd 13
parkbanks 0:ac93d9db8dbd 14 You should have received a copy of the GNU General Public License
parkbanks 0:ac93d9db8dbd 15 along with this program. If not, see <http://www.gnu.org/licenses/>.
parkbanks 0:ac93d9db8dbd 16 */
parkbanks 0:ac93d9db8dbd 17 //
parkbanks 0:ac93d9db8dbd 18 // Description: For use with the m3pi in conjunction with Ping))) sensors.
parkbanks 0:ac93d9db8dbd 19 // Creates user defined neural nets, iterating through each to
parkbanks 0:ac93d9db8dbd 20 // complete a task. Reward is calculated using reflactance sensors
parkbanks 0:ac93d9db8dbd 21 // on underside of m3pi chassis.
parkbanks 0:ac93d9db8dbd 22
parkbanks 0:ac93d9db8dbd 23 #include <vector>
parkbanks 0:ac93d9db8dbd 24 #include <math.h>
parkbanks 0:ac93d9db8dbd 25 #include <cstdlib>
parkbanks 0:ac93d9db8dbd 26 #include <ctime>
parkbanks 0:ac93d9db8dbd 27
parkbanks 0:ac93d9db8dbd 28 #include "mbed.h"
parkbanks 0:ac93d9db8dbd 29 #include "m3pimaze.h"
parkbanks 0:ac93d9db8dbd 30 #include "Ping.h"
parkbanks 0:ac93d9db8dbd 31 #include "CNeuralNet.h"
parkbanks 0:ac93d9db8dbd 32 #include "CGenAlg.h"
parkbanks 0:ac93d9db8dbd 33 #include "utils.h"
parkbanks 0:ac93d9db8dbd 34
parkbanks 0:ac93d9db8dbd 35 const unsigned int NBots = 6; //set number of networks each bot has
parkbanks 0:ac93d9db8dbd 36 const unsigned int Gens = 48; //number of generations to run
parkbanks 0:ac93d9db8dbd 37 const float MAX = 0.5;
parkbanks 0:ac93d9db8dbd 38 const unsigned int CSz = 90; //half of container size in cm, limiting squashing function
parkbanks 0:ac93d9db8dbd 39
parkbanks 0:ac93d9db8dbd 40 m3pi m3pi(p23,p9,p10);
parkbanks 0:ac93d9db8dbd 41 Ping Pinger1(p15);
parkbanks 0:ac93d9db8dbd 42 Ping Pinger2(p16);
parkbanks 0:ac93d9db8dbd 43 LocalFileSystem local("local");//output storage
parkbanks 0:ac93d9db8dbd 44
parkbanks 0:ac93d9db8dbd 45 int main()
parkbanks 0:ac93d9db8dbd 46 {
parkbanks 0:ac93d9db8dbd 47 float pr1; //right
parkbanks 0:ac93d9db8dbd 48 float pr2; //left
parkbanks 0:ac93d9db8dbd 49 Pinger1.Send();//send ping
parkbanks 0:ac93d9db8dbd 50 Pinger2.Send();
parkbanks 0:ac93d9db8dbd 51 wait_ms(30);
parkbanks 0:ac93d9db8dbd 52 pr1 = Pinger1.Read_cm();
parkbanks 0:ac93d9db8dbd 53 pr2 = Pinger2.Read_cm();
parkbanks 0:ac93d9db8dbd 54 pr1 = pr1 * 100;//for seeding random # generator
parkbanks 0:ac93d9db8dbd 55 pr2 = pr2 * 100;
parkbanks 0:ac93d9db8dbd 56
parkbanks 0:ac93d9db8dbd 57 srand(int(pr2*pr1));
parkbanks 0:ac93d9db8dbd 58 FILE *fp = fopen("/local/out.txt", "a");
parkbanks 0:ac93d9db8dbd 59 fprintf(fp, "Bot tick fit rIn lIn out1 out2");
parkbanks 0:ac93d9db8dbd 60 fprintf(fp, "\n");
parkbanks 0:ac93d9db8dbd 61 fclose(fp);
parkbanks 0:ac93d9db8dbd 62
parkbanks 0:ac93d9db8dbd 63 CNeuralNet myNets[NBots]; //networks
parkbanks 0:ac93d9db8dbd 64 vector<SGenome> botGenes; //genes
parkbanks 0:ac93d9db8dbd 65 CGenAlg* m_pGA; //genetic algorithm
parkbanks 0:ac93d9db8dbd 66 CGenAlg pGA(NBots, 0.1, 0.7, myNets[0].GetNumberOfWeights()); //nets,mutrate,crossrate,weightsinnn
parkbanks 0:ac93d9db8dbd 67 m_pGA = &pGA;
parkbanks 0:ac93d9db8dbd 68 botGenes = m_pGA->GetChromos();
parkbanks 0:ac93d9db8dbd 69
parkbanks 0:ac93d9db8dbd 70 for (int i=0; i<NBots; i++){
parkbanks 0:ac93d9db8dbd 71 myNets[i].PutWeights(botGenes[i].vecWeights);
parkbanks 0:ac93d9db8dbd 72 }
parkbanks 0:ac93d9db8dbd 73
parkbanks 0:ac93d9db8dbd 74 for(int j=0; j<Gens; j++){
parkbanks 0:ac93d9db8dbd 75 for(int i=0; i<NBots; i++){
parkbanks 0:ac93d9db8dbd 76 //int fit = 0;
parkbanks 0:ac93d9db8dbd 77 float fit = 0;
parkbanks 0:ac93d9db8dbd 78 int ticks = 128;
parkbanks 0:ac93d9db8dbd 79 int sensors[5];
parkbanks 0:ac93d9db8dbd 80 m3pi.locate(0,1);
parkbanks 0:ac93d9db8dbd 81 m3pi.sensor_auto_calibrate();//calibrate reflectance sensors
parkbanks 0:ac93d9db8dbd 82
parkbanks 0:ac93d9db8dbd 83 //check if goal found
parkbanks 0:ac93d9db8dbd 84 while(ticks >= 0){
parkbanks 0:ac93d9db8dbd 85 Pinger1.Send();
parkbanks 0:ac93d9db8dbd 86 Pinger2.Send();
parkbanks 0:ac93d9db8dbd 87 wait_ms(30);
parkbanks 0:ac93d9db8dbd 88 pr1 = Pinger1.Read_cm();
parkbanks 0:ac93d9db8dbd 89 pr2 = Pinger2.Read_cm();
parkbanks 0:ac93d9db8dbd 90 if(pr1 < 10 || pr2 < 10){
parkbanks 0:ac93d9db8dbd 91 break;
parkbanks 0:ac93d9db8dbd 92 }
parkbanks 0:ac93d9db8dbd 93 Squash(pr1,CSz,-1,1);
parkbanks 0:ac93d9db8dbd 94 Squash(pr2,CSz,-1,1);
parkbanks 0:ac93d9db8dbd 95 vector<float> inputs; //inputs to network
parkbanks 0:ac93d9db8dbd 96 inputs.push_back(pr1);
parkbanks 0:ac93d9db8dbd 97 inputs.push_back(pr2);
parkbanks 0:ac93d9db8dbd 98 vector<float> output = myNets[i].Update(inputs);//get outputs from network
parkbanks 0:ac93d9db8dbd 99 m3pi.right_motor(output[0] * MAX);
parkbanks 0:ac93d9db8dbd 100 m3pi.left_motor(output[1] * MAX);
parkbanks 0:ac93d9db8dbd 101 m3pi.readsensor(sensors);
parkbanks 0:ac93d9db8dbd 102 if(sensors[0] > 600 && sensors[1] > 600 && sensors[2] > 600 && sensors[3] > 600 && sensors[4] > 600){
parkbanks 0:ac93d9db8dbd 103 fit=384.0;
parkbanks 0:ac93d9db8dbd 104 FILE *fp = fopen("/local/out.txt", "a");
parkbanks 0:ac93d9db8dbd 105 fprintf(fp, "%d, %d, %f, %f, %f, %f, %f", i, ticks, fit, pr1, pr2, output[0], output[1]);
parkbanks 0:ac93d9db8dbd 106 fprintf(fp, "\n");
parkbanks 0:ac93d9db8dbd 107 fclose(fp);
parkbanks 0:ac93d9db8dbd 108 break;
parkbanks 0:ac93d9db8dbd 109 }
parkbanks 0:ac93d9db8dbd 110 FILE *fp = fopen("/local/out.txt", "a");
parkbanks 0:ac93d9db8dbd 111 fprintf(fp, "%d, %d, %f, %f, %f, %f, %f", i, ticks, fit, pr1, pr2, output[0], output[1]);
parkbanks 0:ac93d9db8dbd 112 fprintf(fp, "\n");
parkbanks 0:ac93d9db8dbd 113 fclose(fp);
parkbanks 0:ac93d9db8dbd 114 ticks--;
parkbanks 0:ac93d9db8dbd 115 //fit++;
parkbanks 0:ac93d9db8dbd 116 fit += (output[0] + output[1]);
parkbanks 0:ac93d9db8dbd 117 wait(0.1);
parkbanks 0:ac93d9db8dbd 118 }
parkbanks 0:ac93d9db8dbd 119 m3pi.right_motor(0);//stop bot
parkbanks 0:ac93d9db8dbd 120 m3pi.left_motor(0);
parkbanks 0:ac93d9db8dbd 121 botGenes[i].dFitness = fit;
parkbanks 0:ac93d9db8dbd 122 wait(6); //reset robot position
parkbanks 0:ac93d9db8dbd 123 }
parkbanks 0:ac93d9db8dbd 124 botGenes = m_pGA->Epoch(botGenes);
parkbanks 0:ac93d9db8dbd 125 for (int i=0; i<NBots; ++i){
parkbanks 0:ac93d9db8dbd 126 myNets[i].PutWeights(botGenes[i].vecWeights);
parkbanks 0:ac93d9db8dbd 127 }
parkbanks 0:ac93d9db8dbd 128 }
parkbanks 0:ac93d9db8dbd 129 return 0;
parkbanks 0:ac93d9db8dbd 130 }