amitdutta121

Eval 2

Sep 12th, 2023
1,405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include<bitset>
  5.  
  6. using namespace std;
  7.  
  8. double eval(int *pj);
  9.  
  10. int* RandomSolutionGenerator(){
  11.     int* solution = new int[100];
  12.    
  13.     for(int i=0; i<100; i++){
  14.         solution[i] = rand() %2;
  15.     }
  16.    
  17.     return solution;
  18. }
  19.  
  20. double Evaluate(int* solution){
  21.     return eval(solution);
  22. }
  23.  
  24. void Modify(int* solution){
  25.     int index = rand()%100;
  26.     solution[index] = 1 - solution[index];
  27. }
  28.  
  29.  
  30.  
  31. void Modify(int* solution, double mutationRate){
  32.     for(int i=0; i<100; i++){
  33.         double randomVal = static_cast<double>(rand())/ RAND_MAX;
  34.        
  35.         if(randomVal < mutationRate){
  36.             solution[i] = 1- solution[i];
  37.         }
  38.     }
  39. }
  40.  
  41. void PrintSolution(int* solution){
  42.     for(int i=0; i<100; i++){
  43.         cout << solution[i];
  44.     }
  45.    
  46.     cout << endl;
  47. }
  48.  
  49.  
  50.  
  51.  
  52. int main()
  53. {
  54.     srand(time(0));
  55.    
  56.     int* solutionBase = RandomSolutionGenerator();
  57.     double fitnessBase =  Evaluate(solutionBase);
  58.    
  59.    
  60.     bool done = false;
  61.     int* solutionNew;
  62.    
  63.     while(!done){
  64.         solutionNew = new int[100];
  65.        
  66.         for(int i=0; i<100; i++){
  67.             solutionNew[i] = solutionBase[i];
  68.         }
  69.        
  70.         Modify(solutionNew, 0.01);
  71.        
  72.        
  73.         double fitnessNew = Evaluate(solutionNew);
  74.        
  75.         cout << fitnessNew << endl;
  76.        
  77.         if(fitnessNew  >= fitnessBase){
  78.             delete[] solutionBase;
  79.             solutionBase = solutionNew;
  80.             fitnessBase = fitnessNew;
  81.         }
  82.        
  83.        
  84.         if(fitnessNew >=70){
  85.             done = true;
  86.             PrintSolution(solutionNew);
  87.         }
  88.     }
  89.    
  90.     for(int i=0; i<1073741823; i++){
  91.             int number = i; // Replace with your integer value
  92.             int numBits = 30; // Number of bits for a 30-bit binary representation
  93.  
  94.             std::bitset<30> binaryRepresentation(number);
  95.  
  96.  
  97.             for (int i = 0; i < numBits; ++i) {
  98.             solutionNew[i] = binaryRepresentation[numBits - 1 - i];
  99.             }
  100.             double fitnessNew = Evaluate(solutionNew);
  101.            
  102.             cout << "Iteration: "<< i << " Fitness: " << fitnessNew <<endl;
  103.             if(fitnessNew >=100){
  104.                 break;
  105.             }
  106.            
  107.     }
  108.    
  109.     delete[] solutionBase;
  110.    
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment