-
Notifications
You must be signed in to change notification settings - Fork 2
/
find_password.cpp
57 lines (47 loc) · 1.57 KB
/
find_password.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @file find_password.cpp
* @author Ali Marouf (https://github.com/AliMarouf97)
* @brief solving a maximization problem using GA
* @version 0.1
*
* @copyright Copyright (c) 2022
*
*/
#include <iostream>
#include "GeneticAlgorithm.h"
using namespace std;
// Your friend discovered a security hole in his neighbor's wireless network,
// which allows him to determine the length of the password
// and test any combination of passwords that comes to his mind.
// Moreover, he can measure the time it takes to verify the password,
// which allows him to estimate the number of correct characters in each word.
// Your friend wants you to help him find the password as soon as possible
// because he is tired of waiting.
char password[] = "Ali Marouf ali marouf ALI MAROUF 2022";
const int p_size = sizeof(password) - 1; // -1 to ignore '\0' char
// The chromosome represents the password.
struct Chromosome
{
char c[p_size];
};
double fitnessFunction(Chromosome &chromosome)
{
double scoure = 0;
for (int i = 0; i < p_size; i++)
{
char c = chromosome.c[i];
if (c == password[i])
++scoure;
}
return scoure;
}
int main()
{
GeneticAlgorithm<Chromosome> ga(fitnessFunction, true, 1000, 1.5, 20);
ga.initializePopulation(2000);
ga.terminationConditions.setFitnessGoal(p_size);
auto bestIndividual = ga.solve();
printf("Best: c = %.*s", p_size, bestIndividual.getChromosome().c);
// cout << "Best: c = " << bestIndividual.getChromosome().c << "\n";
return 0;
}