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:

#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);
#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:

#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:

#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 Array. Any ideas?

Before I tell you what I think is wrong I am going to tell you what I think has lead you to not being able to solve this.

First: the layout. It is a mess. Code has to be tidy. I understand you are most likely not a CS grad but please take some pride in your code. It should be the same as your lab work etc.

Second: the try blocks. Put ONE round the whole lot. You then refine later.

Third:

catch(bad_alloc)
{
    cout << " MUTMEMLIMIT ";
     try{
        mutcross_sig.push_back(0);
        }
}

Are you insane - What do you think is going to happen now! You have freed no large block of memory, (since the code above is singular addition.) This is just a 100% mess.

Fourth: Include string.h/math.h is about 10 years out of date.

Five: using namespace std; as a global is asking for trouble.

Six: This doesn't compile, it is not that you just couldn't be bothered to copy with the mouse and that you forgot whole section of classes. Trainer refers directly to stuff in runner etc.

Seven: What is this loading string, then using char[40] to get a filename...

Eight: How many times do you create a large 2d vector THEN create another version in a copy. e.g. vector<vector<float> > Runner::load_training_data()

Nine:

training_data = vector<vector<float> >(n_trainings, vector<float> (total) );
training_data = load_training_data();

So excellent create something into training_data and then forget it and write something new.

Ten: You are using std::vector (why I don't know) and then decide to mix in float t_sig[total-1]

Eleven: Well you couldn't even be bothered to write a short int main() so we can see how to use the code.

Twelve: You decide not to give us runner's destructor but you are having memory allocation/deallocation/corruption problems??

Anyway now you have read this far...... Well I had no problem running it after I fixed all the problems. If you wish to post better code and an example that we can all run and shows a problem you might get some more success. The problem is not directly here.

In short: stop treating the community that are normally more than happy to help, particularly interesting stuff like genetic learning code with such little respect.

FINALLY: DON'T DOUBLE POST ON TWO FORUMS AT THE SAME TIME.
http://www.neowin.net/forum/index.php?showtopic=734718&pid=590566894&st=0&#entry590566894

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Thanks for the help. As for why the code won't compile or anything like that, as I wrote, "I would post all the code, but it is proprietary technology". I basically had to cut a lot out. Seriously, about half the code deals with or references something that no one outside my department and a few people at a certain cancer research center know about. As for why it is a little shoddy/old looking, the original code was written in 1998-99, I have had to rewrite a lot of it to get it to work. Originally it was just one long file with crap going everywhere, this is a lot cleaner than before.

As for double posting, I didn't realize it was an issue. I just assumed more forums would give more people a chance to look at it.

Sorry but the "but it is proprietary technology" is complete junk.
What made me irritated, was that you couldn't be bothered to post a working example of the problem. Sure if you don't want to post proprietry code then just base your example on it. BUT MAKE IT WORK. MAKE IT COMPLETE

You have learnt very little about your problem and we have gained no new insight into stl problems/interesting code etc.

What you are doing using an old code like that especially in the field of genetic algorithms, you are going to suffer, since a lot has been discovered since 2000.

Additionally, if you are using proprietary code for you Phd thesis, expect two things (a) that an external examiner will want to see the code -- but then because he is old etc will want to give it to his grad student etc. (b) that the next person who hires you will want to help eductate his/her group with your techniques. Both of these are going to be very very unpleasent experiences with your proprietary code.

Sorry but the "but it is proprietary technology" is complete junk.
What made me irritated, was that you couldn't be bothered to post a working example of the problem. Sure if you don't want to post proprietry code then just base your example on it. BUT MAKE IT WORK. MAKE IT COMPLETE

You have learnt very little about your problem and we have gained no new insight into stl problems/interesting code etc.

What you are doing using an old code like that especially in the field of genetic algorithms, you are going to suffer, since a lot has been discovered since 2000.

Additionally, if you are using proprietary code for you Phd thesis, expect two things (a) that an external examiner will want to see the code -- but then because he is old etc will want to give it to his grad student etc. (b) that the next person who hires you will want to help eductate his/her group with your techniques. Both of these are going to be very very unpleasent experiences with your proprietary code.

Sorry it wasn't complete. The code was theorized by my adviser and tested around 1999, but computers of the time weren't fast enough AND the problems weren't big enough for this method to gain notice. Now this theory, along with some useful additions, is both plausible and there are problems which can utilize its full capabilities. There is nothing like this out there now, and the professor I work with is one of the founders of the field of Machine Intelligence(he is quite old :) ).

It isn't actually a GA at all, though it uses Differential Evolution as a training method. To give a very basic understanding without giving away too much, it is a method that combines various classifiers(BPNN, SVMs, etc) in a way that is far better than simply averaging their results. There is a lot more to it than that, but we are planning to publish several papers on it soon, hence not giving away too many details.

I am only a first year Graduate Student(going for my Master's, then PhD after that). My undergrad is in Bioengineering, and my CS background is mostly what I've taught myself. The code itself is merely a means to test this theory, after which CS students could rewrite the code to be cleaner looking, faster running, and have a GUI. My term "proprietary" is more in the sense that we don't want people to beat us in publications somehow by disseminating the theory right now. It is the first of many things I will work on, and in a year or two I imagine that this theory/code will be available if someone wants to use it.

Sorry to be such a pain though, I will try to clean up things better next time I post a question.

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

#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

#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

#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

#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

#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 is included:

I-Oracle-View1.txt

Attachments
-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 -

Well first off, thanks for actually posting a proper program.

The following code crashes for at least four reasons. (all easily discoverable with 20 seconds with the debugger. [which is both why you should learn to use ddd/gdb, why you should compile with full warnings, and why you should post complete code]

The first error. in Runner::Runner(int,int,int) you allocate memory with malloc. Then in Runner::~Runner() you deallocate that memory with delete []. This is not correct and will result in memory corruption. What happens after that I have no idea.

Second: Trainer::random() does not work. using rand() gives a number between 0 and INT_MAX. BUT you have hand coded in int max on a 16 bit machine!! So you don't get a number between 0 and 1. That messes up many other things, e.g. base becomes lots.

Third: Initialization, you do none. So you end up deleting memory locations that don't exist. If you write a default constructor then write a proper initializer list.

Forth: assignment operators : operator=(const Runner&) is compiler constructed. BUT you have allocated memory. 99.99% certain to fail. Write one/ OR if you think it is not used declare it private. Then you will see that g=Runner() uses it. [And is a pointless create/copy/delete sequence.]

That fixes just about everything. Well I didn't worry about the results. But it runs without memory corruption.

This article has been dead for over six months. Start a new discussion instead.