Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
skdeng committed Oct 7, 2014
0 parents commit 31976ad
Show file tree
Hide file tree
Showing 11 changed files with 365 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Agent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "State.h"

#include "Agent.h"

Agent::Agent()
{
string name = "Individual " + NextValidID;
Agent(name);
}

Agent::Agent(string name)
{
int x = RandInt(0, CParams::WorldX);
int y = RandInt(0, CParams::WorldY);
int z = CParams::GroundLevel;
Agent(name, x, y, z);
}

Agent(string name, int x, int y, int z)
{
Name = name;
Position = vec3(x, y, z);
Look = vec3(random);
MoveDirection = vec3(0);
Speed = 0;

CurrentState = new State()
}

Agent::~Agent()
{

}

void Agent::ChangeState(State* newState)
{
assert(CurrentState && newState);

CurrentState->Exit(this);
CurrentState = newState;
CurrentState->Enter(this);
}
51 changes: 51 additions & 0 deletions Agent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef _AGENT_H_
#define _AGENT_H_

#include "SVector2D.h"
#include "Object.h"

enum AGENT_STATE
{
STATE_ROAM,
STATE_FLEE
};

class Agent : public Object
{
public:
Agent();
Agent(string name);
Agent(string name, int x, int y, int z);
~Agent();

void Move(float direction, float distance);

void ChangeState(State* newState);
void Interact(Object* target);

void Update();

vector<Object*> GetSurrounding(float radius);
vector<Object*> GetSurrounding(int numObj);

// Accessor functions
public:
vec3 GetPosition const () { return Position; }
vec3 GetMoveDirection const () { return MoveDirection; }
vec3 GetLook const () { return Look; }
float GetSpeed const () { return Speed; }
State* GetState const () { return CurrentState; }

private:
string Name;

vec3 Look;
vec3 MoveDirection;
float Speed;

State* CurrentState;


};

#endif
6 changes: 6 additions & 0 deletions New Text Document.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Artificial
Self
Teaching
Roaming
Intelligent
Agent
31 changes: 31 additions & 0 deletions Object.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef _OBJECT_H_
#define _OBJECT_H_

enum OBJ_FLAG
{
FLAG_NONE = 0x00000000,
FLAG_GRAVITY = 0x00000002,
FLAG_GHOST = 0x00000004
};

typedef unsigned int uint;

class Object
{
public:
Object();
virtual ~Object();
vec3 GetPosition const () { return Position; }
OBJ_FLAG GetFlag const () { return Flag; }
uint GetID const () { return ID; }

virtual void Update() = 0;

protected:
vec3 Position;
OBJ_FLAG Flag;
uint ID;
static uint NextValidID;
};

#endif
66 changes: 66 additions & 0 deletions Params.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "Params.h"

char* CParams::File = "param.ini";

//Set everything to zero before loading the values from .ini file
double CParams::Pi = 3.14159265358979;
double CParams::HalfPi = Pi / 2;
double CParams::TwoPi = Pi * 2;
char* CParams::WindowName = "NeuralNetwork";
int CParams::WindowWidth = 480;
int CParams::WindowHeight = 360;
int CParams::FoodCount = 10;
int CParams::InitIndividualCount = 10;
int CParams::WorldMinX = -1000;
int CParams::WorldMaxX = 1000;
int CParams::WorldMinY = -1000;
int CParams::WorldMaxY = 1000;
int CParams::WorldMinZ = -1000;
int CParams::WorldMaxZ = 1000;
int CParams::GroundLevel = 0;
double CParams::ActivationResponse = 0;
double CParams::Bias = 0;
double CParams::CrossoverRate = 0;
double CParams::MutationRate = 0;
double CParams::MaxPerturbation = 0;
int CParams::DecayTimeInterval = 0;
int CParams::AgeTimeInterval = 0;

