Jump to content
Science Forums

Neural Network Help Request


Recommended Posts

Well. It's been quite a while since I've posted here. As it turns out, I managed to block the site through my router without realizing it. Oops.

 

Anyway, the reason I'm posting this is that I'm working on writing a backpropagating neural network in C++. I'm not entirely new to either, though I'm definitely still a novice in both, so any advice would be appreciated. Specifically, though, I've been chasing this one vector-index-out-of-bounds error for the last several days with no luck, and I'm thinking that someone else might fare better. The program is broken up into two main files, with the debug.h file being a library I put together for my own use across projects. Here are the files:

 

main.cpp:

#include <iostream>
#include <fstream>
#include <vector>
#include "neuron.h"
#include "debug.h"

using namespace std;

class bool_wrap
{
public:
int state;
bool_wrap(char);
bool noconflict(bool);
bool asbool();
};
bool_wrap::bool_wrap(char cs)
{
if (cs == '1') { state = 1; }		//true
else if (cs == '0') { state = 0; }	//false
else { state = 2; }					//ambiv
}
bool bool_wrap::noconflict(bool t)
{
if (t)
{
	if (state > 0) { return true; }
	else { return false; }
}
else
{
	if (state == 0 || state == 2) { return true; }
	else { return false; }
}
}
bool bool_wrap::asbool()
{
if (state == 1) { return true; }
else { return false; }
}

