Skip to content

Commit

Permalink
first commit in modularizing the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramblurr committed Feb 12, 2011
1 parent 6c6a449 commit 5cd5c9c
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 262 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*~
*.swp*
*.kdev4
Debug/
Release/
Expand Down
79 changes: 79 additions & 0 deletions Agent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "Agent.h"

#include "settings.h"
#include "helpers.h"
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
Agent::Agent()
{
pos= Vector2f(randf(0,WIDTH),randf(0,HEIGHT));
angle= randf(-M_PI,M_PI);
health= 1.0+randf(0,0.1);
age=0;
spikeLength=0;
red= 0;
gre= 0;
blu= 0;
w1=0;
w2=0;
soundmul=1;
give=0;
clockf1= randf(5,100);
clockf2= randf(5,100);
boost=false;
indicator=0;
gencount=0;
selectflag=0;
ir=0;
ig=0;
ib=0;
hybrid= false;
herbivore= randf(0,1);
repcounter= herbivore*randf(conf::REPRATEH-0.1,conf::REPRATEH+0.1) + (1-herbivore)*randf(conf::REPRATEC-0.1,conf::REPRATEC+0.1);

id=0;

MUTRATE1= 0.003;
MUTRATE2= 0.05;

in.resize(INPUTSIZE, 0);
out.resize(OUTPUTSIZE, 0);
}

Agent::Agent(const Agent& other)
{

}

Agent::~Agent()
{

}

Agent& Agent::operator=(const Agent& other)
{
return *this;
}

bool Agent::operator==(const Agent& other) const
{
///TODO: return ...;
}

void Agent::printSelf()
{
printf("Agent age=%i\n", age);
for (int i=0;i<mutations.size();i++) {
cout << mutations[i];
}
}

void Agent::initEvent(float size, float r, float g, float b)
{
indicator=size;
ir=r;
ig=g;
ib=b;
}
65 changes: 65 additions & 0 deletions Agent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef AGENT_H
#define AGENT_H
#include "vmath.h"
#include <vector>
#include <string>
class Agent
{

public:
Agent();
Agent(const Agent& other);
virtual ~Agent();
virtual Agent& operator=(const Agent& other);
virtual bool operator==(const Agent& other) const;

void printSelf();
//for drawing purposes
void initEvent(float size, float r, float g, float b);
Vector2f pos;

float health; //in [0,2]. I cant remember why.
float angle; //of the bot

float red;
float gre;
float blu;

float w1; //wheel speeds
float w2;
bool boost; //is this agent boosting

float spikeLength;
int age;

std::vector<float> in; //input: 2 eyes, sensors for R,G,B,proximity each, then Sound, Smell, Health
std::vector<float> out; //output: Left, Right, R, G, B, SPIKE

float repcounter; //when repcounter gets to 0, this bot reproduces
int gencount; //generation counter
bool hybrid; //is this agent result of crossover?
float clockf1, clockf2; //the frequencies of the two clocks of this bot
float soundmul; //sound multiplier of this bot. It can scream, or be very sneaky. This is actually always set to output 8
//variables for drawing purposes
float indicator;
float ir;float ig;float ib; //indicator colors
int selectflag; //is this agent selected?
float dfood; //what is change in health of this agent due to giving/receiving?

float give; //is this agent attempting to give food to other agent?

int id;

//inhereted stuff
float herbivore; //is this agent a herbivore? between 0 and 1
float MUTRATE1; //how often do mutations occur?
float MUTRATE2; //how significant are they?

DWRAONBrain brain; //THE BRAIN!!!!

//will store the mutations that this agent has from its parent
//can be used to tune the mutation rate
std::vector<std::string> mutations;
};

#endif // AGENT_H
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} ${OPENGL_INCLUDE_DIRS} ${GLUT_IN

set( SB_SRCS
main.cpp
DWRAONBrain.cpp
Agent.cpp
vmath.cpp )

add_executable(scriptbots ${SB_SRCS})
Expand Down
140 changes: 140 additions & 0 deletions DWRAONBrain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#include "DWRAONBrain.h"
using namespace std;
class Box {
public:

Box();

//props
int type; //0: AND, 1:OR
float kp; //kp: damping strength
vector<float> w; //weight of each connecting box (in [0,inf])
vector<int> id; //id in boxes[] of the connecting box
vector<bool> notted; //is this input notted before coming in?
float bias;

//state variables
float target; //target value this node is going toward
float out; //current output, and history. 0 is farthest back. -1 is latest


};

Box::Box()
{

w.resize(CONNS,0);
id.resize(CONNS,0);
notted.resize(CONNS,0);

//constructor
for (int i=0;i<CONNS;i++) {
w[i]= randf(0.1,2);
id[i]= randi(0,BRAINSIZE);
if (randf(0,1)<0.2) id[i]= randi(0,INPUTSIZE); //20% of the brain AT LEAST should connect to input.
notted[i]= randf(0,1)<0.5;
}

type= (randf(0,1)>0.5)?(0):(1);
kp= randf(0.8,1);
bias= randf(-1,1);

out=0;
target=0;
}

DWRAONBrain::DWRAONBrain()
{

//constructor
for (int i=0;i<BRAINSIZE;i++) {
Box a; //make a random box and copy it over
boxes.push_back(a);

boxes[i].out= a.out;
boxes[i].target= a.target;
boxes[i].type= a.type;
boxes[i].bias= a.bias;
for (int j=0;j<CONNS;j++) {
boxes[i].notted[j]= a.notted[j];
boxes[i].w[j]= a.w[j];
boxes[i].id[j]= a.id[j];

if (randf(0,1)<0.05) boxes[i].id[j]=0;
if (randf(0,1)<0.05) boxes[i].id[j]=5;
if (randf(0,1)<0.05) boxes[i].id[j]=12;
if (randf(0,1)<0.05) boxes[i].id[j]=4;

//boxes[i].id[j]= max(min(BRAINSIZE-1, randi(i-10,i+10)), 0);
if (i<BRAINSIZE/2) {
boxes[i].id[j]= randi(0,INPUTSIZE);
}
}
}

//do other initializations
init();
}
void DWRAONBrain::init()
{

}

void DWRAONBrain::tick(vector< float >& in, vector< float >& out)
{

//do a single tick of the brain

//take first few boxes and set their out to in[].
for (int i=0;i<INPUTSIZE;i++) {
boxes[i].out= in[i];
}

//then do a dynamics tick and set all targets
for (int i=INPUTSIZE;i<BRAINSIZE;i++) {
Box* abox= &boxes[i];

if (abox->type==0) {

//AND NODE
float res=1;
for (int j=0;j<CONNS;j++) {
int idx=abox->id[j];
float val= boxes[idx].out;
if (abox->notted[j]) val= 1-val;
//res= res * pow(val, abox->w[j]);
res= res * val;
}
res*= abox->bias;
abox->target= res;

} else {

//OR NODE
float res=0;
for (int j=0;j<CONNS;j++) {
int idx=abox->id[j];
float val= boxes[idx].out;
if (abox->notted[j]) val= 1-val;
res= res + val*abox->w[j];
}
res+= abox->bias;
abox->target= res;
}

//clamp target
if (abox->target<0) abox->target=0;
if (abox->target>1) abox->target=1;
}

//make all boxes go a bit toward target
for (int i=INPUTSIZE;i<BRAINSIZE;i++) {
Box* abox= &boxes[i];
abox->out =abox->out + (abox->target-abox->out)*abox->kp;
}

//finally set out[] to the last few boxes output
for (int i=0;i<OUTPUTSIZE;i++) {
out[i]= boxes[BRAINSIZE-1-i].out;
}
}
33 changes: 33 additions & 0 deletions DWRAONBrain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef DWRAONBRAIN_H
#define DWRAONBRAIN_H

#include "settings.h"
#include "helpers.h"

#include <vector>

#define INPUTSIZE 20
#define OUTPUTSIZE 9

#define BRAINSIZE 100
#define CONNS 3


class Box; // A private class for DWRAONBrain
/**
* Damped Weighted Recurrent AND/OR Network
*/
class DWRAONBrain
{
public:

std::vector<Box> boxes;

DWRAONBrain();

void tick(std::vector<float>& in, std::vector<float>& out);
private:
void init();
};

#endif
3 changes: 3 additions & 0 deletions helpers.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifndef HELPERS_H
#define HELPERS_H
//uniform random in [a,b)
float randf(float a, float b){return ((b-a)*((float)rand()/RAND_MAX))+a;}

Expand Down Expand Up @@ -32,3 +34,4 @@ float cap(float a){
if (a>1) return 1;
return a;
}
#endif
Loading

0 comments on commit 5cd5c9c

Please sign in to comment.