# Betrayer_of_Code

Member

265

130 Neutral

• Rank
Member
1. ## Help with my GA

Thanks ill try that out now. I do not understand what you mean here "You have an off-by-one error regarding MAXPOP (you access newpop[MAXPOP] in the loop when you shouldn't" Even without that part I didn't understand it works, so far as I can tell 100% of the time now. Thank you a lot.
2. ## Help with my GA

I've been working on getting this program to work for quite a while. It is supposed to solve the problem "52 = a + 2b + 3c". Please help me to get it to work. It solves the problem sometimes, but other times it just loops the same number until the program ends or it crashes. I am almost positive it is something wrong with the "Choose_Mate()" function #include <iostream> #include <math.h> #include <fstream> #include <stdlib.h> #include <windows.h> using namespace std; #define MAXPOP 10 #define MAXALL 3 // 52 = a + 2b + 3c ofstream fout; void start_pop(), fitness(), probability(), choose_mate(), mutate(); class POPULATION { public: int FitSum; int ProbSum; int generation; }population; class INDIVIDUAL { public: int fitness; int probability; int allele[3]; int mother; int father; }individual[MAXPOP]; int main() { srand(GetTickCount()); fout.open("stats.txt"); start_pop(); cout << "Simulating Population\n"; while(1) { population.FitSum = 0; population.ProbSum = 0; population.generation++; fitness(); probability(); choose_mate(); mutate(); } return 0; } void fitness() { int sum = 0; if(population.generation > 1000) { system("pause"); exit(0); } for(int i = 0; i < MAXPOP; i++) { individual.fitness = 100 - (abs(52 - (individual.allele[0] + (2 * individual.allele[1]) + (3 * individual.allele[2])))); population.FitSum += individual.fitness; if(individual.fitness == 100) { fout << "+++++++++++++++++++++++++++\n"; fout << "+SOLVED:\n"; fout << "+A = " << individual.allele[0] << endl; fout << "+B = " << individual.allele[1] << endl; fout << "+C = " <<individual.allele[2] << endl; fout << "+Generation: " << population.generation; fout << "\n+++++++++++++++++++++++++++\n"; cout << "\nSolved\n"; system("pause"); exit(0); } } /*for(int i = 0; i < MAXPOP; i++) { if(individual.fitness > (population.FitSum / MAXPOP)) { individual.fitness += 20; } if(individual.fitness < (population.FitSum / MAXPOP)) { individual.fitness -= 20; } } */ return; } void probability() { for(int i = 0; i < (MAXPOP); ++i) { individual.probability = individual.fitness + population.ProbSum; population.ProbSum += individual.fitness; } return; } void choose_mate() { INDIVIDUAL newpop[MAXPOP]; int pop = 0; bool fmalleles[3]; while(pop < MAXPOP) { fmalleles[rand() % 3] = true; fmalleles[rand() % 3] = true; bool father = false; bool mother = false; int RandFather = (rand() % population.ProbSum); int RandMother = (rand() % population.ProbSum); for(int i = 0; i < (MAXPOP); i++) { if(RandFather > individual.probability && RandFather < individual[i + 1].probability || RandFather == individual.probability) { father = true; int trans = 0; while(trans < 3) { if(fmalleles[trans] == true) { newpop[pop].allele[trans] = individual.allele[trans]; } trans++; } } if(RandMother > individual.probability && RandMother < individual[i + 1].probability || RandMother == individual.probability) { mother = true; int trans = 0; while(trans < 3) { if(fmalleles[trans] == false) { newpop[pop].allele[trans] = individual.allele[trans]; } trans++; } } } if(mother == false || father == false) { pop -= 1; } pop++; } for(int i = 0; i < (MAXPOP); i++) { individual = newpop; } return; } void start_pop() { for(int i = 0; i < (MAXPOP); i++) { for(int j = 0; j < 3; j++) { individual.allele[j] = rand() % 10 + 1; } } return; } void mutate() { int pop = 0; while(pop < MAXPOP) { int allele = rand() % 3; int pm = rand() % 1; int mutate = rand() % 50; if(mutate == 15 || mutate == 21) { if(pm == 0) { individual[pop].allele[allele] += 1; } else { individual[pop].allele[allele] -= 1; } } pop++; } return; }
3. ## Wierd Output on My G.A. Code

I wrote a some code to solve the problem x = a + 2b + 3c, where I can change the value of x fairly easily. When I run the code (with x = to 52 currently) I get a fairly normal log, except for one thing...Right in the middle, EVERY time I get a string of "Average Fitness" that are the same. No mutations occur in this set of about 100 generations. After that and before that mutations occur normally? Is there a reason for this? here is my code. Yes i realize i shouldnt use namespaces. I think my computer gets board :D #include <iostream> #include <math.h> #include <fstream> #include <stdlib.h> #include <windows.h> using namespace std; #define MAXPOP 10 #define MAXALL 3 // 52 = a + 2b + 3c ofstream fout; void start_pop(), fitness(), probability(), choose_mate(), mutate(); class POPULATION { public: int FitSum; int ProbSum; int generation; }population; class INDIVIDUAL { public: int fitness; int probability; int allele[3]; int mother; int father; }individual[MAXPOP]; int main() { fout.open("stats.txt"); start_pop(); while(1) { population.FitSum = 0; population.ProbSum = 0; population.generation++; cout << population.generation << endl; fitness(); probability(); choose_mate(); mutate(); //system("pause"); } return 0; } void fitness() { if(population.generation > 500) { system("pause"); exit(0); } int sum = 0; for(int i = 0; i < MAXPOP; i++) { individual.fitness = 100 - (abs(52 - (individual.allele[0] + (2 * individual.allele[1]) + (3 * individual.allele[2])))); population.FitSum += individual.fitness; //fout << "Fitness: " << individual.fitness << endl; if(individual.fitness == 100) { //solve(i); fout << "\n\nSOLVED:\n"; fout << individual.allele[0] << endl; fout << individual.allele[1] << endl; fout << individual.allele[2] << endl; fout << "Generation: " << population.generation; fout << "\n+++++++++++++++++++++++++++\n"; cout << "\nSolved\n"; system("pause"); exit(0); } if(individual.fitness > (population.FitSum / MAXPOP)) { individual.fitness += 5; sum += 5; } if(individual.fitness < (population.FitSum / MAXPOP)) { individual.fitness -= 5; sum -= 5; } } population.FitSum += sum; fout << "Average Fitness: " << (population.FitSum / MAXPOP) << endl; return; } void probability() { for(int i = 0; i < (MAXPOP); i++) { individual.probability = individual.fitness + population.ProbSum; population.ProbSum += individual.fitness; } fout << "Probability: " << (population.ProbSum / MAXPOP) << endl; return; } void choose_mate() { srand(GetTickCount()); INDIVIDUAL newpop[MAXPOP]; int pop = 0; while(pop < MAXPOP) { bool fmalleles[3]; fmalleles[rand() % 3] = true; fmalleles[rand() % 3] = true; bool father = false; bool mother = false; int RandFather = (rand() % population.ProbSum); //cout << endl << RandFather; int RandMother = (rand() % population.ProbSum); //cout << endl << RandMother << endl; for(int i = 0; i < (MAXPOP); i++) { if(RandFather > individual.probability && RandFather < individual[i + 1].probability || RandFather == individual.probability) { //cout << "\nDad\n"; father = true; int trans = 0; while(trans < 3) { if(fmalleles[trans] == true) { newpop[pop].allele[trans] = individual.allele[trans]; } trans++; } } if(RandMother > individual.probability && RandMother < individual[i + 1].probability || RandMother == individual.probability) { //cout << "\nMom\n"; mother = true; int trans = 0; while(trans < 3) { if(fmalleles[trans] == false) { newpop[pop].allele[trans] = individual.allele[trans]; } trans++; } } } if(mother == false || father == false) { pop -= 1; } pop++; } for(int i = 0; i < (MAXPOP); i++) { individual = newpop; } return; } void start_pop() { srand(GetTickCount()); for(int i = 0; i < (MAXPOP); i++) { for(int j = 0; j < 3; j++) { individual.allele[j] = rand() % 10 + 1; } } return; } void mutate() { int pop = 0; while(pop < MAXPOP) { int allele = rand() % 3; int pm = rand() % 1; int mutate = rand() % 50; if(mutate == 2 || mutate == 21) { individual[pop].allele[allele] = rand() % 10; fout << "\nindividual[" << pop << "] mutated " << allele << " allele" << endl; } if(mutate == 5 || mutate == 15) { fout << "\nindividual[" << pop << "] mutated"; if(pm == 0) { individual[pop].allele[allele] += 1; } else { individual[pop].allele[allele] -= 1; } fout << endl; } pop++; } return; } [Edited by - Betrayer_of_Code on November 13, 2004 9:31:51 PM]
4. ## & Operator, Vector

typedef vector<ga_struct> ga_vector;// What does the above statement actually do? Guess: (Create ga_vector of type ga_struct?) (ga_vector &population, ga_vector &buffer) Also what does the & symbol before population and buffer do? I read somethings about pointers, which I understand and that symbol was there, but never explained. EDIT::Also what does the function abs(int, int) do? I think its in time.h or math.h
5. ## Setting up A* Pathfinding

Well, I have not really looked into priority queues or vectors yet so...I'll go look those up and get back to you :D
6. ## Setting up A* Pathfinding

Aye, I learned that later, after I posted. But HOW do you find out which node is the parent, is what I was tryign to ask. And would you store it as a pointer? and int? what?
7. ## Setting up A* Pathfinding

Ive been reading some of the stuff you ahve showed me and I have a less general question... "Look at all the reachable or walkable squares adjacent to the starting point, ignoring squares with walls, water, or other illegal terrain. Add them to the open list, too. For each of these squares, save point A as its “parent square”. This parent square stuff is important when we want to trace our path. It will be explained more later." struct open { int cost; open *parent; open *next; }; in some function: { current->parent = previous; } //I really dont think this will work, because each parent has several "children" so the node before it will not neccesarily be the parent... Is that how I would define the parent pointer? or would that just point to the next node like next does? If I cant define it like that how would I?

Thanks guys
9. ## Hillary Duff Is A Curse On The World

Allright, yes all of them suck at singing. Yes all of them are great with mute... "Then ease her into Nirvana (teenagers can't get enough of Nirvana)" You dont ease into Nirvana, your drop right in because its the best band ever... Oh, yeah and he didnt kill himself, I know I was there. I saw his wife with the gun...
10. ## WHY do you split files?

I understand that in many larger programs you would have to split the source into different source files (.cpp, .h). But what do you put in each. Do you only put specific data in .h's or .cpp's and what is the realtionship between two .cpp's in the same file? Thanks
11. ## Setting up A* Pathfinding

I have been looking up, and working on, psudocode to write a pathfinder using A*. A lot of the places I have looked tie linked lists into it. I would like to know how I would use a linked list to create an A* pathfinder. My steup: 1. Load the Maze 2. Place all the numbers for the maze into an array[30][30]; loop 3. A* stuff that I kind of understand... 4. If(if current->next->solution == true) 5a. Solved 5b.Top of Loop Could someone explain the concept of A* a little more and show how I would use a linked list in this? Thanks for your time.

Thanks guys.

If I wanted to compare the info in a node: if (current->f(n) < current->nxt->f(n)) would i have to create a new pointer *next and do next = current->next; if(current->f(n) < next->f(n)) Thanks
14. ## makeing a 20 side dice

use srand(GetTickCount()); at the beggining of your function, or the beggining of your program. Then whenever you use rand()% 20 + 1 you will get a number between 1 and 20.
15. ## Code Organization

Alright, here, ill put it in source boxes. /* Name: Maze Runner v2 Copyright: 2004 Author: Peter Bretton Date: 19/09/04 19:19 Description: This will run a 30 x 30 maze, and find the "cheese", it's sucsess rate os mpt 100% it is maybe 85% of the mazes that are possible. It is the prototype I am using to create "worlds" for my ASCII graphics game. */ #include "Move.h" #define MAP_WIDTH 30 #define MAP_HEIGHT 30 #define MAP_SQUARE 30 #define NORTH 1 #define SOUTH 2 #define EAST 3 #define WEST 4 //Simply a main menu function int main() { nk = 0; srand(GetTickCount()); int choice; cout << "(1)Random\n"; cout << "(2)Test\n"; cout << "(3)Exit\n"; cin >> choice; switch(choice) { case 1: //create a random 30 x 30 maze random_maze(); break; case 2: //load a pre-made 30 x 30 maze load_maze(); break; case 3: //exit exit(0); break; default: load_maze(); break; } return 0; } /*This function is a first pass at the maze if it has to backtrack or if it get's "stuck" then it goes to the next function "backtrack" */ void walk_maze(int pass_maze[][MAP_WIDTH]) { set_backtrack_flag = false; set_btII_flag = false; int col = 0; int row = 0; for(int r = 0; r < MAP_SQUARE; ++r) { for(int c = 0; c < MAP_SQUARE; ++c) { if(pass_maze[c][r] == 3) { col = c; row = r; } } } if(pass_maze[col + 1][row] == 2 || pass_maze[col - 1][row] == 2 || pass_maze[col][row + 1] == 2 || pass_maze[col][row - 1] == 2) { cout << "\nMaze Solved!!\n"; cout << "Total Moves: " << nk << endl; system("pause"); system("cls"); main(); } if(pass_maze[col][row + 1] == 0) { choose_direction(pass_maze, SOUTH); } else if(pass_maze[col + 1][row] == 0) { choose_direction(pass_maze, EAST); } else if(pass_maze[col][row - 1] == 0) { choose_direction(pass_maze, NORTH); } else if(pass_maze[col - 1][row] == 0) { choose_direction(pass_maze, WEST); } if(pass_maze[col + 1][row] != 0 && pass_maze[col - 1][row] != 0 || pass_maze[col][row + 1] != 0 || pass_maze[col][row - 1] != 0) { //Go to the next function, which will run the maze again in a different way backtrack(pass_maze); } } /*This will run the maze as though the path created by walk_maze() didnt exist */ void backtrack(int pass_maze[][MAP_WIDTH]) { set_backtrack_flag = true; set_btII_flag = false; int col = 0; int row = 0; //Find where the "mouse" is for(int r = 0; r < MAP_SQUARE; ++r) { for(int c = 0; c < MAP_SQUARE; ++c) { if(pass_maze[c][r] == 3) { col = c; row = r; } } } if(pass_maze[col + 1][row] == 2 || pass_maze[col - 1][row] == 2 || pass_maze[col][row + 1] == 2 || pass_maze[col][row - 1] == 2) { cout << "\nMaze Solved!!\n"; cout << "Total Moves: " << nk << endl; system("pause"); system("cls"); main(); } if(pass_maze[col][row - 1] == 0 || pass_maze[col][row + 1] == 0 || pass_maze[col - 1][row] == 0 || pass_maze[col + 1][row] == 0) { set_backtrack_flag = false; draw_maze(pass_maze); } ++nk; if(pass_maze[col][row + 1] == 4) { set_backtrack_flag = true; choose_direction(pass_maze, SOUTH); } else if(pass_maze[col + 1][row] == 4) { set_backtrack_flag = true; choose_direction(pass_maze, EAST); } else if(pass_maze[col][row - 1] == 4) { set_backtrack_flag = true; choose_direction(pass_maze, NORTH); } else if(pass_maze[col - 1][row] == 4) { set_backtrack_flag = true; choose_direction(pass_maze, WEST); } else if(pass_maze[col + 1][row] != 4 && pass_maze[col - 1][row] != 4 || pass_maze[col][row + 1] != 4 || pass_maze[col][row - 1] != 4) { backtrackII(pass_maze); } } //Another backtrack, just in case void backtrackII(int pass_maze[][MAP_WIDTH]) { set_btII_flag = true; set_backtrack_flag = false; int col = 0; int row = 0; for(int r = 0; r < MAP_SQUARE; ++r) { for(int c = 0; c < MAP_SQUARE; ++c) { if(pass_maze[c][r] == 3) { col = c; row = r; } } } if(pass_maze[col + 1][row] == 2 || pass_maze[col - 1][row] == 2 || pass_maze[col][row + 1] == 2 || pass_maze[col][row - 1] == 2) { cout << "\nMaze Solved!!\n"; cout << "Total Moves: " << nk << endl; system("pause"); system("cls"); main(); } if(pass_maze[col][row - 1] == 4 || pass_maze[col][row + 1] == 4 || pass_maze[col - 1][row] == 4 || pass_maze[col + 1][row] == 4) { set_btII_flag = false; set_backtrack_flag = true; backtrack(pass_maze); } if(pass_maze[col][row - 1] == 0 || pass_maze[col][row + 1] == 0 || pass_maze[col - 1][row] == 0 || pass_maze[col + 1][row] == 0) { set_btII_flag = false; set_backtrack_flag = false; walk_maze(pass_maze); } if(pass_maze[col + 1][row] == 5 || pass_maze[col + 1][row] == 6) { set_btII_flag = true; choose_direction(pass_maze, EAST); } else if(pass_maze[col][row + 1] == 5 || pass_maze[col][row + 1] == 6) { set_btII_flag = true; choose_direction(pass_maze, SOUTH); } else if(pass_maze[col - 1][row] == 5 || pass_maze[col - 1][row] == 5) { set_btII_flag = true; choose_direction(pass_maze, WEST); } else if(pass_maze[col][row - 1] == 5 || pass_maze[col][row - 1] == 6) { set_btII_flag = true; choose_direction(pass_maze, NORTH); } cout << "\nThis maze is insovlable\n"; system("pause"); system("cls"); main(); } //This function actually decides what direction to go in //It may be obsolete, I think I may be able to go right from //walking function to the actual moving but...Oh well void choose_direction(int pass_maze[][MAP_WIDTH], int direction) { if(nk >= 600) { cout << "\nExceeded 600 move limit\n"; nk = 0; system("pause"); system("cls"); main(); } switch(direction) { case 1: go.north(pass_maze); break; case 2: go.south(pass_maze); break; case 3: go.east(pass_maze); break; case 4: go.west(pass_maze); break; default: cout << "\n\nChoose Error\n\n"; system("pause"); exit(0); break; } } //Draw and Initilize the Maze// void draw_maze(int pass_maze[][MAP_SQUARE]) { system("cls"); char char_maze[MAP_HEIGHT][MAP_WIDTH]; int switch_char; int k = 0; for (int r = 0; r < MAP_SQUARE; r++) { for(int c = 0; c < MAP_SQUARE; c++) { switch_char = pass_maze[c][r]; switch(switch_char) { case 0: char_maze[c][r] = space; break; case 1: char_maze[c][r] = wall; break; case 2: char_maze[c][r] = cheese; break; case 3: char_maze[c][r] = mouse; break; case 4: char_maze[c][r] = walk; break; case 5: char_maze[c][r] = back; break; case 6: char_maze[c][r] = backII; break; default: char_maze[c][r] = 'H'; break; } } } for (int r = 0; r < MAP_SQUARE; ++r) { for(int c = 0; c < MAP_SQUARE; ++c) { k++; cout << char_maze[c][r]; if ( k == MAP_SQUARE ) { cout << "\n"; k = 0; } } } walk_maze(pass_maze); } //Creates a 30x30 maze, with walls and a cheese //Also exports the created maze to "maze.txt" //For later use void random_maze() { fout.open("maze.txt"); int maze[MAP_HEIGHT][MAP_WIDTH]; for (int r = 0; r < MAP_SQUARE; ++r) { for(int c = 0; c < MAP_SQUARE; ++c) { maze[c][r] = (rand()% 3); if(maze[c][r] == 2) { maze[c][r] = 0; } if(c == 0 || c == 29) { maze[c][r] = 1; } if(r == 0 || r == 29) { maze[c][r] = 1; } if(r == 1 && c == 1) { maze[c][r] = 3; } if(r == 28 && c == 28) { maze[c][r] = 2; } fout << maze[c][r]; fout << "\n"; } } fout.close(); draw_maze(maze); } //Loads a 30 x 30 maze from "maze.txt" void load_maze() { ifstream fin; int maze[MAP_HEIGHT][MAP_WIDTH]; fin.open("maze.txt"); if(fin.fail()) { cout << "\nError\n"; } for(int r = 0; r < MAP_SQUARE; ++r) { for(int c = 0; c < MAP_SQUARE; ++c) { fin >> maze[c][r]; } } fin.close(); draw_maze(maze); return; } Move.h #include <iostream> #include <stdlib.h> #include <string> #include <fstream> #include <windows.h> #define MAP_WIDTH 30 #define MAP_HEIGHT 30 #define MAP_SQUARE 30 #define NORTH 1 #define SOUTH 2 #define EAST 3 #define WEST 4 using namespace std; int nk = 0; fstream fin; fstream fout; const char mouse = 'M'; const char cheese = 'C'; const char space = ' '; const char wall = '#'; const char walk = '+'; const char back = 'X'; const char backII = '!'; void draw_maze(int pass_maze[][MAP_WIDTH]), load_maze(), walk_maze(int pass_maze[][MAP_WIDTH]), backtrack(int pass_maze[][MAP_WIDTH]), backtrackII(int pass_maze[][MAP_WIDTH]), choose_direction(int pass_maze[][MAP_WIDTH], int direction); void random_maze(); bool set_backtrack_flag = false; bool set_btII_flag = false; class GO { public: //All of these actually move the "mouse" and then starts over at walk_maze(); void north(int pass_maze[][MAP_WIDTH]); void south(int pass_maze[][MAP_WIDTH]); void east(int pass_maze[][MAP_WIDTH]); void west(int pass_maze[][MAP_WIDTH]); }go; void GO::north(int pass_maze[][MAP_WIDTH]) { int col = 0; int row = 0; for(int r = 0; r < MAP_SQUARE; ++r) { for(int c = 0; c < MAP_SQUARE; ++c) { if(pass_maze[c][r] == 3) { col = c; row = r; } } } ++nk; if(set_btII_flag == true) { pass_maze[col][row] = 6; pass_maze[col][row - 1] = 3; draw_maze(pass_maze); } else if(set_backtrack_flag == true) { pass_maze[col][row] = 5; pass_maze[col][row - 1] = 3; draw_maze(pass_maze); } else if(set_backtrack_flag == false && set_btII_flag == false) { pass_maze[col][row] = 4; pass_maze[col][row - 1] = 3; draw_maze(pass_maze); } else { cout <<"\nn\n"; system("pause"); } } void GO::south(int pass_maze[][MAP_WIDTH]) { int col = 0; int row = 0; for(int r = 0; r < MAP_SQUARE; ++r) { for(int c = 0; c < MAP_SQUARE; ++c) { if(pass_maze[c][r] == 3) { col = c; row = r; } } } ++nk; if(set_btII_flag == true) { pass_maze[col][row] = 6; pass_maze[col][row + 1] = 3; draw_maze(pass_maze); } else if(set_backtrack_flag == true) { pass_maze[col][row] = 5; pass_maze[col][row + 1] = 3; draw_maze(pass_maze); } else if(set_backtrack_flag == false && set_btII_flag == false) { pass_maze[col][row] = 4; pass_maze[col][row + 1] = 3; draw_maze(pass_maze); } else { cout <<"\ns\n"; system("pause"); } } void GO::east(int pass_maze[][MAP_WIDTH]) { int col = 0; int row = 0; for(int r = 0; r < MAP_SQUARE; ++r) { for(int c = 0; c < MAP_SQUARE; ++c) { if(pass_maze[c][r] == 3) { col = c; row = r; } } } ++nk; if(set_btII_flag == true) { pass_maze[col][row] = 6; pass_maze[col + 1][row ] = 3; draw_maze(pass_maze); } else if(set_backtrack_flag == true) { pass_maze[col][row] = 5; pass_maze[col + 1][row ] = 3; draw_maze(pass_maze); } else if(set_backtrack_flag == false && set_btII_flag == false) { pass_maze[col][row] = 4; pass_maze[col + 1][row] = 3; draw_maze(pass_maze); } else { cout <<"\ne\n"; system("pause"); } } void GO::west(int pass_maze[][MAP_WIDTH]) { int col = 0; int row = 0; for(int r = 0; r < MAP_SQUARE; ++r) { for(int c = 0; c < MAP_SQUARE; ++c) { if(pass_maze[c][r] == 3) { col = c; row = r; } } } ++nk; if(set_btII_flag == true) { pass_maze[col][row] = 6; pass_maze[col - 1][row] = 3; draw_maze(pass_maze); } else if(set_backtrack_flag == true) { pass_maze[col][row] = 5; pass_maze[col - 1][row] = 3; draw_maze(pass_maze); } else if(set_backtrack_flag == false && set_btII_flag == false) { pass_maze[col][row] = 4; pass_maze[col - 1][row] = 3; draw_maze(pass_maze); } else { cout <<"\nw\n"; system("pause"); } } Thanks