-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
330024b
commit 8a785cc
Showing
4 changed files
with
381 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.4.33110.190 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Genetic Algorithm", "Genetic Algorithm\Genetic Algorithm.vcxproj", "{9B9C689C-50B4-4EA9-B430-FC463A7BA442}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|x64 = Debug|x64 | ||
Debug|x86 = Debug|x86 | ||
Release|x64 = Release|x64 | ||
Release|x86 = Release|x86 | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{9B9C689C-50B4-4EA9-B430-FC463A7BA442}.Debug|x64.ActiveCfg = Debug|x64 | ||
{9B9C689C-50B4-4EA9-B430-FC463A7BA442}.Debug|x64.Build.0 = Debug|x64 | ||
{9B9C689C-50B4-4EA9-B430-FC463A7BA442}.Debug|x86.ActiveCfg = Debug|Win32 | ||
{9B9C689C-50B4-4EA9-B430-FC463A7BA442}.Debug|x86.Build.0 = Debug|Win32 | ||
{9B9C689C-50B4-4EA9-B430-FC463A7BA442}.Release|x64.ActiveCfg = Release|x64 | ||
{9B9C689C-50B4-4EA9-B430-FC463A7BA442}.Release|x64.Build.0 = Release|x64 | ||
{9B9C689C-50B4-4EA9-B430-FC463A7BA442}.Release|x86.ActiveCfg = Release|Win32 | ||
{9B9C689C-50B4-4EA9-B430-FC463A7BA442}.Release|x86.Build.0 = Release|Win32 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {6FA0AF4F-16F4-48D9-813B-DB22697449ED} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup Label="ProjectConfigurations"> | ||
<ProjectConfiguration Include="Debug|Win32"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|Win32"> | ||
<Configuration>Release</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Debug|x64"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|x64"> | ||
<Configuration>Release</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
</ItemGroup> | ||
<PropertyGroup Label="Globals"> | ||
<VCProjectVersion>16.0</VCProjectVersion> | ||
<Keyword>Win32Proj</Keyword> | ||
<ProjectGuid>{9b9c689c-50b4-4ea9-b430-fc463a7ba442}</ProjectGuid> | ||
<RootNamespace>GeneticAlgorithm</RootNamespace> | ||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v143</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v143</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v143</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v143</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
<ImportGroup Label="ExtensionSettings"> | ||
</ImportGroup> | ||
<ImportGroup Label="Shared"> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<PropertyGroup Label="UserMacros" /> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<SDLCheck>true</SDLCheck> | ||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<SDLCheck>true</SDLCheck> | ||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<SDLCheck>true</SDLCheck> | ||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<SDLCheck>true</SDLCheck> | ||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<ClCompile Include="src.cpp" /> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup> | ||
<Filter Include="Source Files"> | ||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | ||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | ||
</Filter> | ||
<Filter Include="Header Files"> | ||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | ||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions> | ||
</Filter> | ||
<Filter Include="Resource Files"> | ||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | ||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | ||
</Filter> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClCompile Include="src.cpp"> | ||
<Filter>Source Files</Filter> | ||
</ClCompile> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
#include<iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
void genetic_algorithm(vector <vector <int>>& chromosomes); | ||
vector <int> generate_chromosome(); | ||
void generate_chromosomes(vector <vector <int>>& chromosomes); | ||
void cross_over(vector <vector <int>>& chromosomes, vector <vector <int>>& children); | ||
void mutation(vector <vector <int>>& children); | ||
void selection(vector <vector <int>>& chromosomes, vector <vector <int>>& children); | ||
int fitness(const vector <int>& chromosome); | ||
bool check_status_of_chromosome(int r, int c, const vector <int>& chromosome); | ||
void print_final_chromosome(const vector <int>& chromosome); | ||
|
||
#define NUMBER_OF_CHROMOSOMES 200 | ||
|
||
void main() | ||
{ | ||
srand(time(NULL)); // To make the rand is not static | ||
vector <vector <int>> chromosomes(NUMBER_OF_CHROMOSOMES, vector <int> (8)); | ||
generate_chromosomes(chromosomes); // generat the first generation | ||
genetic_algorithm(chromosomes); // start the genetic algorithm | ||
} | ||
|
||
// The genetic algorithm function | ||
void genetic_algorithm(vector <vector <int>>& chromosomes) | ||
{ | ||
int itearation = 0; // number of iterations | ||
while (fitness(chromosomes[0]) != 8) | ||
{ | ||
vector <vector <int>> children; | ||
cross_over(chromosomes, children); | ||
mutation(children); | ||
selection(chromosomes, children); | ||
|
||
cout << "\nIteration Number => " << ++itearation << '\n'; | ||
cout << "Best Rank = " << fitness(chromosomes[0]) << '\n'; | ||
cout << "_______________________________\n"; | ||
} | ||
print_final_chromosome(chromosomes[0]); | ||
} | ||
|
||
void generate_chromosomes(vector <vector <int>>& chromosomes) | ||
{ | ||
for (int i = 0; i < NUMBER_OF_CHROMOSOMES; i++) | ||
{ | ||
chromosomes[i] = generate_chromosome(); | ||
} | ||
} | ||
|
||
vector <int> generate_chromosome() | ||
{ | ||
vector <int> chromosome(8); | ||
for (int i = 0; i < 8; i++) | ||
{ | ||
chromosome[i] = (rand() % 8); | ||
} | ||
return chromosome; | ||
} | ||
|
||
void cross_over(vector <vector <int>>& chromosomes, vector <vector <int>>& children) | ||
{ | ||
vector <vector <int>> result_of_cross_over; | ||
|
||
for (int i = 0; i < NUMBER_OF_CHROMOSOMES; i += 2) | ||
{ | ||
vector <int> parent1 = chromosomes[i]; | ||
vector <int> parent2 = chromosomes[i + 1]; | ||
vector <int> child1(8); | ||
vector <int> child2(8); | ||
|
||
int split = 1 + (rand() % 7); | ||
for (int i = 0; i < 8; i++) | ||
{ | ||
if (i < split) | ||
{ | ||
child1[i] = parent1[i]; | ||
} | ||
else | ||
{ | ||
child1[i] = parent2[i]; | ||
} | ||
} | ||
for (int i = 0; i < 8; i++) | ||
{ | ||
if (i < split) | ||
{ | ||
child2[i] = parent2[i]; | ||
} | ||
else | ||
{ | ||
child2[i] = parent1[i]; | ||
} | ||
} | ||
result_of_cross_over.emplace_back(child1); | ||
result_of_cross_over.emplace_back(child2); | ||
} | ||
children = result_of_cross_over; | ||
} | ||
|
||
void mutation(vector <vector <int>>& children) | ||
{ | ||
for (int i = 0; i < NUMBER_OF_CHROMOSOMES; i++) | ||
{ | ||
int n = 1 + (rand() % 7); | ||
for (int j = 0; j < n; j++) | ||
{ | ||
int choise = rand() % 2; | ||
if (choise) | ||
children[i][(rand() % 8)] = (rand() % 8); | ||
} | ||
} | ||
} | ||
|
||
void selection(vector <vector <int>>& chromosomes, vector <vector <int>>& children) | ||
{ | ||
vector <vector <int>> v; | ||
for (int i = 0; i < NUMBER_OF_CHROMOSOMES; i++) | ||
v.emplace_back(chromosomes[i]); | ||
for (int i = 0; i < NUMBER_OF_CHROMOSOMES; i++) | ||
v.emplace_back(children[i]); | ||
|
||
sort(v.rbegin(), v.rend(), [](const vector <int>& v1, const vector <int>& v2) {return fitness(v1) < fitness(v2); }); | ||
|
||
for (int i = 0; i < NUMBER_OF_CHROMOSOMES; i++) | ||
chromosomes[i] = v[i]; | ||
} | ||
|
||
|
||
int fitness(const vector <int>& chromosome) | ||
{ | ||
int rank = 0; | ||
for (int i = 0; i < 8; i++) | ||
{ | ||
int r = i, c = chromosome[i]; | ||
rank += check_status_of_chromosome(r, c, chromosome); | ||
} | ||
return rank; | ||
} | ||
|
||
bool check_status_of_chromosome(int r, int c, const vector <int>& chromosome) | ||
{ | ||
// check columns | ||
for (int i = 0; i < 8; i++) | ||
{ | ||
if (i != r && chromosome[i] == c) | ||
return false; | ||
} | ||
|
||
// check diagonals: | ||
// main diagonal | ||
for (int i = r + 1, j = c + 1; i < 8 && j < 8; i++, j++) | ||
{ | ||
if (chromosome[i] == j) | ||
return false; | ||
} | ||
for (int i = r - 1, j = c - 1; i >= 0 && j >= 0; i--, j--) | ||
{ | ||
if (chromosome[i] == j) | ||
return false; | ||
} | ||
// reverse diagonal | ||
for (int i = r - 1, j = c + 1; i >= 0 && j < 8; i--, j++) | ||
{ | ||
if (chromosome[i] == j) | ||
return false; | ||
} | ||
for (int i = r + 1, j = c - 1; i < 8 && j >= 0; i++, j--) | ||
{ | ||
if (chromosome[i] == j) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
void print_final_chromosome(const vector <int>& chromosome) | ||
{ | ||
cout << "\n_______________________________\n\nFinished, Congrats\nThe final order is : \n\n"; | ||
for (int i = 0; i < 8; i++) | ||
{ | ||
for (int j = 0; j < 8; j++) | ||
{ | ||
if (chromosome[i] == j) | ||
cout << "Q" << " "; | ||
else | ||
cout << "*" << " "; | ||
} | ||
cout << '\n'; | ||
} | ||
cout << "\n\n"; | ||
} |