Skip to content

Commit

Permalink
cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknown committed Nov 18, 2016
1 parent c7a4ae5 commit 7065755
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 89 deletions.
5 changes: 0 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
CC=g++
CFLAGS=-c -Wall -std=c++11
LDFLAGS=
SOURCES=main.cpp grid.cpp RNG.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=schelling.out


all: $(SOURCES) $(EXECUTABLE)

debug: CFLAGS += -g
debug: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
Expand Down
1 change: 0 additions & 1 deletion RNG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ RNG::RNG(int seed) {
}
void RNG::setseed(int newSeed){
seed = newSeed;
gen.seed(seed);
re.seed(seed);
}
void RNG::printseed() {
Expand Down
1 change: 0 additions & 1 deletion RNG.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class RNG {

private:
int seed;
std::mt19937 gen;
std::mt19937 re {};
using distUnifInt = std::uniform_int_distribution<int>;
using distUnifDbl = std::uniform_real_distribution<double>;
Expand Down
116 changes: 58 additions & 58 deletions grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,91 +12,91 @@ Grid::~Grid() {

void Grid::initiate(unsigned int gridSize){
this->gridSize = gridSize;
std::cout << "Creating a grid of size " << gridSize << "x" << gridSize << "\n";
std::cout << "Creating a grid of size " << gridSize << "x" << gridSize << "\n";

// 1. create the grid
GridEl* lastEl;
GridEl* el;
GridEl* upperEl = nullptr;
unsigned int row = 0, col = 0;
// 1. create the grid
GridEl* lastEl;
GridEl* el;
GridEl* upperEl = nullptr;
unsigned int row = 0, col = 0;

// first iteration
el = new GridEl(row, col, 0, nullptr, nullptr, nullptr, nullptr);
head = el;
lastEl = el;
++col;

while (col < gridSize) {
// iterate left to right and link vertically
el = new GridEl(row, col, 0, nullptr, nullptr, nullptr, nullptr);
el->W = lastEl;
lastEl->E = el;
lastEl = el;
++col;
}
upperEl = lastEl;
++row;
// first iteration
el = new GridEl(row, col, 0, nullptr, nullptr, nullptr, nullptr);
head = el;
lastEl = el;
++col;

while (col < gridSize) {
// iterate left to right and link vertically
el = new GridEl(row, col, 0, nullptr, nullptr, nullptr, nullptr);
el->W = lastEl;
lastEl->E = el;
lastEl = el;
++col;
}
upperEl = lastEl;
++row;

// loop the body
while(row < gridSize) {
col = 0;
if (row % 2 != 0) {
while(row < gridSize) {
col = 0;
if (row % 2 != 0) {
el = new GridEl(row, gridSize - col - 1, 0, nullptr, nullptr, nullptr, nullptr);

// even row (starts at 0...), iterate right to left
el->E = nullptr; // right is nothing
el->N = upperEl;
el->E = nullptr; // right is nothing
el->N = upperEl;
upperEl->S = el;
lastEl = el;
upperEl = upperEl->W; // move to the left
lastEl = el;
upperEl = upperEl->W; // move to the left
++col;

while (col < gridSize) {
el = new GridEl(row, gridSize - col - 1, 0, nullptr, nullptr, nullptr, nullptr);
el->E = lastEl;
lastEl->W = el;
el->N = upperEl;
upperEl->S = el;
upperEl = upperEl->W;
while (col < gridSize) {
el = new GridEl(row, gridSize - col - 1, 0, nullptr, nullptr, nullptr, nullptr);
el->E = lastEl;
lastEl->W = el;
el->N = upperEl;
upperEl->S = el;
upperEl = upperEl->W;
lastEl = el;
++col;
}
} else {
++col;
}
} else {
el = new GridEl(row, col, 0, nullptr, nullptr, nullptr, nullptr);

// uneven rows (starts at 0...), iterate left to right
el->W = nullptr; // left is nothing
el->N = upperEl;
el->W = nullptr; // left is nothing
el->N = upperEl;
upperEl->S = el;
lastEl = el;
upperEl = upperEl->E;
lastEl = el;
upperEl = upperEl->E;
col++;

while (col < gridSize) {
el = new GridEl(row, col, 0, nullptr, nullptr, nullptr, nullptr);
el->W = lastEl;
lastEl->E = el;
el->N = upperEl;
upperEl->S = el;
upperEl = upperEl->E;
while (col < gridSize) {
el = new GridEl(row, col, 0, nullptr, nullptr, nullptr, nullptr);
el->W = lastEl;
lastEl->E = el;
el->N = upperEl;
upperEl->S = el;
upperEl = upperEl->E;
lastEl = el;
++col;
}
}
++row;
++col;
}
}
++row;
upperEl = el;
}
}
}

void Grid::countElements(){
unsigned int n = 0;
unsigned int n = 0;
GridEl* el = head;

while (el != nullptr) {
el = next(el);
++n;
}
std::cout << "Total number of elements: " << n << "\n";
std::cout << "Total number of elements: " << n << "\n";
}

Grid::GridEl* Grid::next(Grid::GridEl* reference) {
Expand Down Expand Up @@ -314,6 +314,6 @@ void Grid::simulate(unsigned int nMax, int plotAfter, int sleep) {
}

std::cout << "\033[2J\033[1;1H";
std::cout << "------ Grid after " << nSim - 1 << " Iterations, with " << moves << " moves. -------------------\n";
std::cout << "------ Grid after " << nSim << " Iterations, with " << moves << " moves. -------------------\n";
drawPlot();
}
46 changes: 23 additions & 23 deletions grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@

class Grid {
public:
Grid(double threshold = 0.5, int seed = (int) time(0));
~Grid();
struct GridEl {
unsigned int row;
unsigned int col;
GridEl* N;
GridEl* E;
GridEl* S;
GridEl* W;
unsigned int race;
bool happy, movable;
GridEl(unsigned int r, unsigned int c, unsigned int racevalue, GridEl* former, GridEl* next, GridEl* lower, GridEl* upper) :
row(r), col(c), race(racevalue), W(former), S(lower), N(upper), E(next), happy(false), movable(true) {}
};
Grid(double threshold = 0.5, int seed = (int) time(0));
~Grid();
struct GridEl {
unsigned int row;
unsigned int col;
GridEl* N;
GridEl* E;
GridEl* S;
GridEl* W;
unsigned int race;
bool happy, movable;
GridEl(unsigned int r, unsigned int c, unsigned int racevalue, GridEl* former, GridEl* next, GridEl* lower, GridEl* upper) :
row(r), col(c), race(racevalue), W(former), S(lower), N(upper), E(next), happy(false), movable(true) {}
};

void initiate(unsigned int gridSize);
void populate(double percentEmpty, unsigned int nRaces);
void initiate(unsigned int gridSize);
void populate(double percentEmpty, unsigned int nRaces);
void drawPlot();
void simulate(unsigned int nMax, int plotAfter, int sleep = 10000);
void updateHappiness();
void move();
void countElements();
GridEl* next(GridEl* reference);
void simulate(unsigned int nMax, int plotAfter, int sleep = 10000);
void updateHappiness();
void move();
void countElements();
GridEl* next(GridEl* reference);

private:
double happinessThreshold;
unsigned int n, gridSize, nRaces;
GridEl* head;
unsigned int n, gridSize, nRaces;
GridEl* head;
bool allHappy;
int moves;
std::vector<GridEl*> emptyPlaces;
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static void show_usage()
<< " -s,--seed\t\tSet seed, defaults to current time\n"
<< " -g,--gridSize\t\tSpecify the length of a simulation grid, defaults to 10 (i.e., 10x10)\n"
<< " -p,--percEmpty\tSpecify the percentage of empty grids, defaults to 0.2\n"
<< " -t,--threshold\tSpecify the threshold above which agents are happy, defaults to 0.5\n"
<< " -t,--threhhold\tSpecify the threshold above which agents are happy, defaults to 0.5\n"
<< " -n,--nRaces\t\tSpecify the number of races [2,26], defaults to 3\n"
<< " -m,--maxIter\t\tSpecify the maximum number of iterations, defaults to 10,000\n"
<< " -pi,--plotInterval\tSpecify the intervals in which the plots should be updated, defaults to 100\n"
Expand Down

0 comments on commit 7065755

Please sign in to comment.