//Load the paramets from the file
//Return false if failed to load file
bool CParams::Load(char* file)
{
ifstream inFile(file);

//Verify file opening
if (!inFile)
{
return false;
}

//Load from the file
char ParamDescription[40];
inFile >> ParamDescription >> WindowWidth;
inFile >> ParamDescription >> WindowHeight;
inFile >> ParamDescription >> FoodCount;
inFile >> ParamDescription >> InitIndividualCount;
inFile >> ParamDescription >> WorldMinX >> WorldMaxX;
inFile >> ParamDescription >> WorldMinY >> WorldMaxY;
inFile >> ParamDescription >> WorldMinZ >> WorldMaxZ;
inFile >> ParamDescription >> GroundLevel;
inFile >> ParamDescription >> ActivationResponse;
inFile >> ParamDescription >> Bias;
inFile >> ParamDescription >> CrossoverRate;
inFile >> ParamDescription >> MutationRate;
inFile >> ParamDescription >> MaxPerturbation;
inFile >> ParamDescription >> DecayTimeInterval;
inFile >> ParamDescription >> AgeTimeInterval;
inFile >> ParamDescription >> MatingCooldown;

return true;
}

void CParams::Store(char* file)
{

}
62 changes: 62 additions & 0 deletions Params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#ifndef _PARAM_H_
#define _PARAM_H_

#include <fstream>
#include <Windows.h>

using namespace std;

class CParams
{
public:
static char* File;

// ===== General parameters ======
static double Pi;
static double HalfPi;
static double TwoPi;

static char* WindowName;
static int WindowWidth;
static int WindowHeight;

// ===== General properties =====
static int FoodCount;
static int InitIndividualCount;

// ===== Environmental properties =====
static int WorldMinX;
static int WorldMaxX;
static int WorldMinY;
static int WorldMaxY;
static int WorldMinZ;
static int WorldMaxZ;
static int GroundLevel;

// ===== Genetic algorithm =====
static double CrossoverRate;
static double MutationRate;
static double MaxPerturbation; //Maximum amount the genetic algorithm may mutate each weight by
static double ActivationResponse; //For tweaking sigmoid function
static double Bias; //bias value

// ===== Individual properties =====
static int DecayTimeInterval; //in millisecond
static int AgeTimeInterval; //in millisecond


CParams()
{
if (!Load(File))
{
char* errormsg;
sprintf(errormsg, "Cannot find %s", File);
MessageBox(NULL, errormsg, "Error", MB_ICONERROR);
}
}

static bool Load(char* file);
static void Store(char* file);
};

#endif
28 changes: 28 additions & 0 deletions State.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "State.h"

State::State()
{

}

State::~State()
{

}

// ===== Pure virtual functions =====

void State::Enter(Object*)
{

}

void State::Execute(Object*)
{

}

void State::Exit(Object*)
{

}
31 changes: 31 additions & 0 deletions State.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef _STATE_H_
#define _STATE_H_

#include "Object.h"

class State
{
public:
State();
virtual ~State();

virtual void Enter(Object*) = 0;
virtual void Execute(Object*) = 0;
virtual void Exit(Object*) = 0;
};

class RoamState : public State
{
public:
RoamState();
void Enter(Object* agent);
void Execute(Object* agent);
void Exit(Object* agent);
};

class FleeState : public State
{

};

#endif
12 changes: 12 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _COMMON_H_
#define _COMMON_H_

// Include common headers
#include <SDL/SDL.h>
#include <cassert>
#include <vector>
#include <string>

using namespace std;

#endif
17 changes: 17 additions & 0 deletions log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "log.h"

void Log(...)
{
va_list arglist;
va_start(arglist, arg);

#if _DEBUG
cout << arg << ' ';
va_end(arglist);
cout << endl;

#else
MessageBox(NULL, arg, "Error", MB_ICON_ERROR);
va_end(arglist);
#endif
}
19 changes: 19 additions & 0 deletions log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _LOG_H_
#define _LOG_H_

#include <cstdarg>

using namespace std;

#define _DEBUG 1

#if _DEBUG
#include <iostream>
void Log(...);
#else
#define NDEBUG
#include <Windows.h>
void Log(...);
#endif

#endif

0 comments on commit 31976ad

Please sign in to comment.