int main(int argc, char* argv[])
{
    typedef double DecisionVariable_t;
    typedef std::mt19937 RNG_t;
    unsigned int seed = 1;
    double eta = 10;
    double crossover_probability = 1.0;
    double eps = 0.00001;
    double proportion_crossed = 1.0;
    RNG_t rng(seed);
    
    DebsSBXCrossover<RNG_t> crossover_tester(rng, eta, crossover_probability, eps, proportion_crossed);
    
    int number_dvs = 1; //number of decision variables
    double min_value = -1.0;
    double max_value = 8.0;
    std::vector<double> lower_bounds(number_dvs, min_value);
    std::vector<double> upper_bounds(number_dvs, max_value);
    std::vector<int> lower_bounds_i;
    std::vector<int> upper_bounds_i;
    std::vector<MinOrMaxType> min_or_max(1, MINIMISATION);
    
//    std::vector<DecisionVariable_t> parent1_dv_values {2};
//    std::vector<DecisionVariable_t> parent2_dv_values {5};
    ProblemDefinitions defs(lower_bounds, upper_bounds,lower_bounds_i, upper_bounds_i, min_or_max, 0);
    Individual parent1(defs);
    Individual parent2(defs);
    
    std::vector<double> results;
    
    int num_samples = 1000000;
    for (int i = 0; i < num_samples; ++i)
    {
        Individual child1(parent1);
        Individual child2(parent2);
        crossover_tester.crossover_implementation(child1, child2);
        results.push_back(child1.getRealDV(0));
        results.push_back(child2.getRealDV(0));
//        std::cout << child1[0] << std::endl;
//        std::cout << child2[0] << std::endl;
    }
    
#ifdef WITH_VTK
    // plot results.
    // Set up a 2D scene, add an XY chart to it
    VTK_CREATE(vtkContextView, view);
    view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
    view->GetRenderWindow()->SetSize(400, 300);
    VTK_CREATE(vtkChartXY, chart);
    view->GetScene()->AddItem(chart);
    
    // Create a table with some points in it...
    VTK_CREATE(vtkTable, table);
    
    VTK_CREATE(vtkIntArray, arrBin);
    arrBin->SetName("decision variable value");
    table->AddColumn(arrBin);
    
    VTK_CREATE(vtkIntArray, arrFrequency);
    arrFrequency->SetName("Frequency");
    table->AddColumn(arrFrequency);
    
    int num_bins = 50;
    table->SetNumberOfRows(num_bins);
    
    std::vector<int> frequency_count(num_bins);
//    std::vector<std::string> bin_names(num_bins);
    std::vector<int> bin_names(num_bins);
    
    for (int i = 0; i < num_samples; ++i)
    {
        frequency_count[int((results[i] - min_value) / (max_value - min_value) * num_bins)]++;
    }
    
    for (int i = 0; i < num_bins; ++i)
    {
//        bin_names[i] = std::to_string((double(i) / double(num_bins)) * (max_value-min_value) + min_value);
        bin_names[i] = i;
    }
    
    for (int i = 0; i < num_bins; i++)
    {
        table->SetValue(i,0,bin_names[i]);
        table->SetValue(i,1,frequency_count[i]);
    }
    
    // Add multiple line plots, setting the colors etc
    vtkPlot *line = 0;
    
    line = chart->AddPlot(vtkChart::BAR);
#if VTK_MAJOR_VERSION <= 5
    line->SetInput(table, 0, 1);
#else
    line->SetInputData(table, 0, 1);
#endif
    line->SetColor(0, 255, 0, 255);
    
    //Finally render the scene and compare the image to a reference image
    view->GetRenderWindow()->SetMultiSamples(0);
    view->GetInteractor()->Initialize();
    view->GetInteractor()->Start();
#endif
    
}
Esempio n. 2
0
//Running everything and saving it correctly.
int main(int argc, char **argv)
{

  if(argc < 2)
    {
      printf("Error, no input given.\n");
      return(0);
    }

  int freq_array[127];

  char * filename = argv[1];
  int filename_size = strlen(filename);

  frequency_count(filename, freq_array);

  char * outfile = (char *)malloc(filename_size+5 * sizeof(char));

  strcpy(outfile, filename);
  outfile = strcat(outfile, ".huff");

  Node * list;

  list = List_build(freq_array);
  if (list == NULL)
    {
      return(0);
    }

  Node * temp;
  temp = list;
  while(temp != NULL)
    {
      printf("Frequency of %c = %d\n", temp -> char_val, temp -> freq);
      temp = temp -> next;
    }
  ListNode * tree_list;
  tree_list = ListNode_build(list);

  while(tree_list -> next != NULL)
    {
      ListNode * second = tree_list -> next;
      ListNode * third = second -> next;

      TreeNode * tn1 = tree_list -> tree_ptr;
      TreeNode * tn2 = second -> tree_ptr;

      free(tree_list);
      free(second);
      tree_list = third;

      TreeNode * fin = Tree_merge(tn1, tn2);
      ListNode * wn = ListNode_create(fin);

      tree_list = ListNode_insert(tree_list, wn);
    }
  Weight_print(tree_list); 
  List_destroy(list);
  TreeNode * root = tree_list -> tree_ptr; 
  free(tree_list);

  int numRow = Tree_leaf(root);
  int numCol = Tree_height(root);
  numCol++;
  int ** codebook = malloc(sizeof(int*) * numRow);
  int row;
  for(row = 0; row < numRow; row++)
    {
      codebook[row] = malloc(sizeof(int) * numCol);
      int col;
      for(col = 0; col < numCol; col++)
	{
	  codebook[row][col] = -1;
	}
    }
  buildCodeBook(root, codebook);
  printCodeBook(codebook, numRow);

  unsigned int numChar = 0;
  int z;
  for(z = 0; z < 127; z++)
    {
      numChar += freq_array[z];
    }
  printf("numChar: %d\n", numChar);
  int mapping[127];
  int ind;
  for(ind = 0; ind < 127; ind++)
    {
      mapping[ind] = -1;
      int ind2;
      for(ind2 = 0; ind2 < numRow; ind2 ++)
	{
	  if(codebook[ind2][0] == ind)
	    {
	      mapping[ind] = ind2;
	    }
	}
    }
  Tree_header(root, numChar,outfile);
  compress(filename, outfile, codebook, mapping);
  return(0);
}