GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Feb-2009, 15:05
Omega037 Omega037 is offline
New Member
 
Join Date: Feb 2009
Posts: 5
Omega037 is on a distinguished road

2D Vector Memory Problems


I have been having a serious problem relating to memory allocation and arrays/vectors in C++ using the G++ compilier on both a windows machine(Dev-C++) and linux(debian). I would post all the code, but it is proprietary technology(I'm a Graduate Student) and therefore I can't share it openly without permission.

That said, the part of the code that is having a problem is not the proprietary part, so I will give a redacted version of what I am doing and explain the error I am getting.


Basically, the program has 2 classes, which we will Trainer and Runner. The main function merely calls an instance of Trainer once with certain parameters, which in turn calls one instance of Runner with certain parameters.


The redacted header files for each look something like this:

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string.h>
#include <time.h>
#include <math.h>
#include <vector>

using namespace std;

class Runner {

private:
   int num_gates;                      
   int num_models;                     
   int num_trainings;                  
   int total;



  vector< vector<float> > training_data;              //array of training pairs


public:

       Runner();
   Runner(int n_gates, int n_models, int n_trainings, char *train_file);             //constructor
   ~Runner();                                     //destructor

   float compute_output();

   vector< vector<float> > load_training_data();	//loads training data

   void setsigmas(vector<float> sig);


CPP / C++ / C Code:
#include "Runner.h"


class Trainer {

private:
             
   vector<vector<float> > sig_data;             

   int population_size;     
   int num_generations;     //number of generations
  

   Runner g;      
   int num_trainings;       //number of training pairs
   int num_gates;           //number of gate variables
   int num_models;          //number of models

   void save_pop();
   vector<vector<float> > load_pop();

   vector<float> mutcross_sig;

   void mutcross();


Basically, each class has a 2D Vector which holds training set data and paramter data respectively. These do not change in size once filled, and the training data never changes once loaded.

Here is the constructor and load data for the Runner class:

CPP / C++ / C Code:
#include "Runner.h"

Runner::Runner(){
       
                           }

Runner::Runner(int n_gates, int n_models, int n_trainings){


   num_gates = n_gates;
   num_models = n_models;
   num_trainings = n_trainings;
   total = n_gates + n_models + 1;


 try{
training_data = vector<vector<float> >(n_trainings, vector<float> (total) );
}
 catch (bad_alloc) {
  cout << "PROBLEM";
 }


 training_data = load_training_data();


}


vector<vector<float> > Runner::load_training_data()
{

   ifstream c("test1.txt");
   float abc;
   
   vector< vector<float> > t_data;
   vector<float> def;

      for(int i = 0; i < num_trainings; i++){

		for(int j = 0; j < total; j++){

         	c >> abc;
             def.push_back(abc);

              }

              t_data.push_back(def);
              def.clear();

     }
    return t_data;
}

This is the constructor and load data for the Trainer class:

CPP / C++ / C Code:
#include "Trainer.h"
Trainer::Trainer(int pop_size, int num_gen, int n_trainings, int n_gates, int n_models){
      
      


     total = n_gates +1;  
     float t_sig[total-1];

     top=1000;
   
   
char savefile[40];         
float holdB;
      int nogo=0;

   //set the parameters
   population_size = pop_size;
   num_generations = num_gen;
   num_trainings = n_trainings;
   num_gates = n_gates;
   num_models = n_models;

   
   cout <<"Save To Filename" << endl;
   cin >> savefile; 
   pop.open(savefile);
  gen_report.open("abctre4.txt");


 
    g = Runner(num_gates, num_models, num_trainings);
	 
   
   int load=0;
   cout << "Load Data? (1=YES, 0=NO)";
   cin >> load;
   
   if(load == 1){

           sig_data = load_pop();
              gen_best=0;
              gen_avg=0;
           
           }
   else{
   
   for(int t=0;t<pop_size;t++){
   sig_data.push_back(vector<float>());
}
   for(int y=0; y < pop_size; y++){

           for(int j=0; j < num_gates; j++){
                   holdB=random(min_value, max_value);

                              try{
                 sig_data[y].push_back(holdB);
                 }
                 catch(bad_alloc){
                                  cout << " MEMLIMIT ";
                                  try{
                 sig_data[y].push_back(holdB);
                 }
                 catch(bad_alloc){
                                  cout << " MEMLIMIT_AGAIN ";
                                  nogo=1;
                                  }
                                  }
                 t_sig[j]=holdB;
                 }

                g.setsigmas(t_sig);
                holdB=g.crossvalidation_run(0);

             sig_data[y].push_back(holdB);

                       
             if(holdB <= top){
                    gen_best = y;
                    top = holdB;
                    }   
                    
                 if(nogo == 0){
                       gen_avg = gen_avg + holdB;                                        
                    }
                    else{
                    gen_avg = gen_avg + (gen_avg/y+1);
                       nogo = 0;        
                    }
                    }

}

                    for(int x=0; x < num_gates; x++){
                              try{
                 mutcross_sig.push_back(0);
                 }
                 catch(bad_alloc){
                                  cout << " MUTMEMLIMIT ";
                                  try{
                 mutcross_sig.push_back(0);
                 }
                 catch(bad_alloc){
                                  cout << " MUTMEMLIMIT_AGAIN ";
                                  }
                                  }
                                  }
              

   
}
/***************************************************************/

vector<vector<float> > Trainer::load_pop()
{

char loadfile[40];

cout <<"Load From Filename" << endl;

cin >> loadfile; 
 	ifstream inpop(loadfile);
   float abc;
   vector< vector<float> > s_data;
   vector<float> indef;
      for(int i = 0; i < population_size; i++){

		for(int j = 0; j < total; j++){

         	inpop >> abc;
             indef.push_back(abc);

              }

              s_data.push_back(indef);
              indef.clear();
     }
     inpop.close();
    return s_data;
}

The Trainer then goes through iterations of setting certain paramters in the Runner and running it, then mutating the parameters based on the output from that process.

Unfortunately, regardless of if I try to load the data from a file or randomly generate it, it will often fire off bad allocation errors or crash the program altogether. The same thing happens if I use float ** and "new" to create the 2D Vectors. Any ideas?

Also:

1. With float ** used instead of vectors, it basically doesn't work and crashes. With the vectors, I can at the very least get it to skip past the ones it allocates badly so that it will run for a few generations(sometimes), though loading data from the file for sig_data is very hit or miss.


2. It uses only floats, though sometimes when it makes a bad allocation error, it fills a spot with junk data.


Basically, the program uses the training data values, along with the sigma values, in a complex series of mathematical equations. The vectors store floats and sig_data only stores positive floats.

The program is using Differential Evolution at one point, so mutcross_sig is filled with parameters based on mutated versions of the one it is competing against. In other words, for each sig_data[i], mutcross becomes something like:

mutcross[j] = sig_data[i][j] * 1.2;

Anyways, the try/catch actually catches a few bad_allocs, but I don't know why I am getting them or how to handle them. Also, if I try to add ifstreams or ofstreams, it causes a lot of errors too. Backtrace in GDB is no help.

3. When it crashes tends to change wildly if I make any kind of adjustment to the program. Even if I add a cout it might change which function it happens in.

Before I had the try/catch, it would sometimes fail when reading the data into the file, or when passing the vector back from load_training_data. Sometimes it would crash anytime I tried to cout the contents of the vector/array or tried to use them in a calculation. Sometimes it runs fine for 20+ generations through the mutation and selection process, then crashes.

4. It might also be worth noting that if I don't load the sig_data from a file, and I keep the population size small, it almost never crashes. Unfortunately I need both of those features working.
Last edited by admin : 12-Feb-2009 at 20:59. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 12-Feb-2009, 18:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: 2D Vector Memory Problems


Quote:
Originally Posted by Omega037
I have been having a serious problem...but it is proprietary technology(I'm a Graduate Student) and therefore...

So, here's a suggestion: Boil things down to something that you can post that can actually be compiled and will show the problem.

There are several advantages to that:

1. If someone else can reproduce the problem, maybe you can get some help.

2. In the process of creating an executable subset you may actually spot something in your code that is causing the problem.

3. If the subset doesn't show the problem, then you may be able to add stuff incrementally to get to the point if misbehaviour.

Quote:
Originally Posted by Omega037
...The main function merely calls an instance of Trainer once with certain parameters, which in turn calls one instance of Runner with certain parameters....

Unfortunately, regardless of if I try to load the data from a file or randomly generate it, it will often fire off bad allocation errors or crash the program altogether.
So, in your reduced problem, forget about all of the file stuff and just do something predictable and repeatable.

Quote:
Originally Posted by Omega037
With the vectors, I can at the very least get it to skip past the ones it allocates badly so that it will run for a few generations(sometimes),
Are you saying that you get allocation errors and then try to proceed anyhow? Why?
Quote:
Originally Posted by Omega037
2. It uses only floats, though sometimes when it makes a bad allocation error, it fills a spot with junk data.
Well, yes. If it can't allocate new storage, the program probably writes over old stuff. Memory allocated by vectors will not necessarily be aligned in such a way that the stuff is still a valid floating point number. But so what? Why the heck would you try to proceed when you know it's bad?

Quote:
Originally Posted by Omega037
Basically, the program...
But if your problem is with allocation, all of the manipulations are irrelevant, right?
Quote:
Quote:
Originally Posted by Omega037
Anyways, the try/catch actually catches a few bad_allocs, but I don't know why
There's not much point in proceeding until you get to the bottom of it,
3. When it crashes tends to change wildly if I make any kind of adjustment to the program.
That's typical of memory-gone-wild. Stuff is writing outside the allocated storage.
Quote:
Originally Posted by Omega037
4. It might also be worth noting that if I don't load the sig_data from a file, and I keep the population size small, it almost never crashes. Unfortunately I need both of those features working.
I don't see where you check whether the file is even opened successfully, let alone whether it can actually read data from the file. Also, we have no idea what you consider a small population. Is "small" 1000, 10000, 100000, or what?

CPP / C++ / C Code:
//Runner.h
#ifndef RUNNER_H__
#define RUNNER_H__
#include <vector>
using std::vector;

class Runner
{

  private:
    int num_gates;                      
    int num_models;                     
    int num_trainings;                  
    int total;
    vector< vector<float> > training_data;  //array of training pairs

  public:
    Runner(){};
    //Runner(int n_gates, int n_models, int n_trainings, char *train_file);
    Runner(int n_gates, int n_models, int n_trainings);
    ~Runner(){};
    float compute_output();
    vector< vector<float> > load_training_data();
    void setsigmas(vector<float> sig);
};
#endif

CPP / C++ / C Code:
//Runner.cpp
#include <iostream>
#include <fstream>
#include <vector>

#include "Runner.h"

using namespace std;
Runner::Runner(int n_gates, int n_models, int n_trainings)
{
    num_gates     = n_gates;
    num_models    = n_models;
    num_trainings = n_trainings;
    total = n_gates + n_models + 1;

    cout << endl
         << "Runner constructor: n_gates = " << n_gates
         << ", n_models = " << n_models
         << ", n_trainings = " << n_trainings
         << endl;
    cout << "Will allocate "
         << n_trainings*total*sizeof(float)
         << " bytes for training_data"
         << endl;
    try{
        training_data = 
           vector<vector<float> >(n_trainings, vector<float>(total));
    }

    catch (bad_alloc) {
        cout << "PROBLEM";
    }
    cout << "training_data.size() * training_data[0].size * sizeof(float) = "
         << training_data.size() * training_data[0].size() * sizeof(float) << endl;

    training_data = load_training_data();
}

vector< vector<float> > Runner::load_training_data()
{
    ifstream c("test1.txt");
    //
    // Put something here to print an error message
    // and, maybe, bail out if the file can't be opened
    //
    float abc;

    vector< vector<float> > t_data;
    vector<float> def;

    cout << endl
         << "In load_training_data(): num_trainings = " << num_trainings
         << ", total = " << total 
         << endl;

    for(int i = 0; i < num_trainings; i++) {
        for(int j = 0; j < total; j++){
            c >> abc;
            // Put something here to print a message and bail out if
            // the file fails to read the data item
            def.push_back(abc);
        }
        t_data.push_back(def);
        def.clear();
    }
    cout << "t_data.size()*sizeof(float) = " << t_data.size()*sizeof(float) 
         << endl 
         << "Leaving load_data()"
         << endl;

    return t_data;
}
CPP / C++ / C Code:
//Trainer.h
#ifndef TRAINER_H__
#define TRAINER_H__
#include <fstream>
using std::ofstream;
#include "Runner.h"


class Trainer {
  public:
    Trainer(int, int, int, int, int);
    int get_total(){return total;};
    

  private:
    vector<vector<float> > sig_data;             
    int population_size;     
    int num_generations;     //number of generations
    Runner g;      
    int num_trainings;       //number of training pairs
    int num_gates;           //number of gate variables
    int num_models;          //number of models
    void save_pop();
    vector<vector<float> > load_pop();
    vector<float> mutcross_sig;
    void mutcross();
    int top;
    int total;
    float gen_avg;
    int gen_best;
    int min_value, max_value;
};
#endif
CPP / C++ / C Code:
//Trainer.cpp
#include <iostream>
#include "Trainer.h"

using namespace std;

Trainer::Trainer(int pop_size, int num_gen, int n_trainings, int n_gates,
                 int n_models)
{
    cout << endl
         << "Trainer constructor:" << endl
         << "pop_size    = " << pop_size << endl
         << "num_gen     = " << num_gen << endl
         << "n_trainings = " << n_trainings << endl
         << "n_gates     = " << n_gates << endl
         << "n_models    = " << n_models << endl;

    total = n_gates + 1;
    cout << "Trainer constructor: total = " << total
         << ", allocating storage for " << total-1
         << " elements" << endl;
    //
    // Note that variable-size arrays are not allowed in
    // ISO C++
    // Why the heck don't you use a vector or something?
    //
    float t_sig[total - 1];
    top = 1000;
    char savefile[40];
    float holdB;
    int nogo = 0;

    //set the parameters
    population_size = pop_size;
    num_generations = num_gen;
    num_trainings   = n_trainings;
    num_gates       = n_gates;
    num_models      = n_models;
    //cout << "Enter file name for saving the data: ";
    //cin >> savefile;
    //pop.open(savefile);
    //gen_report.open("abctre4.txt");

    cout << "Creating runner(" << num_gates
         << "," << num_models
         << "," << num_trainings
         << ")"
         << endl;
    g = Runner(num_gates, num_models, num_trainings);


    cout << "Back from creating runner, pop_size = " << pop_size
         << ", num_gates = " << num_gates
         << endl;

    cout << "Number of data bytes required for sig_data = "
         << pop_size*(num_gates+1) * sizeof(float) << endl;
    for (int t = 0; t < pop_size; t++) {
        sig_data.push_back(vector < float >());
    }
    for (int y = 0; y < pop_size; y++) {
        for (int j = 0; j < num_gates; j++) {
            //
            // random(int, int) is not a standard library function
            // For test purposes, we don't even need it
            //holdB = random(min_value, max_value);
            // Just for kicks, put something random in here
            //
            holdB = min_value + (max_value-min_value)*rand()/(RAND_MAX+1.0);
            try {
                sig_data[y].push_back(holdB);
            }
            catch(bad_alloc) {
                cout << " MEMLIMIT ";
                try {
                    sig_data[y].push_back(holdB);
                }
                catch(bad_alloc) {
                    cout << " MEMLIMIT_AGAIN ";
                    nogo = 1;
                }
            }
            if (j >= total-1) {
                cout << "A grievous error is about to occur"
                     << ", storing tsig[" << j << "]" << endl;
            }
            t_sig[j] = holdB;
        }

        sig_data[y].push_back(holdB);
        if (holdB <= top) {
            gen_best = y;
            top = holdB;
        }

        if (nogo == 0) {
            gen_avg = gen_avg + holdB;
        }
        else {
            gen_avg = gen_avg + (gen_avg / y + 1);
            nogo = 0;
        }
    }
    cout << endl
         << "sig_data.size()= " << sig_data.size() << endl
         << "sig_data[0].size()= " << sig_data[0].size() << endl
         << "Number of data bytes in sig_data = "
         << sig_data[0].size()*sig_data.size() * sizeof(float) << endl;

    for (int x = 0; x < num_gates; x++) {
        try {
            mutcross_sig.push_back(0);
        }
        catch(bad_alloc) {
            cout << " MUTMEMLIMIT ";
            try {
                mutcross_sig.push_back(0);
            }
            catch(bad_alloc) {
                cout << " MUTMEMLIMIT_AGAIN ";
            }
        }
    }
    cout << "mutcross_sig.size() * sizeof(float) = "
         << mutcross_sig.size() * sizeof(float)
         << endl
         << "Leaving Trainer constructor" << endl << endl;
}


CPP / C++ / C Code:
//main.cpp
#include <iostream>
#include "Trainer.h"
#include "Runner.h"

using namespace std;

int main()
{
    Trainer trainer(10000, 10000, 10000, 10000, 10000);
    cout << "Total = " << trainer.get_total() << endl;
    return 0;
}

The idea isn't to get something useful, but to see if adjusting the sizes gives reasonable values for total memory required.

Code:
Trainer constructor: pop_size = 10000 num_gen = 10000 n_trainings = 10000 n_gates = 10000 n_models = 10000 Trainer constructor: total = 10001, allocating storage for 10000 elements Creating runner(10000,10000,10000) Runner constructor: n_gates = 10000, n_models = 10000, n_trainings = 10000 Will allocate 800040000 bytes for training_data training_data.size() * training_data[0].size * sizeof(float) = 800040000 In load_training_data(): num_trainings = 10000, total = 20001 t_data.size()*sizeof(float) = 40000 pop_size = 10000, num_gates = 10000 Number of data bytes required for sig_data = 400040000 sig_data.size()= 10000 sig_data[0].size()= 10001 Number of data bytes in sig_data = 400040000 mutcross_sig.size() * sizeof(float) = 40000 Total = 10001

With a program like you described (not a lot of de-allocation and re-allocation) array and vector sizes containing a couple of GigaBytes should be possible with any reasonable compiler and operating system.

A final note: Sometimes allocation fails not because the program actually asks for too much memory, but because someplace in the program data was written beyond the end of allocated storage. Note, particularly, that the [] index operator for vectors does not check for bounds violations. If somewhere (in the hidden part of your program) you stored something illegally, then School is Out. Undefined behavior can cause unpredictable and hard-to-duplicate (not to mention hard-to-find) errors.

Regards,

Dave
  #3  
Old 14-Feb-2009, 15:44
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 157
dlp has a spectacular aura about

Re: 2D Vector Memory Problems


One way to help with bounds checking is to use the at() method for vectors. It works just like the [] operator but it throws an out_of_range exception. Like so (uncompiled, untested):
CPP / C++ / C Code:
#include <iostream>
#include <vector>
#include <exception>
using namespace std;

int main()
{
    vector<int> v;

    for(int i = 0; i < 10; i++)
        v.push_back(i);

    // bounds too far, tries to read anyway
    for(int i = 0; i <= 10; i++)
        cout << v[i] << endl;

    try {
    // bounds too far, exception is thrown
    for(int i = 0; i <= 10; i++)
        cout << v.at(i) << endl;
    }
    catch(exception & e) {
        cout << e.what() << endl;
    }
}
  #4  
Old 15-Feb-2009, 03:18
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: 2D Vector Memory Problems


Another approach is declare 2D vector with predefined row and resize it using resize().
  #5  
Old 15-Feb-2009, 23:36
Omega037 Omega037 is offline
New Member
 
Join Date: Feb 2009
Posts: 5
Omega037 is on a distinguished road

Re: 2D Vector Memory Problems


At lower amounts it works, but at Trainer trainer(10000, 10000, 10000, 10000, 10000) the program crashes on me.
  #6  
Old 16-Feb-2009, 00:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: 2D Vector Memory Problems


Quote:
Originally Posted by Omega037
At lower amounts it works, but at Trainer trainer(10000, 10000, 10000, 10000, 10000) the program crashes on me.
What compiler? What operating system.

As I browse the results of my simplified test programs I see a total of something on the order of 2 GBytes in the data part of the various vectors for those values. Are you using my example, or have you added some stuff?

On my 32-bit Linux systems with GNU compilers, program and data memory can approach 3 GBytes. I feel that is probably true on Debian, since it also uses GNU compilers. Addressable memory on 64-bit Linux systems is a lot more, but I can't recall off of the top of my head what I found the limits to be when I tested it. (GNU compilers on Windows xp have a limit that is closer to 2 GBytes, as I recall).

If your program needs more memory than can be addressed by your compiler and operating system, and you don't need much more than a couple of GBytes then you can consider:

1. Get another compiler and/or operating system

or

2. Reformulate the problem in such a way that not everything needs to be in memory at the same time. This may not be easy, and, depending on the problem, may not be possible without making run time excessive.

or

3. All of the above

or

4. Something else.


Bottom line: Something that they don't necessarily teach in programming classes (at least not in the beginning) is to estimate the resource requirements before you start writing code. I used the program to tell me, but a careful (manual) analysis could be at least as valuable.

Regards,

Dave
  #7  
Old 16-Feb-2009, 01:08
Omega037 Omega037 is offline
New Member
 
Join Date: Feb 2009
Posts: 5
Omega037 is on a distinguished road

Re: 2D Vector Memory Problems


I tried with both Dev-C++(Bloodshed) which uses Mingw on Windows Vista with a 64-bit Processor AND I tried it on a 32-bit processor machine running Debian Linux using g++.
  #8  
Old 18-Feb-2009, 01:58
Omega037 Omega037 is offline
New Member
 
Join Date: Feb 2009
Posts: 5
Omega037 is on a distinguished road

Re: 2D Vector Memory Problems


I decided to go back to 2D dynamic arrays and have replaced the proprietary stuff with a simple mathematical equation. This should compile fine and if you run it a couple times, it will crash. Please help if you can!

Project.cpp
CPP / C++ / C Code:
#include "Trainer.h"


int main(int argc, char * argv[])

{
   int n_gates=14;
   int n_models=3;
   int n_trainings=89;

   int pop_size=500;                 
   int num_gen=100;                 
   float min=0;
   float max=50;
   float cross_factor=.5;               
   float mut_factor=.5;
   

    Trainer d(pop_size, num_gen, min, max, cross_factor, mut_factor, n_trainings, n_gates, n_models);
     
     d.run();

}  

Trainer.cpp
CPP / C++ / C Code:

#include "Trainer.h"
Trainer::Trainer(int pop_size, int num_gen, float min, float max, float cross_factor, float mut_factor, 
int n_trainings, int n_gates, int n_models){
      
      


     total = n_gates +1;  
     float t_sig[total-1];

     top=1000;
   gen_best=0;
   gen_avg=0;
   
   
   char savefile[40];         
   float holdB;
      int nogo=0;
   //set the parameters
   population_size = pop_size;
   num_generations = num_gen;
   min_value = min;
   max_value = max;   
   crossover_factor = cross_factor;
   mutation_factor = mut_factor;

   
   cout <<"Save To Filename" << endl;
   cin >> savefile; 
   pop.open(savefile);
  gen_report.open("abctre4.txt");
   //set the Runner and training data
   num_trainings = n_trainings;
   num_gates = n_gates;
   num_models = n_models;

g = Runner(num_gates, num_models, num_trainings);

try{
   sig_data= (float **) malloc (pop_size * sizeof(float *));
   for(int i=0;i<pop_size;i++){
           sig_data[i] = (float *) malloc (total * sizeof(float));
           }
}
catch(bad_alloc){
                 "SIGNOOOO";
                 }       




   initialize_random_generator(); 
   

  
     
           for(int y=0; y < pop_size; y++){
                 for(int j=0; j < num_gates; j++){
                      holdB=random(min_value, max_value);
                      t_sig[j]=holdB;
                      sig_data[y][j] = holdB;   
                 }
                

                 g.setparameters(t_sig);

                 
                 holdB=g.crossvalidation_run(0);


                 sig_data[y][total-1] = holdB;
                                    
                 if(holdB <= top){
                    gen_best = y;
                    top = holdB;
                    }   
                 gen_avg = gen_avg + holdB;                                      
                 }

          

}
/***************************************************************/

void Trainer::save_pop(){




     cout << " SAVING ";
     cout << "SAVE STREAM OPEN ";
     for(int v=0; v < population_size; v++){
              for(int f=0; f < total; f++){
              pop << sig_data[v][f] << " ";
              }
              pop << endl;
     cout << "SAVED";
     }
}

/****************************************************************************/

Trainer::~Trainer()
{
 delete sig_data;
}

/****************************************************************************/

void Trainer::run(){
     

     mut_data = (float *) malloc(total*sizeof(float));

   float replace = 1000;
    cout << "GEN: 0 " << endl;
    report(0, gen_best, gen_avg);

   for(int q=1; q < num_generations+1; q++){
           gen_best=0;
           gen_avg=0;
           cout << "GEN:" << q << endl;
                for(int i=0; i < population_size; i++){
                        mutcross(i);                    
                        g.setparameters(mut_data);                     
                        replace = g.crossvalidation_run(0);
                        
                        if(sig_data[i][num_gates] > replace){
                                                  cout << "MUT" << endl;
                                   for(int p=0; p < num_gates; p++){
                                           sig_data[i][p] = mut_data[p];
                                                  }
                                   sig_data[i][num_gates] = replace;
                                   }
                        if(sig_data[i][num_gates] < sig_data[gen_best][num_gates]){
                                                  gen_best = i;
                                                  cout << "BEST SWITCH: " << gen_best << endl;                                                  
                                                  }
                        gen_avg = gen_avg + sig_data[i][num_gates];
                        
                        }
                
                        
                report(q, gen_best, gen_avg);
                }
   save_pop();             
   gen_report.close();

}


/****************************************************************************/

void Trainer::mutcross(int cur){
      int base, diff1, diff2;
      float x=0;
      float y=1;   

   
      random(0, population_size-1, cur, base, diff1, diff2);
      for(int r=0; r < num_gates; r++){
	     if(random(x, y) < crossover_factor){
               mut_data[r] = sig_data[cur][r];
		}
		else{
		mut_data[r] = sig_data[base][r] + (mutation_factor * (sig_data[diff1][r] - sig_data[diff2][r]));
               }
	}


}

/****************************************************************************/

void Trainer::report(int gen, int best, float avg){


     float outAz=0;
     float temp_sig[num_gates];

     if(gen == 0){
            gen_report << "Gen     Avg-mse     Smallest-mse     Az" << endl;
            }
     for(int s=0; s < num_gates; s++){     
              temp_sig[s] = sig_data[best][s];
              }
              
     g.setparameters(temp_sig);

     outAz = g.crossvalidation_run(1);       
     
     gen_report << setw(3) << gen << "  ";
          
     gen_report << setw(10) << (avg / population_size) << "  ";
          
     gen_report << setw(10) << sig_data[best][num_gates] << "  ";
          
     gen_report << setw(10) << outAz << endl;
     
      gen_report << setw(10) << "BEST: " << best << endl;
     
}

/****************************************************************************/

void Trainer::initialize_random_generator()
{
   for (unsigned long i = 0; i < 500000; i++) ; //start the system clock
   srand((unsigned)time(NULL));                  //initialize random seed 
}

/****************************************************************************/

float Trainer::random()
{
   return rand()/(float)32767;          //32767 is the maximum random integer
}

/****************************************************************************/

float Trainer::random(float low, float high)
{
   return (low + (high - low)*random());
}

/****************************************************************************/

int Trainer::random(int low, int high)
{
   return (low + (int)((high - low + 1) * random()));
}

/***************************************************************************/

int Trainer::random(int low, int high, int exclude, int &one, int &two, int &three)
{
   do
   {
      one = random((int)low, (int)high);   
   }
   while (one == exclude);

   do
   {
      two = random((int)low, (int)high);   
   }
   while (two == exclude || two == one);

   do
   {
      three = random((int)low, (int)high);   
   }
   while (three == exclude || three == one || three == two);
   
   return 0;			
}


Trainer.h
CPP / C++ / C Code:

#include "Runner.h"


class Trainer {

private:
                   
   float **sig_data;

   int population_size;     //size of population
   int num_generations;     //number of generations
   float min_value;         //minimum string value
   float max_value;         //maximum string value
   float crossover_factor;  //crossover factor
   float mutation_factor;   //mutation factor



   ofstream pop;   
   ofstream gen_report;
   
   float top;
   int gen_best;
   float gen_avg;
   int total;
   Runner g;          //Runner Project

   int num_trainings;       
   int num_gates;          
   int num_models;         

   void save_pop();
   void load_pop();
   void initialize_random_generator(); //initializes the random seed
   float random();                     //returns a real between 0 and 1
   float random(float low, float high);//returns a real between low and high
   int random(int low, int high);      //returns an int between low and high
   int random(int low, int high, int exclude, int &one, int &two, int &three);
                                       //returns three integers beween low and
   float *mut_data;                                       //high excluding exclude
   void mutcross(int cur);


public:
       

       
   Trainer(int pop_size, int num_gen, float min, float max, float cross_factor, float mut_factor, int n_trainings, int n_gates, int n_models);   //constructor
       
   ~Trainer();                                  //destructor



   void run();    //generates successive generations

   void report(int gen, int best, float avg);
};


Runner.cpp
CPP / C++ / C Code:

#include "Runner.h"

Runner::Runner(){         

                           }

Runner::Runner(int n_gates, int n_models, int n_trainings){

   num_gates = n_gates;
   num_models = n_models;
   num_trainings = n_trainings;

   total = n_gates + n_models + 1;
try{
   training_data = (float **) malloc (n_trainings * sizeof (float *));
   for(int i=0;i<n_trainings;i++){
           training_data[i] = (float *) malloc (total * sizeof(float));
           }}
           catch(bad_alloc){
                            
                            "WHOOPS";}



   load_training_data();



   parameters = (float *) malloc (num_gates * sizeof(float));             
   errors = (float *) malloc (num_gates * sizeof(float));              
   weights = (float *) malloc (num_gates * sizeof(float));          


}



/**************************************************************************/

Runner::~Runner()
{

    //delete []parameters;
	delete []errors;
   delete []weights;

       
   

}

    

/**************************************************************************/

float Runner::compute_output(int exclude)
{

float temp=0;
         temp = training_data[exclude][num_gates + num_models] - training_data[exclude][num_gates]/parameters[exclude];
 


   return temp;
   

}


/***************************************************************************/

void Runner::load_training_data()
{

   ifstream c("abc.txt");
  

      for(int i = 0; i < num_trainings; i++){
		for(int j = 0; j < num_gates + num_models + 1; j++){
        c >> training_data[i][j];
        }
     }

}


/***************************************************************************/


void Runner::setparameters(float *sig){
     
      for (int i = 0; i < num_gates; i++)
      {
       parameters[i] = sig[i];
      }
     }

/***************************************************************************/



float Runner::getAz(float *Z_pred, float *Z_actual, int NumTrain){

       
     
       float Az = 0.0; 


//*** Calculate Hit Rate and FAR


      int maxbinsize = NumTrain;

      int numofcuts = 40;
      int cutoff=0;
      float truepos=0;
      float trueneg=0;
      float falsepos=0;
      float falseneg=0;
      float ROCStore[numofcuts+1][2];
      float cutsize=0;
      float delta = 20;


      while(cutsize <= 1){
                    
      truepos=0;
      trueneg=0;
      falsepos=0;
      falseneg=0;
      

//We find the False Positive, True Positive, False Negative, and True Negative for each cutoff point, in this case increments of .05 from -1 to 1      
      for(int p = 0; p < maxbinsize; p++){

            if (Z_actual[p] == 1){
                 if(Z_pred[p] > cutsize){
                              truepos++;
                              }           
                 else if(Z_pred[p] < cutsize){
                              falseneg++;
                              }
                 else if(Z_pred[p] == cutsize){ //err on side of positive
                              truepos++;
                              }                               
                 }     
             else if (Z_actual[p] == -1){
                 if(Z_pred[p] > cutsize){
                              falsepos++;
                              }           
                 else if(Z_pred[p] < cutsize){
                              trueneg++;
                              }                            
                 else if(Z_pred[p] == cutsize){ //err on side of positive
                              falsepos++;
                              }           
                 }
                                                            
       }
       
      
                                
       float sensitivity=0;
       float specificity=0;
       float FAR=0; //False Alarm Rate   
       
       
                 

//We find the sensitivities and specificities for each cutoff point and then put them into an array
       if((truepos + falseneg) > 0){
                  sensitivity = (truepos / (truepos + falseneg));   
                                                                      
            }
       else{
                  sensitivity = 1;    
                  }
				  
       if((trueneg + falsepos) > 0){
                  
                  specificity = (trueneg / (trueneg + falsepos));                                                                 
            }
       else{
                  specificity = 1;    
                  } 

       FAR = 1-specificity;                   
       ROCStore[cutoff][1] = sensitivity;                   
       ROCStore[cutoff][0] = FAR;
                
                
                         
       cutoff++;
       cutsize = (cutoff / delta) - 1;    

       }            



       Az=0;
       int notset=1;
       int partAzStart=0;
       float getslope=0;


//Sort by FAR

       int sortB;
       float index=0;
       float indexB=0;       
       
       for(int sortA=1; sortA <= numofcuts+1; sortA++){
               
       index = ROCStore[sortA][1];
       indexB = ROCStore[sortA][0];  
           
       sortB = sortA;
       
       while ((sortB > 0) && (ROCStore[sortB-1][0] >= indexB))
       {
             ROCStore[sortB][1] = ROCStore[sortB-1][1];
             ROCStore[sortB][0] = ROCStore[sortB-1][0];
             sortB = sortB-1;
             }
             
       ROCStore[sortB][1] = index;      
       ROCStore[sortB][0] = indexB;
                                 
       }
// End Sort

 
       for(int h=0; h < numofcuts+1; h++){
              
       Az = Az + (((ROCStore[h+1][0] - ROCStore[h][0])*(ROCStore[h][1] + ROCStore[h+1][1]))/2);
       }
       
//  Partial Az disabled to save computation time
/*          if (ROCStore[h+1][1]>.9 && notset == 1){
                partAzStart=h;
                notset=0;
                }
                }

                
       float partROC[numofcuts-partAzStart][2];
       
       getslope = ((ROCStore[partAzStart+1][1]-ROCStore[partAzStart][1])/(ROCStore[partAzStart+1][0]-ROCStore[partAzStart][0]));
       partROC[0][0] = ROCStore[partAzStart][0] + ((.9-ROCStore[partAzStart][1])/getslope);
       partROC[0][1] = .9;
       
       for(int q=0; q < (numofcuts - partAzStart); q++){
               partROC[q][0] = ROCStore[partAzStart+q][0];             
               partROC[q][1] = ROCStore[partAzStart+q][1];
               }    
     
       for(int r=0; r < (numofcuts - partAzStart - 1); r++){

               partAz = partAz + ((partROC[r+1][0] - partROC[r][0]) * ((partROC[r][1] + partROC[r+1][1])/2));
               }           
  */

  if(Az > 1){
        return 0;
        }
  return Az;

}



/***************************************************************************/
/****** 1-holdout CrossValidation -- By Clayton T. Morrison - 5/14/99 *******/

float Runner::crossvalidation_run(int doAz)

{

	float predict[num_trainings];
	float actual[num_trainings];
	float perform;
	

	for(int i=0; i < num_trainings; i++)
	        {                 
                 predict[i] = compute_output(i);    
                 actual[i] = training_data[i][num_gates + num_models];
                 }   
	
    if (doAz == 0){
    	      perform = getMSE(predict, actual, num_trainings);
        }
    else{
	     perform = getAz(predict, actual, num_trainings);
       }
       
	return perform;
	

} // end 1-holdout CrossValidation method

float Runner::getMSE(float *Z_pred, float *Z_actual, int NumTrain){
     
       float MSE = 0.0; 
       float error = 0.0;
       
       
       for(int i=0; i < NumTrain; i++){
               error = error + ((Z_actual[i]-Z_pred[i]) * (Z_actual[i]-Z_pred[i])); 
               }

       return (error / NumTrain);
                     
       
}


Runner.h
CPP / C++ / C Code:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string.h>
#include <time.h>
#include <math.h>


using namespace std;

class Runner {

private:
   int num_gates;                      
   int num_models;                     
   int num_trainings;                  

   float *parameters;                     
   float *errors;                      
   float *weights;                     
 int total;
                
  float** training_data; 



public:

       Runner();
   Runner(int n_gates, int n_models, int n_trainings);             
   ~Runner();                                     

   float compute_output(int exclude);
   
   void load_training_data();	
   
   float crossvalidation_run(int doAz);

   void setparameters(float * sig);
   float getMSE(float *Z_pred, float *Z_actual, int NumTrain);
                                                   
   float getAz(float *Z_pred, float *Z_actual, int NumTrain);
    
                                         
};


And the following is the input file with the data randomized:

I-Oracle-View1.txt
Code:
-0.930901 -0.339333 0.444903 0.403394 -0.149324 -0.300418 -0.923674 -0.672967 0.101429 -0.018499 0.144202 -0.331944 1.0 -0.4944 0.429444 0.184994 -0.39918 1.0 -0.391909 -0.209031 0.994949 -0.414993 -0.018991 -0.931944 -0.904902 -0.224418 1.0 0.067444 0.367189 -0.219183 1.0 -0.499 0.240044 0.189904 -0.399394 1.0 -0.919093 -0.267442 0.329014 -0.039239 0.019491 -0.999944 -0.923674 -0.218233 0.136719 -0.029939 0.118182 -0.219183 1.0 -0.091899 0.490442 0.421844 -0.39991 1.0 -0.441893 -0.993994 0.444129 -0.039239 0.019333 -0.914941 -0.492939 -0.967931 0.113944 -0.131418 0.181393 -0.493184 1.0 0.419999 0.449244 0.493184 -0.399444 1.0 -0.294909 -0.493332 0.329014 -0.118444 0.111413 -0.419418 -0.267094 -0.144449 0.230442 0.191867 0.202449 -0.093331 1.0 -0.3367 0.183904 0.422 -0.40401 1.0 -0.949949 -0.909118 -0.136724 0.333942 -0.674424 -0.390418 -0.923674 -0.902999 0.191494 -0.093044 0.101444 -0.331801 1.0 -0.9494 0.424194 0.429944 -0.399494 1.0 -0.294909 -0.912367 -0.119494 -0.144294 -0.318401 -0.292999 -0.181422 -0.991103 0.149183 0.399933 0.02304 -0.244184 1.0 0.139999 0.418424 0.494442 -0.399302 1.0 -0.929111 -0.449449 -0.021844 0.129214 -0.324181 -0.323944 -0.933319 -0.414418 0.413671 -0.67067 0.199904 -0.993032 1.0 0.094 0.492493 0.494414 -0.399232 1.0 -0.921679 -0.18329 0.141867 0.190339 -0.444404 -0.67499 -0.994413 -0.671894 0.418033 -0.441118 -0.167184 -0.394203 1.0 -0.9494 0.490904 0.181244 -0.399183 1.0 -0.913913 -0.949333 -0.404412 0.193039 -0.440949 -0.240049 -0.41333 -0.23293 -0.101429 0.424494 0.429942 -0.924184 1.0 0.094 0.42201 0.423913 -0.399182 1.0 -1.0 -0.990423 -0.183189 0.932919 -0.192324 -0.409418 -1.0 -0.423674 -0.090267 -0.924444 0.136704 -0.321892 0.999999 -0.3672 0.184142 0.449449 -0.401921 -1.0 -0.496741 -0.941832 0.122193 -0.444182 0.196794 -0.331324 -0.499671 -0.424449 -0.679113 -0.131418 0.944424 -0.339392 1.0 0.419999 0.239393 0.189444 -0.409094 -1.0 -0.294441 -0.901819 -0.021844 -0.449932 -0.670418 -0.929391 -0.18444 -0.242093 -0.090267 0.913674 0.909167 -0.99444 1.0 0.139999 0.39402 0.418944 -0.39933 -1.0 -0.241818 -0.949133 0.018418 -0.67914 -0.418493 -0.990339 -0.679303 -0.493144 -0.404931 -0.332194 0.491833 -0.33132 1.0 0.494 0.940933 0.493044 -0.400291 -1.0 -0.921679 -0.403239 -0.141142 0.996791 -0.144167 -0.299999 -0.929399 -0.33244 0.418444 0.191849 0.442409 -0.201841 1.0 -0.019999 0.181802 0.429242 -0.39994 -1.0 -0.241818 -0.33332 -0.418318 0.418411 -0.310999 -0.442392 -0.391042 -0.4493 -0.049312 -0.494093 0.049442 -0.332404 1.0 0.04 0.444412 0.491832 -0.39939 -1.0 -0.399418 -0.418444 0.301404 -0.333942 0.109999 -0.399933 -0.418004 -0.67067 0.419149 -0.041893 0.994067 -0.411813 1.0 0.094 0.399933 0.181044 -0.400492 -1.0 -0.391909 -0.394412 -0.131894 -0.423314 -0.299322 -0.916749 -0.91944 -0.923267 -0.440902 -0.396793 -0.132944 -0.944124 0.999999 0.367 0.099184 0.32142 -0.400399 -1.0 -0.913913 -0.949333 -0.404412 0.193039 -0.440949 -0.240049 -0.41333 -0.23293 -0.101429 0.424494 0.429942 -0.924184 1.0 0.094 0.424094 0.429929 -0.399367 1.0 -0.182418 -0.339333 -0.021844 0.184449 -0.672999 -0.679302 -0.941891 -0.679914 0.101429 0.019018 0.141867 -0.224049 1.0 0.218999 0.429409 0.41841 -0.399304 1.0 -0.336791 -0.49204 0.118094 0.031442 0.140302 -0.411233 -0.444914 -0.674189 0.014294 -0.67067 0.244499 -0.679318 1.0 0.449999 0.424404 0.441444 -0.400167 1.0 -0.239239 -0.944092 0.094991 -0.449418 -0.181422 -0.244239 -0.189294 -0.944202 0.409023 0.141249 0.044494 -0.249931 1.0 -0.1367 0.394424 0.423312 -0.39924 1.0 -0.402949 -0.339333 -0.167044 0.499119 -0.491918 -0.449903 -0.449393 -0.24929 -0.440902 -0.139304 0.003367 -0.312418 1.0 0.133999 0.421894 0.422444 -0.399184 1.0 -0.496793 0.044412 0.679933 -0.418014 0.416714 0.118132 -0.902923 0.333167 -0.033967 0.186703 0.189292 0.996742 1.0 0.941999 0.180444 0.499249 -0.401893 1.0 -0.496741 -0.67167 -0.119494 0.140421 -0.444414 -0.67121 -0.918932 -0.316701 0.440902 0.033999 0.167444 -0.993032 1.0 0.419999 0.426794 0.499394 -0.399224 1.0 -0.421849 -0.67044 -0.120923 0.101849 -0.367189 -0.330449 -0.939933 -0.294018 0.418444 -0.409304 0.116744 -0.249674 1.0 0.449999 0.499914 0.41223 -0.399224 1.0 -0.921679 -0.390492 0.412901 0.101849 -0.40944 -0.239219 -0.940901 -0.332923 0.409023 -0.031314 0.332919 -0.267184 1.0 -0.091899 0.429924 0.424014 -0.399294 1.0 -0.414231 -0.33994 -0.44903 0.333333 -0.310414 -0.301674 -0.184429 -0.232444 -0.026791 0.144924 0.318139 -0.91824 1.0 0.494 0.180944 0.423314 -0.399444 1.0 -0.679679 -0.293223 -0.032219 -0.184233 -0.490492 -0.244923 -0.324404 -0.301842 -0.026791 0.01093 0.449418 -0.294418 1.0 0.449999 0.392944 0.411183 -0.399314 -1.0 -0.913913 -0.14131 0.094991 -0.419967 0.194491 0.006749 -0.671849 0.049991 0.419149 0.331918 0.399999 0.670067 0.999999 0.941999 0.49313 0.313674 -0.400267 -1.0 -0.223993 -0.416794 -0.314232 0.184402 -0.239019 -0.906703 -0.399444 -0.449149 -0.312499 -0.191867 -0.119494 -0.181367 1.0 0.04 0.184444 0.940093 -0.401024 -1.0 -0.218393 -0.903999 -0.149499 -0.014423 -0.329418 -0.309412 -0.949418 0.318167 -0.090267 0.991804 -0.410393 -0.241894 1.0 -0.091899 0.401894 0.429242 -0.401332 -1.0 -0.674149 -0.493339 0.014044 -0.167004 0.139213 -0.31867 -0.499424 -0.493184 0.671367 1.0 0.900444 -0.674418 1.0 0.92 0.429442 0.24404 -0.399294 -1.0 -0.499213 -0.18401 -0.670441 0.291991 -0.441911 -0.40449 -0.941891 -0.419993 -0.419149 0.321944 0.044944 -0.671044 1.0 0.92 0.184114 0.418492 -0.401893 -1.0 -0.182418 -0.303318 0.14232 0.41167 -0.124401 -0.209149 -0.912184 -0.940299 0.491139 -0.399933 0.189302 -0.184449 1.0 -0.1324 0.429944 0.429674 -0.400679 -1.0 -0.918011 -0.239679 -0.180949 -0.944932 -0.214094 -0.403131 -0.180018 -0.67167 0. -0.674204 -0.013114 -0.318133 1.0 0.04 -0.167444 0.411812 -0.39929 -1.0 -0.903339 -0.913202 -0.122193 0.214444 -0.183194 -0.494942 -0.931814 -0.423672 0. -0.141839 0.092367 -0.672114 1.0 0.139999 0.184944 0.424424 -0.399674 -1.0 -0.496741 -0.414267 -0.186704 0.044049 -0.440044 -0.441123 -0.918039 -0.419239 -0.13944 0.367189 -0.022994 -0.41942 1.0 0.419999 0.426794 0.42933 -0.400022 1.0 -0.336791 -0.233672 -0.314232 0.403918 -0.490994 -0.314918 -0.180018 -0.41214 -0.184441 0.194924 0.126714 -0.902674 1.0 0.133999 0.424944 0.180942 -0.399942 1.0 -0.993223 -0.67967 0.191822 0.167039 -0.004022 -0.924182 -0.96793 -0.679324 0.410142 -0.191867 0.426741 -0.493094 1.0 0.133999 0.42304 0.184494 -0.399294 1.0 -0.903339 -0.413267 0.004412 0.419967 -0.674144 -0.41404 -0.994242 -0.671671 -0.014294 0.03994 -0.04424 -0.394029 1.0 0.133999 0.42293 0.180123 -0.399679 1.0 -0.930901 -1.0 -0.672184 1.0 -0.404939 -0.911844 -0.904902 -0.423672 0.142944 0.103994 -0.399413 -0.186718 1.0 0.924 0.182182 0.429182 -0.400444 1.0 -0.967418 -0.494144 -0.244129 0.292144 -0.939039 -0.393939 -0.96793 -0.367167 -0.394409 -0.189318 0.441919 -0.999499 1.0 0.367 0.429184 0.444918 -0.399442 1.0 -0.449999 -0.31814 0.4 -0.132424 -0.444494 -0.311829 -0.904902 -0.674194 0.493124 -0.131418 0.044967 -0.303239 1.0 -0.3367 0.442249 0.180444 -0.399182 1.0 -0.441893 -0.311824 -0.311809 -0.024949 -0.183193 -0.339333 -0.941891 -0.670394 -0.444101 0.094494 -0.04441 -0.332404 1.0 -0.3244 0.444993 0.923182 -0.406729 1.0 0.944993 1.0 1.0 -1.0 0.999999 0.999999 -0.399331 0.494129 -0.124992 0.1849 0.9911 0.918184 1.0 1.0 -0.01893 0.493024 -0.967993 1.0 -0.944401 -0.919494 -0.030444 -0.102914 -0.442991 -0.918444 -0.93103 -0.330991 0.679113 0.090909 0.314429 -0.189932 1.0 0.411999 0.449391 0.441844 -0.399224 -1.0 -0.391909 -0.218209 0.004412 0.118249 -0.404401 -0.242394 -0.918932 -0.244424 0.491139 0.031314 0.321803 -0.992909 0.999999 -1.0 0.401444 0.184444 -0.409139 -1.0 -0.921679 -0.939049 0.030949 0.41319 -0.367139 -0.41404 -0.918039 -0.339299 0.339672 0.02131 0.092184 -0.394413 1.0 -0.1324 0.181124 0.18124 -0.399242 -1.0 -0.314449 -0.367413 0.367218 -0.220333 -0.09184 -0.31404 -0.323181 0.003144 0.409023 0.133194 0.449912 -0.041244 1.0 0.324 0.494182 0.318144 -0.400018 -1.0 -0.679133 -0.32671 0.679919 -0.679911 0.144013 -0.301844 -0.49419 -0.93067 0.267911 0.329967 0.413114 -0.183299 1.0 -0.1324 0.419449 0.189167 -0.399912 -1.0 -0.239239 -0.931493 -0.32367 -0.311824 -0.944183 -0.244094 -0.444939 -0.999322 0. 0.01093 0.914442 -0.49324 1.0 0.419999 0.671267 0.444 -0.399232 -1.0 -0.318318 -0.679671 -0.999999 0.321002 -0.410319 -0.299679 -0.449944 -0.216744 0.039949 0.031314 0.329441 -0.232323 1.0 0.449999 0.186744 0.449944 -0.400019 -1.0 -0.993223 -0.911423 -0.311809 0.913242 -0.967449 -0.440313 -0.933319 -0.183182 0.429444 0.67067 -0.491183 -0.449903 1.0 -0.6794 0.429303 0.391184 -0.399924 -1.0 -0.249318 -0.414442 0.04129 -0.039239 0.094493 -0.39067 -0.414941 -0.990018 0.090267 0.181494 0.679331 -0.492094 1.0 -0.2067 0.429184 0.186744 -0.400392 -1.0 -0.967418 -0.496704 -0.670441 0.493224 -0.267218 -0.49441 -0.918442 -0.490018 -0.090267 -0.018444 -0.193092 -0.429333 1.0 -0.9494 0.421842 0.204167 -0.39918 1.0 -0.182418 -0.418367 -0.122193 0.129411 -0.499267 -0.49118 -0.918932 -0.184249 0.418444 0.040129 -0.044904 -0.189403 1.0 0.133999 0.429967 0.426718 -0.399267 1.0 -0.999449 -0.930413 0.672184 0.049993 0.391804 -0.493914 -0.309194 -0.31809 0.421894 0.218267 0.49444 -0.189044 1.0 -0.3367 0.499409 0.429323 -0.402949 1.0 -0.994118 -0.391891 -0.109499 -0.181804 -0.002493 -0.406703 -0.449944 -0.942901 -0.396724 -0.118967 0.218041 -0.444418 0.999999 0.133999 0.184067 0.420424 -0.402318 1.0 -0.241818 -0.494232 -0.019314 0.441814 0.141184 -0.132333 -0.249182 0.671182 -0.101429 0.139189 0.94241 0.394029 1.0 0.324 0.42939 0.424967 -0.400494 1.0 -0.491491 -0.301918 -0.044493 0.144442 -0.442113 -0.218912 -0.931814 -0.920679 0.13944 0.041893 0.674299 -0.440491 1.0 0.411999 0.429212 0.492949 -0.399222 1.0 -0.336791 -0.181029 0.994949 0.129411 0.412933 -0.303202 -0.491823 -0.322067 0.429444 -0.018499 0.41893 -0.400333 1.0 0.449999 0.181318 0.180318 -0.399499 1.0 -0.932183 -0.181133 -0.094991 0.141849 -0.14493 -0.224194 -0.944493 -0.320679 0.033967 -0.942418 0.391949 -0.994994 1.0 0.094 0.429994 0.424039 -0.399494 1.0 -0.391909 -0.919967 -0.093941 0.931818 0.030339 -0.180244 -0.944009 -0.932674 0.491139 0.13674 0.290499 -0.409404 1.0 -0.4944 0.184414 0.183042 -0.39939 1.0 -0.090193 -0.413244 0.049394 -0.924167 -0.184267 -0.904183 -0.414492 -0.102993 0.902679 0.291331 0.367049 -0.940309 1.0 0.494 0.367939 0.441892 -0.399244 -1.0 -0.939449 -0.184244 -0.018318 -0.242184 -0.331367 -0.204042 -0.418494 -0.949149 -0.931249 -0.093044 0.424499 -0.903192 1.0 0.04 0.418994 0.422444 -0.400923 -1.0 -0.183183 -0.440994 -1.0 0.182183 -0.218118 -0.499304 -0.941891 -0.189333 0.994932 0.931842 0.444992 -0.391104 -0.199999 0.411999 0.291444 0.999444 -0.9942 -1.0 -0.941839 -0.994223 -0.922193 0.394493 -0.941892 -0.949099 -0.944449 -0.494419 -0.671367 -0.090909 -0.12044 -0.492094 1.0 0.139999 0.42413 0.391499 -0.400319 -1.0 -0.332021 -0.42919 -0.94232 0.214444 -0.404189 -0.991091 -0.91944 -1.0 -1.0 -1.0 -0.213203 -1.0 0.999999 -0.093999 0.18367 -0.991674 -0.403333 -1.0 -0.993223 -0.394292 -0.393672 0.210024 -0.423991 -0.249189 -0.942067 -0.240214 0.92924 0.041893 0.993022 -0.318013 1.0 -0.019999 0.183044 0.422114 -0.399244 -1.0 -0.421003 -0.21393 0.030949 0.094967 0.011319 -0.918924 -0.941904 -0.211823 -0.039949 0.190109 0.492441 -0.944124 1.0 0.411999 0.429932 0.421444 -0.3993 -1.0 -0.180119 -0.930367 0.492367 0.144294 -0.09184 -0.941494 -0.930324 -0.944939 0.499292 0.031314 0.679118 -0.394193 1.0 -0.4192 0.424014 0.184994 -0.39941 -1.0 -0.941839 -0.394182 0.093941 0.671319 -0.184901 -0.313242 -0.918932 -0.219933 0.493124 0.144924 -0.14229 -0.294967 1.0 -0.490999 0.42184 0.183167 -0.399674 -1.0 -0.216799 -0.123202 0.991822 -0.102914 0.102914 -0.109149 -0.18494 0.241103 0.189418 0.090909 0.333013 0.391104 1.0 0.672 0.499399 0.1894 -0.40423 1.0 0.119291 0.440967 0.394114 -0.939039 0.209224 0.441844 -0.183679 1.0 -0.303393 -0.093044 0.449967 1.0 1.0 0.941999 -0.496749 0.424444 -0.944193 1.0 -0.421849 -0.330401 -0.094993 -0.092203 -0.418671 -0.292424 -0.941891 -0.333209 -0.149183 -0.441918 0.404933 -0.244967 1.0 -0.2067 0.444494 0.42442 -0.401041 1.0 -0.321839 -0.229142 -0.136724 0.331029 -0.329301 -0.240049 -0.444011 -0.967194 -0.409023 0.182999 0.390211 -0.949441 1.0 0.449999 0.18418 0.181422 -0.399184 1.0 -0.993223 -0.181313 0.104449 0.183924 -0.092402 -0.239441 -0.91944 -0.299367 -0.312499 -0.103994 0.122674 -0.189303 1.0 0.924 0.184994 0.181844 -0.399394 1.0 -0.182418 -0.24499 0.131894 0.311824 -0.194491 -0.944909 -0.939933 -0.429933 0.418444 0.496724 0.418041 -0.419018 1.0 0.419999 0.184194 0.184018 -0.39922 1.0 -0.929111 -0.918444 -0.444903 0.499119 -0.904903 -0.499933 -0.994444 -0.184444 0.339672 -0.019018 -0.167444 -0.189449 1.0 -0.3244 0.429241 0.391192 -0.399314 1.0 1.0 -0.403918 0.391949 -0.141849 0.494913 -0.104042 1.0 -0.49449 0.180339 0.067444 1.0 -0.449914 1.0 -0.093999 0.18304 0.390413 -0.181314 1.0 -0.944999 -0.490944 0.044493 0.29394 -0.142042 -0.367499 -1.0 -0.932367 0.994932 0.029323 0.401814 -0.318133 1.0 -0.404 0.42449 0.1839 -0.399674 1.0 -0.223993 -0.440449 -0.909499 0.679911 -1.0 -1.0 -0.333194 -0.499033 0.671367 -0.404033 -1.0 -0.922331 -1.0 0.411999 0.490118 0.339324 -0.941122 -1.0 -0.449999 -0.391832 0.149499 0.931844 -0.118918 -0.674184 -0.449944 -0.324932 0.033967 0.144924 0.01894 -0.292311 1.0 -0.404 0.181999 0.429118 -0.400118 -1.0 -0.180119 -0.36744 -0.141142 0.142944 -0.099181 -0.296718 -0.184291 -0.319449 -0.240493 0.018444 0.167967 -0.2339 1.0 0.411999 0.42929 0.180367 -0.400033 -1.0 -0.444129 -0.420918 0.131894 0.119492 -0.049909 -0.491942 -0.999181 -0.39449 0.440902 0.444118 0.309939 -0.413041 1.0 -0.3672 0.180214 0.186713 -0.400418 -1.0 -0.993223 -0.442929 -0.131894 0.394033 -0.182942 -0.399332 -0.96793 -0.994494 0.026791 -0.029323 0.1044 -0.49149 1.0 -0.1324 0.184249 0.429914 -0.399312 -1.0 -0.444931 -0.11339 0.393118 -0.302494 0.191399 -0.309329 -0.241129 0.444944 -0.136719 0.304012 0.181814 -0.318133 1.0 0.04 0.672404 0.429293 -0.4049 -1.0 -0.294441 -0.944184 0.182219 -0.390392 -0.674224 -0.244923 -0.904902 -0.240924 0.404931 -0.399933 -0.444103 -0.674418 1.0 -0.4192 -0.424403 0.44939 -0.400672 -1.0
  #9  
Old 18-Feb-2009, 10:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: 2D Vector Memory Problems


Quote:
Originally Posted by Omega037
...it will crash....


Assuming that you want Trainer::random() to return a value between zero and one, this following absolutely, incontrovertibly, guaran-dam-teed wrong for any version of GNU compiler:
CPP / C++ / C Code:
float Trainer::random()
{
   return rand()/(float)32767;    //32767 is the maximum random integer
}
Just for kicks, put a print statement in some place where the return value is really important. For example
CPP / C++ / C Code:
void Trainer::mutcross(int cur)
{
    int base, diff1, diff2;
    float x=0;
    float y=1;   

    cout << "In Trainer::mutcross" << endl;
    random(0, population_size-1, cur, base, diff1, diff2);
    cout << "After random(0," <<population_size-1
         << "," << cur << ",...): base = " << base 
         << ", diff1 = " << diff1 << ", diff2 = " << diff2 << endl;
    for(int r=0; r < num_gates; r++){
        if(random(x, y) < crossover_factor){
            cout << "storing mut_data[" << cur << "][" << r << "]" << endl;
            mut_data[r] = sig_data[cur][r];
        }
        else{
            cout << "Calculating mut_data[" << r << "]" << endl;
            cout << "sig_data[" << base << "][" << r << "] = " << endl;
            cout << sig_data[base][r] << endl;
            mut_data[r] = sig_data[base][r] + 
                (mutation_factor * (sig_data[diff1][r] - sig_data[diff2][r]));
        }
    }
}
If it gets that far, you may see why there is a problem.

I get the following from GNU g++ on my Linux system:
Code:
In Trainer::mutcross After random(0,499,0,...): base = 19887000, diff1 = 7302500, diff2 = 9095500 Calculating mut_data[0] sig_data[19887000][0] = Segmentation fault

Your Mileage May Vary.


Try changing the random() function to
CPP / C++ / C Code:
float Trainer::random()
{
   return rand()/(float)RAND_MAX;  //RAND_MAX is the maximum random integer
}

Note that this will return a value that is greater than or equal to zero and less than or equal to one.

If you want it to return a value that is greater than or equal to zero and less than one:
CPP / C++ / C Code:
float Trainer::random()
{
   return rand()/(RAND_MAX + 1.0); //RAND_MAX is the maximum random integer
}

There may or may not be other problems, but I hope you see the value of supplying something that we can actually use to compare results with whatever it is that you are running.


Regards,

Dave
  #10  
Old 18-Feb-2009, 11:51
Omega037 Omega037 is offline
New Member
 
Join Date: Feb 2009
Posts: 5
Omega037 is on a distinguished road

Re: 2D Vector Memory Problems


I tested all of my random functions and they work perfectly as is. I will keep your concerns in mind though. The crashing usually happens well before that, anyways.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
IE 7 Problems colliegirl Computer Software Forum - Windows 6 05-Jun-2008 09:10
Recent problems with antivirus programmes steven_symantec Member Announcements, Advertisements & Offers 3 14-Feb-2008 22:22
Challenge problems plz help plz crazyABOUTyou C++ Forum 3 24-Apr-2007 11:32
Chaintech Geforce 5600 FX problems bartster74 Computer Hardware Forum 8 04-May-2004 14:16
Chaintech GeForce FX 5600xt Problems Brymat Computer Hardware Forum 14 22-Feb-2004 16:45

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:06.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.