Skip to content

Instantly share code, notes, and snippets.

@Saket-Upadhyay
Created October 2, 2020 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Saket-Upadhyay/ec8680038af964ca028469c27e9f7398 to your computer and use it in GitHub Desktop.
Save Saket-Upadhyay/ec8680038af964ca028469c27e9f7398 to your computer and use it in GitHub Desktop.
Nature Inspired Genetic Mutation based "String Evolution" Algorithm
/*
MIT License
-----------
Copyright (c) 2020 Saket Upadhyay
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <string>
#include <random>
#include <ctime>
#include <chrono>
using namespace std;
class Config {
private:
string target;
chrono::time_point<chrono::system_clock> startTime;
string GeneSet = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!.,?_:;'<>{}[]()\\/#=+-*";
int gen = 0;
public:
chrono::time_point<chrono::system_clock> getStartTime() {
return this->startTime;
}
void incGen() {
this->gen += 1;
}
int getGen() {
return this->gen;
}
void setTime(chrono::time_point<chrono::system_clock> newtime) {
this->startTime = newtime;
}
void setTarget(string newtarget) {
this->target = newtarget;
}
string getTarget() {
return this->target;
}
string getGeneSet() {
return this->GeneSet;
}
};
class Chromosome {
private:
string Gene;
int Fitness;
public:
Chromosome() {
}
Chromosome(string genes, int fitness) {
this->Gene = genes;
this->Fitness = fitness;
}
string getGene() {
return this->Gene;
}
int getFitness() {
return this->Fitness;
}
void setGene(string newgene) {
this->Gene = newgene;
}
void setFitness(int newfitness) {
this->Fitness = newfitness;
}
};
void display(Chromosome candidate, chrono::time_point<chrono::system_clock> startTime, Config config) {
chrono::duration<double> elapsedtime = chrono::system_clock::now() - startTime;
cout << "Gen.[" << config.getGen() << "]\t=> " << candidate.getGene() << "\t|Fitness => " << candidate.getFitness()
<< "\t|Time elapsed => " << elapsedtime.count() << endl;
}
int get_fitness(string guess, Config config) {
string target = config.getTarget();
int sum = 0;
for (int i = 0; i < target.length(); i++) {
if (guess[i] == target[i]) sum++;
}
return sum;
}
Chromosome _generate_parent(int length, Config config) {
string geneSet = config.getGeneSet();
string genes = "";
int sampleSize;
string tempstr = "";
srand(time(0));
int Fitness;
while (tempstr.length() < length) {
sampleSize = min(length - (int) tempstr.length(), (int) geneSet.length());
for (int i = 0; i < sampleSize; ++i) {
int temprand = (int) ((random() % (geneSet.length() + 1)) + 0);
tempstr.push_back(geneSet[temprand]);
}
}
genes.append(tempstr);
Fitness = get_fitness(genes, config);
return Chromosome(genes, Fitness);
}
Chromosome _mutate(Chromosome parent, Config config) {
int index = (int) (random() % (parent.getGene().length() + 1));
string child_genes = parent.getGene();
string geneSet = config.getGeneSet();
char newGene;
char alternateGene;
int randomselect = (int) (random() % (geneSet.length() + 1));
newGene = geneSet[randomselect];
int newrandomselect = (int) (random() % (geneSet.length() + 1));
while (randomselect == newrandomselect) {
newrandomselect = (int) (random() % (geneSet.length() + 1));
if (randomselect != newrandomselect) {
break;
}
}
alternateGene = geneSet[newrandomselect];
if (newGene == child_genes[index]) {
child_genes[index] = alternateGene;
} else {
child_genes[index] = newGene;
}
int fitness = get_fitness(child_genes, config);
return Chromosome(child_genes, fitness);
}
Chromosome get_best(int optimalFitness, Config config) {
int targetLen = config.getTarget().length();
string geneSet = config.getGeneSet();
Chromosome child;
Chromosome bestParent = _generate_parent(targetLen, config);
display(bestParent, config.getStartTime(), config);
if (bestParent.getFitness() >= optimalFitness) {
return bestParent;
}
while (1) {
config.incGen();
child = _mutate(bestParent, config);
if (bestParent.getFitness() >= child.getFitness()) {
continue;
}
display(child, config.getStartTime(), config);
if (child.getFitness() >= optimalFitness) {
return child;
}
bestParent.setFitness(child.getFitness());
bestParent.setGene(child.getGene());
}
}
void guess_password(Config config) {
int optimalFitness = config.getTarget().length();
auto best = get_best(optimalFitness, config);
}
//MAIN FUNCTION
int main() {
Config config;
srand(time(0));
config.setTarget("This is the target string to reach by mutation, and the best fitness is 86 - x64mayhem");
chrono::time_point<chrono::system_clock> timenow = chrono::system_clock::now();
config.setTime(timenow);
guess_password(config);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment