Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include<bitset>
- using namespace std;
- double eval(int *pj);
- int* RandomSolutionGenerator(){
- int* solution = new int[100];
- for(int i=0; i<100; i++){
- solution[i] = rand() %2;
- }
- return solution;
- }
- double Evaluate(int* solution){
- return eval(solution);
- }
- void Modify(int* solution){
- int index = rand()%100;
- solution[index] = 1 - solution[index];
- }
- void Modify(int* solution, double mutationRate){
- for(int i=0; i<100; i++){
- double randomVal = static_cast<double>(rand())/ RAND_MAX;
- if(randomVal < mutationRate){
- solution[i] = 1- solution[i];
- }
- }
- }
- void PrintSolution(int* solution){
- for(int i=0; i<100; i++){
- cout << solution[i];
- }
- cout << endl;
- }
- int main()
- {
- srand(time(0));
- int* solutionBase = RandomSolutionGenerator();
- double fitnessBase = Evaluate(solutionBase);
- bool done = false;
- int* solutionNew;
- while(!done){
- solutionNew = new int[100];
- for(int i=0; i<100; i++){
- solutionNew[i] = solutionBase[i];
- }
- Modify(solutionNew, 0.01);
- double fitnessNew = Evaluate(solutionNew);
- cout << fitnessNew << endl;
- if(fitnessNew >= fitnessBase){
- delete[] solutionBase;
- solutionBase = solutionNew;
- fitnessBase = fitnessNew;
- }
- if(fitnessNew >=70){
- done = true;
- PrintSolution(solutionNew);
- }
- }
- for(int i=0; i<1073741823; i++){
- int number = i; // Replace with your integer value
- int numBits = 30; // Number of bits for a 30-bit binary representation
- std::bitset<30> binaryRepresentation(number);
- for (int i = 0; i < numBits; ++i) {
- solutionNew[i] = binaryRepresentation[numBits - 1 - i];
- }
- double fitnessNew = Evaluate(solutionNew);
- cout << "Iteration: "<< i << " Fitness: " << fitnessNew <<endl;
- if(fitnessNew >=100){
- break;
- }
- }
- delete[] solutionBase;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment