How to back propagate a Neural Network in C++? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have been trying to make a neural network in c++ and my back propagation code is not working the way I want it to. I have a text document that tells the network how to function. I have it have 2 input neurons, 1 hidden layer with 4 neurons and 2 output neurons. I have it learning to be an XOR gate right now. I have it so it takes the cost of the network, multiplys it by .55(scaling) and adding/subtracting that from the weights/bias' depending on how close the output is from the correct answer and weither or not the weights/bias' are + or -. Here's the code:
void Network::backProp(void)
{
double b = 0,a;
int loop,l;
for(loop=0;loop<4;loop++)
{
//Adds up the cost of the data
b = b + (pow(results[2*loop]-key[4*loop+2],2)+pow(results[2*loop+1]-key[4*loop+3],2));
}
a=.55*b;
if(b>.01)
{
for(l=0;l<4;l++)
{
if(round(results[2*l])!=key[4*l+2])
{
if(data[0] <= 0)
{
data[0] = data[0]+a; //(abs(data[0])/a);
}
else
{
data[0] = data[0]-a; //(abs(data[0])/a);
}
if(data[1] <= 0)
{
data[1] = data[1]+a; //(abs(data[1])/a);
}
else
{
data[1] = data[1]-a; //(abs(data[1])/a);
}
if(data[2] <= 0)
{
data[2] = data[2]+a; //(abs(data[2])/a);
}
else
{
data[2] = data[2]-a; //(abs(data[2])/a);
}
if(data[3] <= 0)
{
data[3] = data[3]+a; //(abs(data[3])/a);
}
else
{
data[3] = data[3]-a; //(abs(data[3])/a);
}
if(data[4] <= 0)
{
data[4] = data[4]+a; //(abs(data[4])/a);
}
else
{
data[4] = data[4]-a; //(abs(data[4])/a);
}
if(data[6] <= 0)
{
data[6] = data[6]+a; //(abs(data[6])/a);
}
else
{
data[6] = data[6]-a; //(abs(data[6])/a);
}
if(data[7] <= 0)
{
data[7] = data[7]+a; //(abs(data[7])/a);
}
else
{
data[7] = data[7]-a; //(abs(data[7])/a);
}
if(data[8] <= 0)
{
data[8] = data[8]+a; //(abs(data[8])/a);
}
else
{
data[8] = data[8]-a; //(abs(data[8])/a);
}
if(data[9] <= 0)
{
data[9] = data[9]+a; //(abs(data[9])/a);
}
else
{
data[9] = data[9]-a; //(abs(data[9])/a);
}
if(data[10] <= 0)
{
data[10] = data[10]+a; //(abs(data[10])/a);
}
else
{
data[10] = data[10]-a; //(abs(data[10])/a);
}
if(data[11] <= 0)
{
data[11] = data[11]+a; //(abs(data[11])/a);
}
else
{
data[11] = data[11]-a; //(abs(data[11])/a);
}
if(data[12] <= 0)
{
data[12] = data[12]+a; //(abs(data[12])/a);
}
else
{
data[12] = data[12]-a; //(abs(data[12])/a);
}
if(data[13] <= 0)
{
data[13] = data[13]+a; //(abs(data[13])/a);
}
else
{
data[13] = data[13]-a; //(abs(data[13])/a);
}
if(data[14] <= 0)
{
data[14] = data[14]+a; //(abs(data[14])/a);
}
else
{
data[14] = data[14]-a; //(abs(data[14])/a);
}
if(data[16] <= 0)
{
data[16] = data[16]+a; //(abs(data[16])/a);
}
else
{
data[16] = data[16]-a; //(abs(data[16])/a);
}
if(data[18] <= 0)
{
data[18] = data[18]+a; //(abs(data[18])/a);
}
else
{
data[18] = data[18]-a; //(abs(data[18])/a);
}
if(data[20] <= 0)
{
data[20] = data[20]+a; //(abs(data[20])/a);
}
else
{
data[20] = data[20]-a; //(abs(data[20])/a);
}
}
else
{
if(data[0] <= 0)
{
data[0] = data[0]-a; //(abs(data[0])/a);
}
else
{
data[0] = data[0]+a; //(abs(data[0])/a);
}
if(data[1] <= 0)
{
data[1] = data[1]-a; //(abs(data[1])/a);
}
else
{
data[1] = data[1]+a; //(abs(data[1])/a);
}
if(data[2] <= 0)
{
data[2] = data[2]-a; //(abs(data[2])/a);
}
else
{
data[2] = data[2]+a; //(abs(data[2])/a);
}
if(data[3] <= 0)
{
data[3] = data[3]-a; //(abs(data[3])/a);
}
else
{
data[3] = data[3]+a; //(abs(data[3])/a);
}
if(data[4] <= 0)
{
data[4] = data[4]-a; //(abs(data[4])/a);
}
else
{
data[4] = data[4]+a; //(abs(data[4])/a);
}
if(data[6] <= 0)
{
data[6] = data[6]-a; //(abs(data[6])/a);
}
else
{
data[6] = data[6]+a; //(abs(data[6])/a);
}
if(data[7] <= 0)
{
data[7] = data[7]-a; //(abs(data[7])/a);
}
else
{
data[7] = data[7]+a; //(abs(data[7])/a);
}
if(data[8] <= 0)
{
data[8] = data[8]-a; //(abs(data[8])/a);
}
else
{
data[8] = data[8]+a; //(abs(data[8])/a);
}
if(data[9] <= 0)
{
data[9] = data[9]-a; //(abs(data[9])/a);
}
else
{
data[9] = data[9]+a; //(abs(data[9])/a);
}
if(data[10] <= 0)
{
data[10] = data[10]-a; //(abs(data[10])/a);
}
else
{
data[10] = data[10]+a; //(abs(data[10])/a);
}
if(data[11] <= 0)
{
data[11] = data[11]-a; //(abs(data[11])/a);
}
else
{
data[11] = data[11]+a; //(abs(data[11])/a);
}
if(data[12] <= 0)
{
data[12] = data[12]-a; //(abs(data[12])/a);
}
else
{
data[12] = data[12]+a; //(abs(data[12])/a);
}
if(data[13] <= 0)
{
data[13] = data[13]-a; //(abs(data[13])/a);
}
else
{
data[13] = data[13]+a; //(abs(data[13])/a);
}
if(data[14] <= 0)
{
data[14] = data[14]-a; //(abs(data[14])/a);
}
else
{
data[14] = data[14]+a; //(abs(data[14])/a);
}
if(data[16] <= 0)
{
data[16] = data[16]-a; //(abs(data[16])/a);
}
else
{
data[16] = data[16]+a; //(abs(data[16])/a);
}
if(data[18] <= 0)
{
data[18] = data[18]-a; //(abs(data[18])/a);
}
else
{
data[18] = data[18]+a; //(abs(data[18])/a);
}
if(data[20] <= 0)
{
data[20] = data[20]-a; //(abs(data[20])/a);
}
else
{
data[20] = data[20]+a; //(abs(data[20])/a);
}
}
if(round(results[2*l+1])!=key[4*l+3])
{
if(data[0] <= 0)
{
data[0] = data[0]+a; //(abs(data[0])/a);
}
else
{
data[0] = data[0]-a; //(abs(data[0])/a);
}
if(data[1] <= 0)
{
data[1] = data[1]+a; //(abs(data[1])/a);
}
else
{
data[1] = data[1]-a; //(abs(data[1])/a);
}
if(data[2] <= 0)
{
data[2] = data[2]+a; //(abs(data[2])/a);
}
else
{
data[2] = data[2]-a; //(abs(data[2])/a);
}
if(data[3] <= 0)
{
data[3] = data[3]+a; //(abs(data[3])/a);
}
else
{
data[3] = data[3]-a; //(abs(data[3])/a);
}
if(data[4] <= 0)
{
data[4] = data[4]+a; //(abs(data[4])/a);
}
else
{
data[4] = data[4]-a; //(abs(data[4])/a);
}
if(data[5] <= 0)
{
data[5] = data[5]+a; //(abs(data[5])/a);
}
else
{
data[5] = data[5]-a; //(abs(data[5])/a);
}
if(data[7] <= 0)
{
data[7] = data[7]+a; //(abs(data[7])/a);
}
else
{
data[7] = data[7]-a; //(abs(data[7])/a);
}
if(data[8] <= 0)
{
data[8] = data[8]+a; //(abs(data[8])/a);
}
else
{
data[8] = data[8]-a; //(abs(data[8])/a);
}
if(data[9] <= 0)
{
data[9] = data[9]+a; //(abs(data[9])/a);
}
else
{
data[9] = data[9]-a; //(abs(data[9])/a);
}
if(data[10] <= 0)
{
data[10] = data[10]+a; //(abs(data[10])/a);
}
else
{
data[10] = data[10]-a; //(abs(data[10])/a);
}
if(data[11] <= 0)
{
data[11] = data[11]+a; //(abs(data[11])/a);
}
else
{
data[11] = data[11]-a; //(abs(data[11])/a);
}
if(data[12] <= 0)
{
data[12] = data[12]+a; //(abs(data[12])/a);
}
else
{
data[12] = data[12]-a; //(abs(data[12])/a);
}
if(data[13] <= 0)
{
data[13] = data[13]+a; //(abs(data[13])/a);
}
else
{
data[13] = data[13]-a; //(abs(data[13])/a);
}
if(data[15] <= 0)
{
data[15] = data[15]+a; //(abs(data[15])/a);
}
else
{
data[15] = data[15]-a; //(abs(data[15])/a);
}
if(data[17] <= 0)
{
data[17] = data[17]+a; //(abs(data[17])/a);
}
else
{
data[17] = data[17]-a; //(abs(data[17])/a);
}
if(data[19] <= 0)
{
data[19] = data[19]+a; //(abs(data[19])/a);
}
else
{
data[19] = data[19]-a; //(abs(data[19])/a);
}
if(data[21] <= 0)
{
data[21] = data[21]+a; //(abs(data[21])/a);
}
else
{
data[21] = data[21]-a; //(abs(data[21])/a);
}
}
else
{
if(data[0] <= 0)
{
data[0] = data[0]-a; //(abs(data[0])/a);
}
else
{
data[0] = data[0]+a; //(abs(data[0])/a);
}
if(data[1] <= 0)
{
data[1] = data[1]-a; //(abs(data[1])/a);
}
else
{
data[1] = data[1]+a; //(abs(data[1])/a);
}
if(data[2] <= 0)
{
data[2] = data[2]-a; //(abs(data[2])/a);
}
else
{
data[2] = data[2]+a; //(abs(data[2])/a);
}
if(data[3] <= 0)
{
data[3] = data[3]-a; //(abs(data[3])/a);
}
else
{
data[3] = data[3]+a; //(abs(data[3])/a);
}
if(data[4] <= 0)
{
data[4] = data[4]-a; //(abs(data[4])/a);
}
else
{
data[4] = data[4]+a; //(abs(data[4])/a);
}
if(data[5] <= 0)
{
data[5] = data[5]-a; //(abs(data[5])/a);
}
else
{
data[5] = data[5]+a; //(abs(data[5])/a);
}
if(data[7] <= 0)
{
data[7] = data[7]-a; //(abs(data[7])/a);
}
else
{
data[7] = data[7]+a; //(abs(data[7])/a);
}
if(data[8] <= 0)
{
data[8] = data[8]-a; //(abs(data[8])/a);
}
else
{
data[8] = data[8]+a; //(abs(data[8])/a);
}
if(data[9] <= 0)
{
data[9] = data[9]-a; //(abs(data[9])/a);
}
else
{
data[9] = data[9]+a; //(abs(data[9])/a);
}
if(data[10] <= 0)
{
data[10] = data[10]-a; //(abs(data[10])/a);
}
else
{
data[10] = data[10]+a; //(abs(data[10])/a);
}
if(data[11] <= 0)
{
data[11] = data[11]-a; //(abs(data[11])/a);
}
else
{
data[11] = data[11]+a; //(abs(data[11])/a);
}
if(data[12] <= 0)
{
data[12] = data[12]-a; //(abs(data[12])/a);
}
else
{
data[12] = data[12]+a; //(abs(data[12])/a);
}
if(data[13] <= 0)
{
data[13] = data[13]-a; //(abs(data[13])/a);
}
else
{
data[13] = data[13]+a; //(abs(data[13])/a);
}
if(data[15] <= 0)
{
data[15] = data[15]-a; //(abs(data[15])/a);
}
else
{
data[15] = data[15]+a; //(abs(data[15])/a);
}
if(data[17] <= 0)
{
data[17] = data[17]-a; //(abs(data[17])/a);
}
else
{
data[17] = data[17]+a; //(abs(data[17])/a);
}
if(data[19] <= 0)
{
data[19] = data[19]-a; //(abs(data[19])/a);
}
else
{
data[19] = data[19]+a; //(abs(data[19])/a);
}
if(data[21] <= 0)
{
data[21] = data[21]-a; //(abs(data[21])/a);
}
else
{
data[21] = data[21]+a; //(abs(data[21])/a);
}
}
}
}
}
I know it's a mess but this is what I came up with. I can post the rest of the code if that would help.

Here is a simplified version of your code
void Network::backProp(void)
{
double b = 0,a;
int loop,l;
int inclusion1 [] = {0,1,2,3,4,6,7,8,9,10,11,12,13,14,16,18,20};
int inclusion2 [] = {0,1,2,3,4,5,7,8,9,10,11,12,13,15,17,19,21};
int j = 0;
for(loop=0;loop<4;loop++)
{
//Adds up the cost of the data
b = b + (pow(results[2*loop]-key[4*loop+2],2)+pow(results[2*loop+1]-key[4*loop+3],2));
}
a=.55*b;
if(b>.01)
{
for(l=0;l<4;l++)
{
for(j=0;j<17;j++)
{
if(round(results[2*l])!=key[4*l+2])
{
data[inclusion1[j]] = data[inclusion1[j]] - abs(data[inclusion1[j]])/data[inclusion1[j]]*a;
}
if(round(results[2*l+1])!=key[4*l+3])
{
data[inclusion2[j]] = data[inclusion2[j]] + abs(data[inclusion2[j]])/data[inclusion2[j]]*a;
}
}
}
}
}
The basic issue as I see is it that your correction variable b i dont think it is defined accurately
Should be more along the lines
b = b + pow((pow(results[2*loop]-key[4*loop+2],2)+pow(results[2*loop+1]-key[4*loop+3],2)),1/2);

move this block to seperate function:
if(data[0] <= 0)
{
data[0] = data[0]+a; //(abs(data[0])/a);
}
else
{
data[0] = data[0]-a; //(abs(data[0])/a);
}
like this: (find proper name)
void AddAtoData(int& data, a)
{
if(data <= 0)
{
data += a;
}
else
{
data -= a;
}
}
then split your data[] structure to logical units, e.g. your layers to avoid the other if-logic and use loops.
once you cleaned up, look if your problem still exists, if so, come back.

Related

Tic tac toe Minimax Algorithm Having Weird Behavior (C++)

The other day, I wrote a console game of Tic-Tac-Toe in c++ for my son. He wanted me to add a computer, and I ended us using the minimax algorithm for the first time. I did some quick testing, but really just gave my laptop to my son as soon as it was printing stuff, who played with it for a couple minuets. I looked over his sholder once or twice, and noticed that it wasn't playing optimally, iv'e been trying to debug it, but I can't see where it goes wrong. I tried getting rid of alpha beta prunning, but that did not change anything.
For context, on the board the computer is -1, blank is 0, and the player is 1.
Here is the minimax function:
int minimax(int board[9], int depth, int alpha, int beta, bool isMaxizimaizingPlayer)
{
bool found = false;
for (int i = 0; i < 9; i++)
{
if (board[i] == 0)
{
found = true;
}
}
if (!found)
{
return eval(board);
}
if (depth == 0 || eval(board) != 0)
{
return eval(board);
}
if (isMaxizimaizingPlayer)
{
int maxEval = -2;
for (int spot = 0; spot < 9; spot++)
{
if (board[spot] == 0)
{
board[spot] = 1;
int e = minimax(board, depth - 1, alpha, beta, false);
if (e > maxEval)
{
maxEval = e;
}
//if (beta < alpha)
//{
// break;
//}
board[spot] = 0;
}
}
return maxEval;
}
else {
int minEval = 2;
for (int spot = 0; spot < 9; spot++)
{
if (board[spot] == 0)
{
board[spot] = -1;
int e = minimax(board, depth - 1, alpha, beta, true);
if (e < minEval)
{
minEval = e;
}
//if (beta < alpha)
//{
// break;
//}
board[spot] = 0;
}
}
return minEval;
}
}
To be compleate, here is my eval function:
int eval(int board[9])
{
/*horizontial*/
for (int i = 0; i < 3; i++)
{
if (board[i * 3] == board[i * 3 + 1] && board[i * 3 + 2] == board[i * 3] && board[i * 3] != 0)
{
return board[i * 3];
}
}
/*vertical*/
for (int i = 0; i < 3; i++)
{
if (board[i] == board[i + 3] && board[i] == board[i + 6] && board[i] != 0)
{
return board[i];
}
}
/*Both diags*/
if (board[4] != 0) {
if (board[0] == board[4] && board[0] == board[8])
{
return board[4];
}
if (board[2] == board[4] && board[4] == board[6])
{
return board[4];
}
}
return 0;
}
And here is the inital call:
int spot = 0;
int minEval = 2;
for (int i = 0; i < 9; i++)
{
if (board[i] == 0)
{
board[i] = -1;
int score = minimax(board, 3, -2, 2, false);
if (score < minEval) {
minEval = score;
spot = i;
}
board[i] = 0;
}
}
std::cout << "The computer went in spot " << spot + 1 << std::endl;
board[spot] = -1;
printBoard(board);
It looks like you only call minimax with a depth of three, so the algorithm will only look up to three moves ahead, if you want optimal play you need to set the depth to > 9, so that the agent is always looking ahead to the end of the game.

Hangs the application when you press the button, can not understand why

I am new to c++ and qt, but I want to learn. The essence of the problem: the application hangs when you click on the button, although before some edits worked adequately (rolling back edits did not help). As far as I understand it is because of the while loop, but nowhere can I find an answer on how to fix it.
The program itself, what I need to implement, is to write the interaction of predators and herbivores, for a start on a primitive level.
So, please help me with the problem of hanging the application.
Creating classes
class Grass
{...};
class Predator
{...};
class Herbivorous
{...};
Button click processing
void MainWindow::on_pushButton_clicked()
{
int kol_grass = ui->lineEdit_11->text().toInt(), kol_weeks = 0, t, t_x, t_y, kol_H = ui->lineEdit->text().toInt(), kol_P = ui->lineEdit_2->text().toInt(), k = 0, kk = 0;
list<Herbivorous> lst_H;
list<Predator> lst_P;
list<Grass> lst_G;
int kol_die_P_hungry = 0, kol_die_H_hungry = 0, kol_die_P_age = 0, kol_die_H_age = 0, eaten = 0;
int years = 0;
qsrand(QDateTime::currentMSecsSinceEpoch());
//resize dynamic lists
lst_H.resize(kol_H);
lst_P.resize(kol_P);
lst_G.resize(kol_grass);
//creating list iterators
list<Herbivorous>::iterator i = lst_H.begin();
list<Herbivorous>::iterator j = lst_H.begin();
list<Predator>::iterator ii = lst_P.begin();
list<Predator>::iterator jj = lst_P.begin();
list<Grass>::iterator grass_it = lst_G.begin();
//creating grass objects
for (; grass_it != lst_G.end(); grass_it++)
{
//...
}
grass_it = lst_G.begin();
//creating herbivore objects
for (; i != lst_H.end(); i++)
{
//...
}
//creating predator objects
for (; ii != lst_P.end(); ii++)
{
//...
}
i = lst_H.begin();
ii = lst_P.begin();
Then the same cycle
Apparently, the application hangs, when you click on the button. What am I doing wrong?
//as long as both animal species are alive, simulate life
while (lst_H.size() > 0 && lst_P.size() > 0)
{
//grass rendering
for (; grass_it != lst_G.end(); grass_it++)
{
if (grass_it->GetValue() != .0)
{
QTableWidgetItem * grass = new QTableWidgetItem();
grass->setBackground(Qt::green);
ui->tableWidget->setItem(grass_it->GetY(), grass_it->GetX(), grass);
}
}
grass_it = lst_G.begin();
//once a month grass replenishment
if (kol_weeks % 4 == 0)
{
grass_it = lst_G.begin();
for(; grass_it != lst_G.end(); grass_it++)
{
grass_it->Recovery();
}
grass_it = lst_G.begin();
}
//once a month check for starvation
if (kol_weeks % 4 == 0)
{
i = lst_H.begin();
for (; i != lst_H.end(); i++)
{
if (i->GetHungry() <= 0)
{
i->SetInLife(false);//смерть от голода
kol_die_H_hungry++;
}
else
{
i->SetAge(i->GetAge() + 1);//+1 год
}
}
i = lst_H.begin();
ii = lst_P.begin();
for (; ii != lst_P.end(); ii++)
{
if (ii->GetHungry() <= 0)
{
ii->SetInLife(false);//смерть от голода
kol_die_P_hungry++;
}
else
{
ii->SetAge(ii->GetAge() + 1);//+1 год
}
}
ii = lst_P.begin();
}
//once a year old age check
if (kol_weeks == 48)
{
i = lst_H.begin();
kol_weeks = 0;
years++;
for (; i != lst_H.end(); i++)
{
if (i->GetAge() >= ui->lineEdit_3->text().toInt())
{
i->SetInLife(false);//смерть от старости
kol_die_H_age++;
}
else
{
i->SetAge(i->GetAge() + 1);//+1 год
}
}
i = lst_H.begin();
ii = lst_P.begin();
for (; ii != lst_P.end(); ii++)
{
if ((ii->GetAge() >= ui->lineEdit_10->text().toInt()) || (ii->GetHungry() <= 0))
{
ii->SetInLife(false);//смерть от старости или от голода
kol_die_P_age++;
}
else
{
ii->SetAge(ii->GetAge() + 1);//+1 год
}
}
ii = lst_P.begin();
}
//movement of herbivores
for (; i != lst_H.end(); i++)
{
QTableWidgetItem * noherbivorous = new QTableWidgetItem();
noherbivorous->setBackground(Qt::black);
ui->tableWidget->setItem(i->GetY(), i->GetX(), noherbivorous);
if (i->GetInLife() == true)
{
i->Direction();
i->Hungry();
QTableWidgetItem * herbivorous = new QTableWidgetItem();
herbivorous->setBackground(Qt::white);
ui->tableWidget->setItem(i->GetY(), i->GetX(), herbivorous);
}
}
//moving predators
for (; ii != lst_P.end(); ii++)
{
QTableWidgetItem * nopredator = new QTableWidgetItem();
nopredator->setBackground(Qt::black);
ui->tableWidget->setItem(ii->GetY(), ii->GetX(), nopredator);
if (ii->GetInLife() == true)
{
ii->Direction();
ii->Hungry();
QTableWidgetItem * predator = new QTableWidgetItem();
predator->setBackground(Qt::red);
ui->tableWidget->setItem(ii->GetY(), ii->GetX(), predator);
}
}
lst_H.remove_if([] (Herbivorous a) {return (a.GetInLife() == false);});//the removal of dead
lst_P.remove_if([] (Predator a) {return (a.GetInLife() == false);});//the removal of dead
i = lst_H.begin();
j = lst_H.begin();
//death of parents and birth of children (herbivores)
for (; i != lst_H.end(); i++)
{
for (; j != lst_H.end(); j++)
{
if ((i->GetNumber() != j->GetNumber()) && (i->GetInLife() == true) && (j->GetInLife() == true) && (i->GetAge() >= i->GetMin_Rep()) && (i->GetAge() <= i->GetMax_Rep()) && (j->GetAge() >= j->GetMin_Rep()) && (j->GetAge() <= j->GetMax_Rep()))
{
t_x = i->GetX() - j->GetX();
t_y = i->GetY() - j->GetY();
if (((t_x == 1) || (t_x == -1) || (t_x == 0)) && ((t_y == 1) || (t_y == -1) || (t_y == 0)))
{
i->SetInLife(false);
j->SetInLife(false);
//creating children
t = qrand() % (4 + 1);//от 0 до 4 детей
for (int u = 0; u < t; u++)
{
Herbivorous child;
child.SetInLife(true);
child.SetX(qrand() % (234 + 1));//рандом
child.SetY(qrand() % (144 + 1));//рандом
child.SetAge(1);
child.SetFood(ui->lineEdit_6->text().toDouble());
child.SetNumber(kk);
child.SetMax_Rep(ui->lineEdit_4->text().toInt());
child.SetMin_Rep(ui->lineEdit_5->text().toInt());
k++;
lst_H.push_back(child);
}
}
}
}
j = lst_H.begin();
}
ii = lst_P.begin();
jj = lst_P.begin();
//death of parents and birth of children (predators)
for (; ii != lst_P.end(); ii++)
{
for (; jj != lst_P.end(); jj++)
{
if ((ii->GetNumber() != jj->GetNumber()) && (ii->GetInLife() == true) && (jj->GetInLife() == true) && (ii->GetAge() >= ii->GetMin_Rep()) && (ii->GetAge() <= ii->GetMax_Rep()) && (jj->GetAge() >= jj->GetMin_Rep()) && (jj->GetAge() <= jj->GetMax_Rep()))
{
t_x = ii->GetX() - jj->GetX();
t_y = ii->GetY() - jj->GetY();
if (((t_x == 1) || (t_x == -1) || (t_x == 0)) && ((t_y == 1) || (t_y == -1) || (t_y == 0)))
{
ii->SetInLife(false);
jj->SetInLife(false);
//creating children
t = qrand() % (4 + 1);//от 0 до 4 детей
for (int u = 0; u < t; u++)
{
Predator child;
child.SetInLife(true);
child.SetX(qrand() % (234 + 1));//рандом
child.SetY(qrand() % (144 + 1));//рандом
child.SetAge(1);
child.SetFood(ui->lineEdit_8->text().toDouble());
child.SetNumber(k);
child.SetMax_Rep(ui->lineEdit_9->text().toInt());
child.SetMin_Rep(ui->lineEdit_7->text().toInt());
kk++;
lst_P.push_back(child);
}
}
}
}
jj = lst_P.begin();
}
ii = lst_P.begin();
j = lst_H.begin();
//hunting of predators on herbivores
for (; ii != lst_P.end(); ii++)
{
if (ii->GetHungry() < 1)
{
for (; j != lst_H.end(); j++)
{
if ((ii->GetInLife() == true) && (j->GetInLife() == true))
{
t_x = ii->GetX() - j->GetX();
t_y = ii->GetY() - j->GetY();
if (((t_x == 1) || (t_x == -1) || (t_x == 0)) && ((t_y == 1) || (t_y == -1) || (t_y == 0)))
{
j->SetInLife(false);
ii->SetHungry(ii->GetHungry() + 1);
eaten++;
}
}
}
}
j = lst_H.begin();
}
grass_it = lst_G.begin();
//hunting herbivores on the grass
for (; j != lst_H.end(); j++)
{
if (j->GetHungry() < 1)
{
for (; grass_it != lst_G.end(); grass_it++)
{
if ((grass_it->GetValue() > 0) && (j->GetInLife() == true) && (j->GetX() == grass_it->GetX()) && (j->GetY() == grass_it->GetY()))
{
if (grass_it->GetValue() > 0.5)
{
j->SetHungry(j->GetHungry() + 0.5);
grass_it->SetValue(grass_it->GetValue() - 0.5);
}
else
{
j->SetHungry(j->GetHungry() + grass_it->GetValue());
grass_it->SetValue(0);
}
}
}
}
j = lst_H.begin();
}
i = lst_H.begin();
ii = lst_P.begin();
grass_it = lst_G.begin();
//summary of interim results
ui->label_11->setText("The number of herbivores: " + QString::number(lst_H.size()));
...
QEventLoop loop;
QTimer::singleShot(100, &loop, SLOT(quit()));
loop.exec();
kol_weeks++;
}
}```

visual studio access violation reading location 0xc0000005

I am receiving data in TCP in C++ using Qt library. I store the received packet in a QByteArray, but after reading the whole data, I face this error in debug. At the end, I try to clear the buffer, but I face this problem while trying to clear it too.
Here is my code :
void AvaNPortTester::scoket_readyRead()
{
ui.lineEdit_Sending_Status_->setText("Sent");
ui.lineEdit_Sending_Status_->setStyleSheet("QLineEdit { background: rgb(50, 255, 50); }");
tcpSocket_data_buffer_.append(tcpSocket_->readAll());
//qDebug() << serialport_data_buffer_.size();
//auto ddd = QString::number(tcpSocket_data_buffer_.size());// +" : " + tcpSocket_data_buffer_.toHex();
//ui.lableSocketRead->setText(ddd);
bool read_aain = false;
QByteArray dummy(int(1446), Qt::Initialization::Uninitialized);
int reminded_data = 0;
int dummy_size = 0;
int frame_size = 0;
int l_size = 0;
int total_size_rcvd = tcpSocket_data_buffer_.size();
//int total_size_rcvd_b = total_size_rcvd_b;
int temp = 0;
while (total_size_rcvd != 0)
{
if(total_size_rcvd != 0){
auto packet = tcpSocket_data_buffer_.mid(0, 1446);
auto rem = tcpSocket_data_buffer_.mid(1446);//****1146
tcpSocket_data_buffer_ = rem;
QDataStream streamdata(packet);
uint8_t Sync_Header[3];
auto ss = streamdata.readRawData((char*)&Sync_Header, 3);
uint8_t Total_size[2];
ss = streamdata.readRawData((char*)&Total_size, 2);
int t_size = Total_size[0] * 256 + Total_size[1];
uint8_t Reserved[2];
ss = streamdata.readRawData((char*)&Reserved, 2);
frame_size = t_size - 2;
reminded_data = t_size - 2;
while (frame_size != 0)
{
uint8_t portid;
ss = streamdata.readRawData((char*)&portid, 1);
//ui.lineEdit_FileSize->setText(QString::number(fileSend_2Ser->size()));
uint8_t ProtocolID;
ss = streamdata.readRawData((char*)&ProtocolID, 1);
uint8_t MoreFragmentFlag;
ss = streamdata.readRawData((char*)&MoreFragmentFlag, 1);
uint8_t Seq;
ss = streamdata.readRawData((char*)&Seq, 1);
uint8_t size[2];
ss = streamdata.readRawData((char*)&size, 2);
l_size = size[0] * 256 + size[1];
if (packet_flags.Ser2Eth.packet_started[portid] == false) {
uint8_t DDCMP_Header[14];
ss = streamdata.readRawData((char*)&DDCMP_Header, 14);
packet_flags.Ser2Eth.protocol_payload_size[portid] = DDCMP_Header[7] + 256 * DDCMP_Header[8];
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
packet_flags.Ser2Eth.packet_started[portid] = true;
}
QByteArray ddcmp_datap(int(l_size), Qt::Initialization::Uninitialized);
streamdata.readRawData(ddcmp_datap.data(), l_size - 14);
if ((pre_more_frag == 0) && (MoreFragmentFlag == 0)) {
packet_flags.Ser2Eth.packet_ended[portid] = true;
packet_flags.Ser2Eth.protocol_payload_size[portid] = l_size;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
else if ((pre_more_frag == 0) && (MoreFragmentFlag == 1)) {
packet_flags.Ser2Eth.packet_ended[portid] = false;
packet_flags.Ser2Eth.protocol_payload_size[portid] = l_size + 16;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
else if ((pre_more_frag == 1) && (MoreFragmentFlag == 1)) {
packet_flags.Ser2Eth.packet_ended[portid] = false;
packet_flags.Ser2Eth.protocol_payload_size[portid] = packet_flags.Ser2Eth.protocol_payload_size[portid] + l_size;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
else if ((pre_more_frag == 1) && (MoreFragmentFlag == 0)) {
packet_flags.Ser2Eth.packet_ended[portid] = true;
packet_flags.Ser2Eth.protocol_payload_size[portid] = packet_flags.Ser2Eth.protocol_payload_size[portid] + l_size;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
if (MoreFragmentFlag == 1) {
pre_more_frag = 1;
}
else {
pre_more_frag = 0;
}
int ff = 0;
if (packet_flags.Ser2Eth.packet_ended[portid] == true) {
packet_flags.Ser2Eth.packet_started[portid] = false;
packet_flags.Ser2Eth.packet_started[portid] = false;
set_port_id_flag(portid, packet_flags.Ser2Eth.protocol_payload_size[portid], ProtocolID);
pre_more_frag = 0;
}
reminded_data = reminded_data - 6 - l_size;
//ui.lableSocketRead->setText(ddcmp_datap.toHex());
frame_size = frame_size - l_size - 6;
}//end of while (frame_size != 0)
uint8_t sync_footer[3];
streamdata.readRawData((char *)&sync_footer, 3);
dummy_size = 1446 - t_size - 8;
uint8_t dummy_data[1000];
streamdata.readRawData((char *)&dummy_data, dummy_size);
total_size_rcvd = total_size_rcvd - 1446;
if (total_size_rcvd == 0) {
tcpSocket_data_buffer_.clear();
}
} //end of if
}//end of while()
}

CPP if elseif statement issues

h value can be between 0<400 or even more, the below coding practise looks tedious...any better thought?
if (h<=40) {
nOfRound = 1;
} else if (40<h<=80) {
nOfRound = 2;
} else if (80<h<=120) {
nOfRound = 3;
} else if (120<h<=160) {
nOfRound = 4;
} else if (160<h<=200) {
nOfRound = 5;
} else {
qDebug() <<"too big";
nOfRound = 6;
}
Like this:
if (h <= 200) { nOfRound = h / 40 + 1; }
else { nOfRound = 6; qDebug << "too big"; }
(Note that division by a constant is actually pretty efficient, since it is typically implemented by a (fast) multiplication rather than a dynamic division.)

Overview rpg tiled space

I'm trying to make it where the character is in a tile and when they move up or down it moves to the next tile but I'm not sure how to do that. Right now, I have it set up where the character moves by pixels but I want it to move by 1 square.
The code right now is this, and it works, but it's glitchy in pixel mode. I believe if it was by blocks it might work better but I might change it anyway.
float spritewidth = sprite->stretchX;
float spriteheight = sprite->stretchY;
float bushwidth = bush->stretchX;
float bushheight = bush->stretchY;
//Basic border collision
if (sprite->x <= 0)
sprite->x = 0;
if (sprite->y <= 0)
sprite->y = 0;
if (sprite->x >= 455)
sprite->x = 455;
if (sprite->y >= 237)
sprite->y = 237;
if ( (sprite->x + spritewidth > bush->x) && (sprite->x < bush->x + bushwidth) && (sprite->y + spriteheight > bush->y) && (sprite->y < bush->y + bushheight) )
{
bushcol = 1;
}
else
{
bushcol = 0;
}
if (osl_keys->held.down)
{
if (bushcol == 1)
{
sprite->y = bush->y - spriteheight - 3;
bushcol = 0;
}
else
{
bushcol = 0;
sprite->y += 3;
}
}
if (osl_keys->held.up)
{
if (bushcol == 1)
{
sprite->y = bush->y + bushheight + 3;
bushcol = 0;
}
else
{
bushcol = 0;
sprite->y -= 3;
}
}
if (osl_keys->held.right)
{
if (bushcol == 1)
{
sprite->x = bush->x - spritewidth - 3;
bushcol = 0;
}
else
{
bushcol = 0;
sprite->x += 3;}
}
if (osl_keys->held.left)
{
if (bushcol == 1)
{
sprite->x = bush->x + bushwidth + 3;
bushcol = 0;
}
else
{
bushcol = 0;
sprite->x -= 3;
}
}
If you want the character to move one tile/square/block at a time, just move the sprite the number of pixels the tile is wide (or tall).
const int tile_width = 32; // or something
// and then
sprite->x += tile_width;