int main()
{
//declarations + initializations
int rowa[] = {3, 3, 2};
vector<int> rows (rowa, rowa + 3);
debuglevel = dev;
vector<nuro> exnv;
vector<nuro> *tnurvpt;
vector<vector<nuro> > net;
	for (size_t i = 0; i < rows.size(); i++)
	{
		net.push_back(exnv);
		if (i > 0) { tnurvpt = &net.at(i-1); }
		for (int j = 0; j < rows.at(i); j++)
		{
			if (i == 0) { net.at(i).push_back(nuro()); continue; }
			shared_ptr<vector<nuro> > sharednurvpt(tnurvpt);
			net.at(i).push_back(nuro(sharednurvpt));
		}
	}
string tstring;
bool cont = true;
fstream data("data.dat", ios::in);
fstream results("results.dat", ios::out);
vector<string> lines;
	while (data.good())
	{
		getline(data, tstring);
		lines.push_back(tstring);
	}
vector<bool_wrap> ans;
vector<bool> tbv;	//temporary bool storage vector
vector<int> tiv;	//temporary integer storage vector
bool goodgo;		//flag to tell if got this line right
int tlc = 0;		//top level cycle number

//main code block
while (cont)
{
	tlc++;
	debug("Iteration #", tlc, dev);
	indent++;
	for (size_t iln = 0; iln < lines.size(); iln++)	//gothru all lines
	{
		debug("Input: ", dev, startofline);
		string dots = "";	//desired output temp string
		for (size_t i = 0; i < net.at(0).size(); i++)	//gothru all nodes in col 1
		{
			net.at(0).at(i).state = bool_wrap(lines.at(iln).at(i)).asbool();		//get input
			debug(lines.at(iln).substr(i, 1), dev, midofline);
			ans.push_back(lines.at(iln).at(i + net.at(net.size() - 1).size() + 1));	//get correct responses
			dots += lines.at(iln).at(i + net.at(net.size() - 1).size() + 1);
		}
		ans.erase(ans.begin());
		dots.erase(dots.begin());
		debug(string("; Desired Output: ").append(dots.c_str()), dev, endofline);
		string ansholder = "";
		goodgo = false;
		indent++;
		while (!goodgo)
		{
			for (size_t i = 1; i < net.size(); i++)
			{
				for (size_t j = 0; j < net.at(i).size(); j++)
				{
					ansholder.append(1, net.at(i).at(j).update());				//update neural states
				}
			}
			ansholder.erase(ansholder.begin(), ansholder.end() - rows.at(rows.size() - 1));
			debug(string("Actual Output: ").append(ansholder.c_str()), dev);
			goodgo = true;
			for (size_t i = 0; i < ans.size(); i++)
			{
				if (!ans.at(i).noconflict(net.at(net.size() - 1).at(i).state))
				{
					goodgo = false;
				}
				if (!goodgo) { break; }
			}
			indent++;
			debug("Approaching '!goodgo' if statement", dev);
			if (!goodgo)														//got a wrong answer - do mod() step
			{
				vector<bool> boolvotes;											//non-cumulative booean votes
				vector<bool_wrap> abv;											//post-correction states
				indent++;
				for (size_t i = net.size() - 1; i > 0; i--)
				{
					debug("current column = ", i, dev);
					int bvoters = 0;
					tiv.erase(tiv.begin(), tiv.end());
					indent++;
					for (size_t j = 0; j < net.at(i).size(); j++)
					{
						debug("current row = ", j, dev);
						indent++;
						if (tiv.size() == 0)
						{	
							debug("tiv.size() == 0", dev);
							for (size_t k = 0; k < net.at(i - 1).size(); k++) { tiv.push_back(0); }
						}
						else
						{
							debug("tiv.size() == ", tiv.size(), dev);
						}
						//next line is future-proofing - in case want to add ambivilance
						if (ans.at(j).state == 2) { indent -= 2; continue; }
						if (i == net.size() - 1)
						{	
							debug("ti == net.size() - 1", dev);
							boolvotes = net.at(i).at(j).mod(ans.at(j).asbool());
						}
						else
						{
							debug("ti != net.size() - 1", dev);
							boolvotes = net.at(i).at(j).mod(abv.at(j).asbool());
						}
						if (boolvotes.size() == 0) { debug("Boolvotes is empty!", dev, onownline); }
						if (boolvotes.size() == 0) { indent--; continue; }
						debug("for(k) starting...", dev);
						for (size_t k = 0; k < boolvotes.size(); k++)
						{
							indent++;
							debug("vote #", k, dev, onownline);
								indent++;
								debug("boolvote[k] = ", boolvotes.at(k), dev, startofline);
								debug("; bvoters = ", bvoters, dev, endofline);
								debug("boolvotes.size() = ", boolvotes.size(), dev, onownline);
								debug("tiv.size() = ", tiv.size(), dev, startofline);
								tiv.at(k) += (int)boolvotes.at(k);
								debug("; tiv.at(k) = ", tiv.at(k), dev, endofline);
								bvoters++;
								indent--;
							indent--;
						}
						indent--;
					}
					indent--;
					abv.erase(abv.begin(), abv.end());
					for (size_t k = 0; k < net.at(i).size(); k++)
					{
						debug("L1, k=", k, fulldebug);
						abv.push_back(bool_wrap('0'));
					}
					for (size_t k = 0; k < tiv.size(); k++)
					{
						debug("L2, k=", k, fulldebug);
						if ((double)tiv.at(k) / (double)bvoters > .5) { abv.at(k) = bool_wrap('1'); }
						else if ((double)tiv.at(k) / (double)bvoters == .5) { abv.at(k) = bool_wrap('2'); }
					}
				}
				indent--;
				debug ("Iterated thru all columns", dev);
			}
			indent--;
			debug ("End of '!goodgo' if statement", dev);
		}	//end of "goodgo" while loop - got this line right
		indent--;
		debug ("End of 'goodgo' while loop reached", dev);
	}	//end of "lines" for loop - got each line right once
	string cst;
	cout << "Continue? <Y/N> ";
	cin >> cst;
	if (cst == "N" || cst == "n") { cont = false; }
	indent--;
}	//end of main while loop

//ending
system("PAUSE");
return 0;
}

 

neuron.h:

#ifndef NEURON_H
#define NEURON_H

#include <vector>
#include <memory>
#include "debug.h"

using namespace std;

double nthreshold = .5;	//neural activation threshold
double lr = .001;		//learning rate
double e = 2.71828183;

class nuro
{
public:
bool state;
vector<bool> states;						//storage for states of the preceeding row
vector<double> weights;						//neural weights
std::shared_ptr<vector<nuro>> lastrow;		//pointers to the nurons of the preceeding row

nuro();										//default constructor - does nothing, just to simplfy syntax at a few points
nuro(std::shared_ptr<std::vector<nuro>>&);	//constructor - ALWAYS use this when possible
char update();								//update own state
double squash(double);						//squash the results so that 0 <= n <= 1
vector<bool> mod(bool);						//perform self-correction step
};
nuro::nuro() {}
nuro::nuro(std::shared_ptr<vector<nuro>>& previousrow)
{
lastrow = previousrow;
for (size_t i = 0; i < (*lastrow).size();i++) { weights.push_back(.5); }
}
char nuro::update()
{
states.clear();
for (size_t i = 0; i < (*lastrow).size(); i++)
{
//		cout << "L1.1, i = " << i << "; ";
	states.push_back((*lastrow).at(i).state);
//		cout << "L1.2" << endl;
}
double sigma = 0;
for (size_t i = 0; i < states.size(); i++)
{
//		cout << "L2.1, i = " << i << "; ";
	sigma += ((int)states.at(i)) * weights.at(i);
//		cout << "L2.2, i = " << i << endl;
}
double val = squash (sigma);
if (val > nthreshold) { state = true; }
else { state = false; }

if (state)	{ return '1'; }
else		{ return '0'; }
}
double nuro::squash(double xval)
{
double aval = -.69314718;
return ((-1 * (1 / (2 * (.5 + pow(e, (xval + aval)))))));
}
vector<bool> nuro::mod(bool goodstate)
{	//NOTE: if returned a blank vector, good == state, so be sure to guard against that
indent++;
debug("Doing mod()", dev);
vector<bool> out;
indent++;
	debug("State = ", state, dev);
	debug("Goodstate = ", goodstate, dev);
indent--;
if (state == goodstate)
{
	debug("\tState == Goodstate", dev);
	indent--;
	return out;
}
indent++;
debug("Pushing... ", dev, startofline);
for (size_t i = 0; i < states.size(); i++)
{
	if (states.at(i))
	{
		weights.at(i) -= lr;
		out.push_back(false);
		debug("'false' ", dev, midofline);
	}
	else
	{
		weights.at(i) += lr;
		out.push_back(true);
		debug("'true' ", dev, midofline);
	}
}
debug("", dev, endofline);
indent--;
indent--;
return out;
}

#endif

 

debug.h:

#ifndef DEBUG_H
#define DEBUG_H

#include <iostream>
#include <string>

using namespace std;

enum format_t {
onownline,
startofline,
midofline,
endofline
};
enum debuglevels {
release,
dev,
fulldebug
};
extern debuglevels debuglevel;
extern unsigned short indent;
debuglevels debuglevel;
unsigned short indent = 0;

void debug (string message, debuglevels threshold, format_t style = onownline)
{
if (threshold > debuglevel)
{
	return;
}
if (style == onownline || style == startofline) { for (unsigned short i = 0; i < indent; i++) { cout << "\t"; } }
cout << message;
if (style == onownline || style == endofline) { cout << endl; }
}
void debug (string message, double num, debuglevels threshold, format_t style = onownline)
{
if (threshold > debuglevel)
{
	return;
}
if (style == onownline || style == startofline) { for (unsigned short i = 0; i < indent; i++) { cout << "\t"; } }
cout << message << num;
if (style == onownline || style == endofline) { cout << endl; }
}

#endif

Link to comment
Share on other sites

I'm using Visual Studio (2010 Express, as it happens), so here's the error it gives:

First-chance exception at 0x76ffc41f in 2013.09.10 Extensible Neural Network.exe: Microsoft C++ exception: std::out_of_range at memory location 0x003ced5c..
Unhandled exception at 0x778b15de in 2013.09.10 Extensible Neural Network.exe: Microsoft C++ exception: std::out_of_range at memory location 0x003ced5c..
The program '[764] 2013.09.10 Extensible Neural Network.exe: Native' has exited with code -529697949 (0xe06d7363).

I have no idea anymore where the bug might be, as it has thus far eluded all my efforts to find it, but here's some detail that might help (probably should have included this from the get-go; I'll edit it into the OP a bit later):

The program prints out

Approaching '!goodgo' if statement

and continues as expected through column #3 (index #2), working through both the rows successfully, before moving on to column #2 (index #1) and works through two of the three rows successfully, before printing out

tiv.size() == 3

and crashing almost immediately after with the error.

 

EDIT: Also, is there some sort of collapsible spoiler-esque tag? That might help readability somewhat.

Edited by The Polymath
Link to comment
Share on other sites

It is a long time I did not write in C++, but I used to know my way around.

 

But there is one thing I wonder in this part of the code (inside main):

 while (!goodgo)
                       {
                               for (size_t i = 1; i < net.size(); i++)
                               {
                                       for (size_t j = 0; j < net.at(i).size(); j++)
                                       {
                                               ansholder.append(1, net.at(i).at(j).update());                          //update neural states
                                       }
                               }
                               ansholder.erase(ansholder.begin(), ansholder.end() - rows.at(rows.size() - 1));
                               debug(string("Actual Output: ").append(ansholder.c_str()), dev);
                               goodgo = true;

 

Thing is I do not remember how exactly the while statement behaves, I mean the last line of the code I pasted sets goodgo to TRUE, so shouldn't the code never execute the rest of the while(!goodgo)-construct? No matter what the inut is?

 

I always worked on linux systems when coding, I think the gdb debugger is worth it! It might exists for windows machines, try it in case, it should give clearer error messages.

Link to comment
Share on other sites

It is a long time I did not write in C++, but I used to know my way around.

 

But there is one thing I wonder in this part of the code (inside main):

 while (!goodgo)
                       {
                               for (size_t i = 1; i < net.size(); i++)
                               {
                                       for (size_t j = 0; j < net.at(i).size(); j++)
                                       {
                                               ansholder.append(1, net.at(i).at(j).update());                          //update neural states
                                       }
                               }
                               ansholder.erase(ansholder.begin(), ansholder.end() - rows.at(rows.size() - 1));
                               debug(string("Actual Output: ").append(ansholder.c_str()), dev);
                               goodgo = true;

 

Thing is I do not remember how exactly the while statement behaves, I mean the last line of the code I pasted sets goodgo to TRUE, so shouldn't the code never execute the rest of the while(!goodgo)-construct? No matter what the inut is?

 

I always worked on linux systems when coding, I think the gdb debugger is worth it! It might exists for windows machines, try it in case, it should give clearer error messages.

 

The while statement only checks the condition at the start of the loop, so it shouldn't be causing problems. And I guess I'll have to look into gdb, then - Visual Studio is pretty usable, but it's a rather heavy program for my laptop, and I suspect gdb would be lighter. Also, Visual Studio is somewhat notorious for not being standard-compliant.

Link to comment
Share on other sites

Hi polymath,

 

It looks like the error is caused by your OS.

 

http://www.wiki-errors.com/wiki-errors.php?wiki=0xe06d7363

 

0xe06d7363 Error Codes are caused in one way or another by misconfigured system files in your windows operating system.

 

So, If you got 0xe06d7363 Error then We strongly recommend that you Download (0xe06d7363) Repair Tool.

 

This article contains information that shows you how to fix your windows 0xe06d7363 error both (manually) and (automatically) , In addition, this article will help you troubleshoot some common error messages related to 0xe06d7363 error code that you may receive.

Link to comment
Share on other sites

Hi polymath,

 

It looks like the error is caused by your OS.

 

http://www.wiki-errors.com/wiki-errors.php?wiki=0xe06d7363

That... Doesn't look like the most credible of sites. All the pages for hexadecimal strings give you the same page with different text swapped in for that particular number, all asking you to download their program. And a vector-index-out-of-range error is... unlikely to be caused by "misconfigured system files in your windows operating system". Never mind the fact that the page is riddled with errors in grammar, reading sort of like an automatically-generated spam email.

Link to comment
Share on other sites

A pity about that, especially as it looks like an English site.

 

The error must be before your next debug statement so debug the variables and see which one blows up.

 

//next line is future-proofing - in case want to add ambivilance

debug("j = ", j, dev);

debug("ans.at(j).state = ", ans.at(j).state, dev);

if (ans.at(j).state == 2) { indent -= 2; continue; }

debug("i = ", i, dev);

debug("net.size() - 1 = ", net.size() - 1, dev);

if (i == net.size() - 1)

Edited by LaurieAG
Link to comment
Share on other sites

And therein lies my frustration. Really the only thing that could be causing this error at that point would be the call to ans.at(j).state, but that went out the window after I tried ensuring that ans.size() is always greater than j. Argh. I suppose I could try it again, just to make sure I didn't completely miss the mark when I was trying, as I really am out of ideas at this point.

 

Wish me luck, I suppose.

Edited by The Polymath
Link to comment
Share on other sites

And therein lies my frustration. Really the only thing that could be causing this error at that point would be the call to ans.at(j).state, but that went out the window after I tried ensuring that ans.size() is always greater than j. Argh. I suppose I could try it again, just to make sure I didn't completely miss the mark when I was trying, as I really am out of ideas at this point.

 

Wish me luck, I suppose.

 

There are still heaps of things to try Polymath.

 

1. If you comment out the error line does the program run?

 

I note that you define state as an integer in main.cpp and as boolean in neuron.h. The c0000005 (windows wobble bug in VS 5) error was caused by redefining the type of a public variable in a subroutine. Each time you ran the offending subroutine the OS would create another COM+ object to its maximum limit of 8 and then blow up the next time the subroutine was was called. I know your public declarations are in classes but almost everything else seems to be in order in your code so you never know your luck.

 

2. Change state to nstate in neuron.h

 

Many of the older development environments also had a list of reserved words that could not be used at the start of a variable name without undesirable consequences (functions & operators etc). While this issue may be mitigated in later versions you never know if there is a finite limit like above.

 

3. Change boolvotes to votesbool.

 

Good luck.

Link to comment
Share on other sites

There are still heaps of things to try Polymath.

 

1. If you comment out the error line does the program run?

 

I note that you define state as an integer in main.cpp and as boolean in neuron.h. The c0000005 (windows wobble bug in VS 5) error was caused by redefining the type of a public variable in a subroutine. Each time you ran the offending subroutine the OS would create another COM+ object to its maximum limit of 8 and then blow up the next time the subroutine was was called. I know your public declarations are in classes but almost everything else seems to be in order in your code so you never know your luck.

 

2. Change state to nstate in neuron.h

 

Many of the older development environments also had a list of reserved words that could not be used at the start of a variable name without undesirable consequences (functions & operators etc). While this issue may be mitigated in later versions you never know if there is a finite limit like above.

 

3. Change boolvotes to votesbool.

 

Good luck.

 

1) The program runs, but it still crashes in the same way.

2) I'm using VS2010, so that shouldn't be an issue, though I'm not sure what you are referring to with redefining the variable's type - I have nuro.state and bool_wrap.state, but they shouldn't be conflicting, as they are in different classes.

3) I went and checked, and this is not an issue in VS2010.

Link to comment
Share on other sites

Please note Polymath I'm interested in your work because one of my uni subjects covered C++, OOP and neural nets separately.

 

1) The program runs, but it still crashes in the same way.

As the test on ans.at(j).state only occurs once in your code it would be handy to know what is blowing up where when this statement is commented out.

 

2) I'm using VS2010, so that shouldn't be an issue, though I'm not sure what you are referring to with redefining the variable's type - I have nuro.state and bool_wrap.state, but they shouldn't be conflicting, as they are in different classes.

 

I always thought that things were very straight forward with MS based software development but that was 20 years ago when I did my Applied Science Computing degree, now I don't discount anything or necessarily believe what I read and realise from experience that the solution is usually the last thing you try and if you don't try you will never know if that was the problem/solution or not. This would take about a minute to do and make no difference to the operation of your program if it did not solve the problem.

 

3) I went and checked, and this is not an issue in VS2010.

Refer above. It is always a good programming policy to keep all your variable names unique so you don't ever have to worry about any weird consequences from not doing so. Again this would take about a minute to do and not change how your program operates.

 

A screen print/text of the last page of debug output before the error would be useful before we start discussing the more complex methods you can try.

Link to comment
Share on other sites

Please note Polymath I'm interested in your work because one of my uni subjects covered C++, OOP and neural nets separately.

 

 

As the test on ans.at(j).state only occurs once in your code it would be handy to know what is blowing up where when this statement is commented out.

 

 

 

I always thought that things were very straight forward with MS based software development but that was 20 years ago when I did my Applied Science Computing degree, now I don't discount anything or necessarily believe what I read and realise from experience that the solution is usually the last thing you try and if you don't try you will never know if that was the problem/solution or not. This would take about a minute to do and make no difference to the operation of your program if it did not solve the problem.

 

 

Refer above. It is always a good programming policy to keep all your variable names unique so you don't ever have to worry about any weird consequences from not doing so. Again this would take about a minute to do and not change how your program operates.

 

A screen print/text of the last page of debug output before the error would be useful before we start discussing the more complex methods you can try.

Apologies if I came off as dismissive or otherwise undiplomatic. I really do appreciate your help.

I went through and followed all your advice, and it seems that that statement was causing it to crash. I could have sworn I tried commenting out that line before, only for it to make no difference, but I suppose I was mistaken. Attached are the screen captures you asked for, for the case when the line is not commented out and when it is commented out, respectively.

When the line is not commented out:

post-20728-0-92298600-1381777478_thumb.png

When the line is commented out:

post-20728-0-97934500-1381777911_thumb.png

Also, I tried commenting out the

boolvotes = net.at(i).at(j).mod(abv.at(j).asbool());

line, thinking that if there was a problem with the "ans" vector, there might be a problem with the "abv" vector, but that made it get stuck in a loop:

post-20728-0-87017600-1381778485_thumb.png

post-20728-0-36194400-1381778487_thumb.png

Which is notable because the

boolvotes = net.at(i).at(j).mod(ans.at(j).asbool());

line doesn't crash the program, even though it should do so just as much as the

if (ans.at(j).state == 2) { indent -= 2; continue; }

one.

 

Tangentially, your coursework covered C++ and OOP separately? Do you mean that you covered C++ without objects? I'm curious to know.

Link to comment
Share on other sites

Apologies if I came off as dismissive or otherwise undiplomatic. I really do appreciate your help.

I went through and followed all your advice, and it seems that that statement was causing it to crash. I could have sworn I tried commenting out that line before, only for it to make no difference, but I suppose I was mistaken.

...

Also, I tried commenting out the

boolvotes = net.at(i).at(j).mod(abv.at(j).asbool());

line, thinking that if there was a problem with the "ans" vector, there might be a problem with the "abv" vector, but that made it get stuck in a loop:

...

Which is notable because the

boolvotes = net.at(i).at(j).mod(ans.at(j).asbool());

line doesn't crash the program, even though it should do so just as much as the

if (ans.at(j).state == 2) { indent -= 2; continue; }

one.

No problems Polymath, I know how frustrating these things can be.

 

1. Apart from changing boolvotes to votesbool globally you could start by just populating boolvotes with something instead of completely commenting out the line to see if it still gets stuck in a loop or goes through to the third row. You may like to test an int and a boolean value to see if you can bring on the error as int and bool types have different memory storage requirements and this is one potential way to get that "out_of_range at memory location" error.

 

2. If 1. gets you out of the loop you can then try different forms of the

if (ans.at(j).state == 2) { indent -= 2; continue; }

statement. i.e. if only the == 2 form causes the error you just have to restructure your test to NOT actually test for the bad form but capture it in a bare else at the end while the working tests ( == 0 and == 1 ) actually do nothing.

 

Tangentially, your coursework covered C++ and OOP separately? Do you mean that you covered C++ without objects? I'm curious to know.

The subject was Emerging Technologies so we broadly covered everything for all three but never actually did all 3 together as in your program. We did cover C++ and OOP together in that we had to write an OOP ping pong program as an assignment in C++ but we never wrote a neural net program.

 

I'm less than a week out from a double laparoscopic hernia operation and am still a bit out of sorts so I'll have a closer look at the outputs over the next day or so and get back to you.

Link to comment
Share on other sites

No problems Polymath, I know how frustrating these things can be.

 

1. Apart from changing boolvotes to votesbool globally you could start by just populating boolvotes with something instead of completely commenting out the line to see if it still gets stuck in a loop or goes through to the third row. You may like to test an int and a boolean value to see if you can bring on the error as int and bool types have different memory storage requirements and this is one potential way to get that "out_of_range at memory location" error.

 

2. If 1. gets you out of the loop you can then try different forms of the

if (ans.at(j).state == 2) { indent -= 2; continue; }

statement. i.e. if only the == 2 form causes the error you just have to restructure your test to NOT actually test for the bad form but capture it in a bare else at the end while the working tests ( == 0 and == 1 ) actually do nothing.

 

 

The subject was Emerging Technologies so we broadly covered everything for all three but never actually did all 3 together as in your program. We did cover C++ and OOP together in that we had to write an OOP ping pong program as an assignment in C++ but we never wrote a neural net program.

 

I'm less than a week out from a double laparoscopic hernia operation and am still a bit out of sorts so I'll have a closer look at the outputs over the next day or so and get back to you.

 

A quick update - I've reworked my program in order to clean up some less than ideal sections and work around some of the potential problem points, and the good news is that I am no longer encountering the same error. Unfortunately, the reason that I am no longer encountering this error is that I appear to have hit another one, earlier on in the program. Here is my program as it stands now:

#include <iostream>
#include <fstream>
#include <vector>
#include "neuron.h"
#include "debug.h"

using namespace std;

class bool_wrap
{
public:
int state;
bool_wrap(char);
bool noconflict(bool);
bool asbool();
};
bool_wrap::bool_wrap(char cs)
{
if (cs == '1') { state = 1; }		//true
else if (cs == '0') { state = 0; }	//false
else { state = 2; }					//ambiv
}
bool bool_wrap::noconflict(bool t)
{
if (t)
{
	if (state > 0) { return true; }
	else { return false; }
}
else
{
	if (state == 0 || state == 2) { return true; }
	else { return false; }
}
}
bool bool_wrap::asbool()
{
if (state == 1) { return true; }
else { return false; }
}

int main()
{
int rowa[] = {3, 3, 2};
vector<int> rows (rowa, rowa + 3);
debuglevel = dev;
//declarations + initializations
vector<nuro> exnv;
vector<nuro> *tnurvpt;
vector<vector<nuro> > net;
	for (size_t i = 0; i < rows.size(); i++)
	{
		net.push_back(exnv);
		if (i > 0) { tnurvpt = &net.at(i-1); }
		for (int j = 0; j < rows.at(i); j++)
		{
			if (i == 0) { net.at(i).push_back(nuro()); continue; }
			shared_ptr<vector<nuro> > sharednurvpt(tnurvpt);
			net.at(i).push_back(nuro(sharednurvpt));
		}
	}
string tstring;
bool cont = true;
fstream data("data.dat", ios::in);
fstream results("results.dat", ios::out);
vector<string> lines;
	while (data.good())
	{
		getline(data, tstring);
		lines.push_back(tstring);
	}
vector<bool_wrap> ans;
vector<bool> tbv;	//temporary bool storage vector
vector<int> tiv;	//temporary integer storage vector
bool goodgo;		//flag to tell if got this line right
int tlc = 0;		//top level cycle number

//main code block
while (cont)
{
	tlc++;
	debug("Iteration #", tlc, dev);
	indent++;
	for (size_t iln = 0; iln < lines.size(); iln++)	//gothru all lines
	{
		debug("Input: ", dev, startofline);
		string dots = "";	//desired output temp string
		for (size_t i = 0; i < net.at(0).size(); i++)	//gothru all nodes in col 1
		{
			net.at(0).at(i).nstate = bool_wrap(lines.at(iln).at(i)).asbool();		//get input
			debug(lines.at(iln).substr(i, 1), dev, midofline);
			ans.push_back(lines.at(iln).at(i + net.at(net.size() - 1).size() + 1));	//get correct responses
			dots += lines.at(iln).at(i + net.at(net.size() - 1).size() + 1);
		}
		ans.erase(ans.begin());
		dots.erase(dots.begin());
		debug(string("; Desired Output: ").append(dots.c_str()), dev, endofline);
		string ansholder = "";	//holds the states the update() calls set the nuros to
		vector<string> ansv;	//replacement for ansholder
			ansv.push_back("");
		goodgo = false;
		indent++;
		while (!goodgo)
		{
			for (size_t i = 1; i < net.size(); i++)
			{
				ansv.push_back("");
				for (size_t j = 0; j < net.at(i).size(); j++)
				{
					ansv.at(i).append(1, net.at(i).at(j).update());
//						ansholder.append(1, net.at(i).at(j).update());				//update neural nstates
				}
			}
//				ansholder.erase(ansholder.begin(), ansholder.end() - rows.at(rows.size() - 1));
			debug(string("Actual Output: ").append(ansv.at(ansv.size()-1).c_str()), dev);
			debug ("Returned from actual output debug statement", dev);
			goodgo = true;
			for (size_t i = 0; i < ans.size(); i++)
			{
				if (!ans.at(i).noconflict(net.at(net.size() - 1).at(i).nstate))
				{
					goodgo = false;
				}
				if (!goodgo) { break; }
			}
			indent++;
			debug("Approaching '!goodgo' if statement", dev);
			if (!goodgo)												//got a wrong answer - do mod() step
			{
				vector<bool> targetnstates;								//states wanted for hidden cols
				vector<bool_wrap> abv;									//storage of this col's wanted states
				indent++;
				for (size_t i = net.size() - 1; i > 0; i--)				//for all cols
				{
					debug("current column = ", i, dev);
					int bvoters = 0;										//cumulative # of voters
					tiv.erase(tiv.begin(), tiv.end());						//clear out tiv
					indent++;
					for (size_t j = 0; j < net.at(i).size(); j++)			//for all rows
					{
						debug("current row = ", j, dev);
						indent++;
						if (tiv.size() == 0)								//if just started this row
						{	//bring tiv to needed size; fill spots w/ 0s
							//needed size is the size() of the next col
							debug("tiv.size() == 0", dev);
							for (size_t k = 0; k < net.at(i - 1).size(); k++) { tiv.push_back(0); }
						}
						else
						{	//do nothing
							debug("tiv.size() == ", tiv.size(), dev);
						}
						if (j < ans.size())
						{	//future-proofing - in case want to add ambivilance
							if (ans.at(j).state == 2) { indent--; continue; }
						}
						if (i == net.size() - 1)
						{	//on first col chrono'ly; last row sequentially
							//take answers from the ans vector
							debug("ti == net.size() - 1", dev);
							targetnstates = net.at(i).at(j).mod(ans.at(j).asbool());
						}
						else
						{	//on other col
							//take answers from abv vector
							debug("ti != net.size() - 1", dev);
							targetnstates = net.at(i).at(j).mod(abv.at(j).asbool());
						}
						if (targetnstates.size() == 0) { debug("targetnstates is empty!", dev, onownline); }
//							if (targetnstates.size() == 0) { indent--; continue; }
						if (targetnstates.size() == 0)
						{
							vector<bool> nextcolstatev;
							for (size_t k = 0; k < net.at(i - 1).size(); k++)
							{	//= next col's states (they are correct)
								nextcolstatev.push_back(net.at(i - 1).at(k).nstate);
							}
							targetnstates = nextcolstatev;
						}
						debug("for(k) starting...", dev);
						for (size_t k = 0; k < targetnstates.size(); k++)
						{
							indent++;
							debug("vote #", k, dev, onownline);
								indent++;
								debug("targetnstates[k] = ", targetnstates.at(k), dev, startofline);
								debug("; bvoters = ", bvoters, dev, endofline);
								debug("targetnstates.size() = ", targetnstates.size(), dev, onownline);
								debug("tiv.size() = ", tiv.size(), dev, startofline);
								tiv.at(k) += (int)targetnstates.at(k);
								debug("; tiv.at(k) = ", tiv.at(k), dev, endofline);
								bvoters++;
								indent--;
							indent--;
						}
						indent--;
					}
					indent--;
					abv.erase(abv.begin(), abv.end());			//clear abv
					for (size_t k = 0; k < net.at(i).size(); k++)
					{	//prime abv with starting "false" states
						debug("L1, k=", k, fulldebug);
						abv.push_back(bool_wrap('0'));
					}
					for (size_t k = 0; k < tiv.size(); k++)
					{
						debug("L2, k=", k, fulldebug);
						if ((double)tiv.at(k) / (double)bvoters > .5)
						{
							abv.at(k) = bool_wrap('1');
						}
						else if ((double)tiv.at(k) / (double)bvoters == .5)
						{
							if (net.at(i - 1).at(k).nstate) { abv.at(k) = bool_wrap('1'); }
						}
						//else; to add ambivilance:
//							else if ((double)tiv.at(k) / (double)bvoters == .5) { abv.at(k) = bool_wrap('2'); }
					}
				}
				indent--;
				debug ("Iterated thru all columns", dev);
			}
			indent--;
			debug ("End of '!goodgo' if statement", dev);
		}	//end of "goodgo" while loop - got this line right
		indent--;
		debug ("End of 'goodgo' while loop reached", dev);
	}	//end of "lines" for loop - got each line right once
	string cst;
	cout << "Continue? <Y/N> ";
	cin >> cst;
	if (cst == "N" || cst == "n") { cont = false; }
	indent--;
}	//end of main while loop

//ending
system("PAUSE");
return 0;

I have changed nothing else in the program (aside from changing state to nstate in the neuron.h file. Here is the output of the program:

And this is the error I get:

After adding

cout << "d";

at the very end of each debug(...) function, I get this output:

 

Which is even more perplexing, as the debug(...) functions are completing successfully, but there seems to be some sort of problem between when this particular instance of the function completes and when it returns. Even worse is that before this error cropped up, I had not changed debug.h at all for a number of months, before I even started this project.

 

Also, if you don't mind me asking, when was this course? For a course titled "Emerging Technologies" to include C++ and OOP it would had to have been a while ago, I would think. That's not to imply that it's outdated, of course - I'm just a bit curious, is all.

Link to comment
Share on other sites

I have changed nothing else in the program (aside from changing state to nstate in the neuron.h file.

...

And this is the error I get:

That's a real pain Polymath as the error is the c0000005 error (type mismatch) I referred to in post # 10.

 

I assume you changed the public declaration (class nuro { public: bool nstate) as well? The minor changes to neuron.h should not have made any difference but it does appear to bring the type error out into the open.

 

Ambivalence is the reason you use separate int and bool types so you may need to restructure your program and modify your code to make the state type an int throughout as you will have no way to introduce ambivalence later if you just use bool for both and get that working. Have a think about this and get back to me.

 

Also, if you don't mind me asking, when was this course? For a course titled "Emerging Technologies" to include C++ and OOP it would had to have been a while ago, I would think. That's not to imply that it's outdated, of course - I'm just a bit curious, is all.

I did that course in 1992 so the OS was DOS. The C++ code was not that much different and the basic event loop provided good insights into windows development in VS (and non MS 16 bit windows software development), especially when migrating legacy applications from DOS to windows. My ping pong main was just a couple of global variables and loop with an incrementing counter that was passed to the object/class for processing.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...