From 31976ad99c6276f64fe4191d11c1bebbd4827378 Mon Sep 17 00:00:00 2001 From: KVP_ Date: Tue, 7 Oct 2014 10:18:23 -0400 Subject: [PATCH] Initial commit --- Agent.cpp | 42 +++++++++++++++++++++++++++ Agent.h | 51 +++++++++++++++++++++++++++++++++ New Text Document.txt | 6 ++++ Object.cpp | 31 ++++++++++++++++++++ Params.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++ Params.h | 62 ++++++++++++++++++++++++++++++++++++++++ State.cpp | 28 ++++++++++++++++++ State.h | 31 ++++++++++++++++++++ common.h | 12 ++++++++ log.cpp | 17 +++++++++++ log.h | 19 +++++++++++++ 11 files changed, 365 insertions(+) create mode 100644 Agent.cpp create mode 100644 Agent.h create mode 100644 New Text Document.txt create mode 100644 Object.cpp create mode 100644 Params.cpp create mode 100644 Params.h create mode 100644 State.cpp create mode 100644 State.h create mode 100644 common.h create mode 100644 log.cpp create mode 100644 log.h diff --git a/Agent.cpp b/Agent.cpp new file mode 100644 index 0000000..a3eb354 --- /dev/null +++ b/Agent.cpp @@ -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); +} \ No newline at end of file diff --git a/Agent.h b/Agent.h new file mode 100644 index 0000000..1fb8b30 --- /dev/null +++ b/Agent.h @@ -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 GetSurrounding(float radius); + vector 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 \ No newline at end of file diff --git a/New Text Document.txt b/New Text Document.txt new file mode 100644 index 0000000..87920c1 --- /dev/null +++ b/New Text Document.txt @@ -0,0 +1,6 @@ +Artificial +Self +Teaching +Roaming +Intelligent +Agent \ No newline at end of file diff --git a/Object.cpp b/Object.cpp new file mode 100644 index 0000000..4ab5d51 --- /dev/null +++ b/Object.cpp @@ -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 \ No newline at end of file diff --git a/Params.cpp b/Params.cpp new file mode 100644 index 0000000..f43ad08 --- /dev/null +++ b/Params.cpp @@ -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) +{ + +} \ No newline at end of file diff --git a/Params.h b/Params.h new file mode 100644 index 0000000..88f5b18 --- /dev/null +++ b/Params.h @@ -0,0 +1,62 @@ +#ifndef _PARAM_H_ +#define _PARAM_H_ + +#include +#include + +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 \ No newline at end of file diff --git a/State.cpp b/State.cpp new file mode 100644 index 0000000..7f3d48c --- /dev/null +++ b/State.cpp @@ -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*) +{ + +} \ No newline at end of file diff --git a/State.h b/State.h new file mode 100644 index 0000000..dcbb520 --- /dev/null +++ b/State.h @@ -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 \ No newline at end of file diff --git a/common.h b/common.h new file mode 100644 index 0000000..c9f9c0d --- /dev/null +++ b/common.h @@ -0,0 +1,12 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +// Include common headers +#include +#include +#include +#include + +using namespace std; + +#endif \ No newline at end of file diff --git a/log.cpp b/log.cpp new file mode 100644 index 0000000..20d7a8e --- /dev/null +++ b/log.cpp @@ -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 +} \ No newline at end of file diff --git a/log.h b/log.h new file mode 100644 index 0000000..fc699fd --- /dev/null +++ b/log.h @@ -0,0 +1,19 @@ +#ifndef _LOG_H_ +#define _LOG_H_ + +#include + +using namespace std; + + #define _DEBUG 1 + + #if _DEBUG + #include + void Log(...); + #else + #define NDEBUG + #include + void Log(...); + #endif + +#endif \ No newline at end of file