Giter Site home page Giter Site logo

voytas75 / genetic-algorithm Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 196 KB

Powershell module for genetic algorithm.

License: MIT License

PowerShell 100.00%
genetic-algorithm algorithm-operation population crossover powershell selection fitness-function

genetic-algorithm's Introduction

Genetic Algorithm Module in PowerShell

DNA Sequence

ko-fi

PowerShell Gallery Version   Codacy Badge   PowerShell Gallery

The Genetic Algorithm Module in PowerShell is a module that implements a genetic algorithm (GA). Genetic algorithms are metaheuristics inspired by the process of natural selection and belong to the class of evolutionary algorithms. They are commonly used to solve optimization and search problems by employing biologically inspired operators such as mutation, crossover, and selection. This module provides a PowerShell implementation of a genetic algorithm.

What is GA?

To understand the genetic algorithm, it is helpful to refer to the Wikipedia page that provides a comprehensive definition and explanation. In brief, a genetic algorithm is a metaheuristic that generates high-quality solutions by mimicking the process of natural selection and evolution. It was introduced by John Holland in 1960 based on the principles of Darwin's theory of evolution.

Syntax

The module provides the following command:

Start-GA [[-Generations] <int>] [[-PopulationSize] <int>] [[-ChromosomeSize] <int>] [[-CrossOverProbability] <double>] [[-MutationProbability] <double>] [[-Selection] <Object>] [-Log] [-Zeros] [-ShowGraph] [-ShowChart] [-ReturnAllGenerations] [<CommonParameters>]

Parameters

  • Generations: Specifies the number of recalculation iterations for the population before completing the algorithm. The default value is 20.
  • PopulationSize: Defines the size of the population used in the genetic algorithm. The size refers to the number of genomes (chromosomes), and it must be an even number. The default value is 30.
  • ChromosomeSize: Determines the number of genes in each chromosome. The default value is 20.
  • CrossOverProbability: Sets the probability of crossing two chromosomes at a random crossing point. The default value is 0.6.
  • MutationProbability: Specifies the probability of a gene mutation in each chromosome. The default value is 0.001.
  • Selection: Determines the type of selection used in the genetic algorithm. The available options are "Roulette" (default) and "Tournament".
  • Log: Generates a log file from the algorithm's operation. The log file is automatically saved with a default path and filename.
  • Zeros: Specifies that the initial population consists of chromosomes with all genes set to 0.
  • ShowGraph: Displays an ASCII graph representing the value of the objective function for the initial population and populations from all iterations of the algorithm.
  • ShowChart: Generates a PNG chart showing the value of the objective function for the initial population and populations from all iterations of the algorithm.
  • ReturnAllGenerations: Returns a result table containing data for all generations, including the initial generation.

How to Use

To use the Genetic Algorithm Module, follow these steps:

Installation

Install the module from the PowerShell Gallery using the following command:

Install-Module -Name GeneticAlgorithm

You can also install it in the current user's directory by adding the -Scope CurrentUser parameter.

Running the Algorithm

Once the module is installed, you can run the genetic algorithm using the Start-GA command:

Start-GA

The algorithm will execute with default parameters and provide output with the best generation, best fitness, fitness gain percentage, and file paths for the log and PNG plot.

Examples

Here are a few examples to demonstrate different usage scenarios:

Example 1

Running the algorithm with logging and an ASCII graph:

Start-GA -Log -ShowGraph

This command will execute the genetic algorithm with default parameters, generate a log file, and display an ASCII graph representing the value of the objective function.

Output:

Best generation: [20]
Best fitness: [374]
Fitness gain: [144,44 %]
┌ GA ──────────────────────────┐
│                              │
│   380┤                    ▄  │
│   370┤                ██  █  │
│   360┤            █ █ ██▄██  │
│   350┤            █ ███████  │
│   340┤            █ ███████  │
│   330┤         █  █▄███████  │
│   320┤      ▄  █  █████████  │
│   310┤      █  █  █████████  │
│   300┤      █  █  █████████  │
│F  290┤      █▄ █▄ █████████  │
│i  280┤     ▄██ ██ █████████  │
│t  270┤     ███ ████████████  │
│n  260┤     ███▄████████████  │
│e  250┤     ████████████████  │
│s  240┤ █ ▄ ████████████████  │
│s  230┤ █ █ ████████████████  │
│   220┤ █▄█ ████████████████  │
│   210┤ ███▄████████████████  │
│   200┤ ████████████████████  │
│   190┤ ████████████████████  │
│   180┤ ████████████████████  │
│   170┤ ████████████████████  │
│   160┤▄████████████████████  │
│      └─────────┬─────────┬─  │
│               10        20   │
│            Generations       │
└──────────────────────────────┘
LOG: C:\Users\voytas\AppData\Local\Temp\GA.log
OUT DATA: C:\Users\voytas\AppData\Local\Temp\allGenerations.log
PNG: C:\Users\voytas\AppData\Local\Temp\GA.png

Output

LOG: C:\Users\voytas\AppData\Local\Temp\GA.log - The path of the generated log file.

Example 2

Running the algorithm with a PNG chart:

Start-GA -ShowChart

This command will execute the genetic algorithm with default parameters and generate a PNG chart representing the value of the objective function.

Output:

Output

Example 3

Customizing the algorithm parameters:

Start-GA -Generations 100 -PopulationSize 40 -MutationProbability 0.009 -Zeros -Log -ShowGraph

This command will execute the genetic algorithm with specific parameter values, including a different number of generations, population size, mutation probability, and using an initial population consisting of chromosomes with all genes set to 0.

Output:

Output

Example 4

Accessing the data of all populations processed by the algorithm:

$GAout = Start-GA -Generations 10 -ChromosomeSize 30 -ReturnAllGenerations
$GAout[10][2].foreach{"$_"}
$GAout[10][1]

These commands will execute the genetic algorithm with specific parameter values and store the output in the $GAout variable. You can then access the data of specific populations using array indexing (switch ReturnAllGenerations). In this example, it retrieves the data of the 10th iteration and displays the chromosomes, as well as the value of the objective function for the iteration.

Output:

> $GAout = Start-GA -Generations 10 -ChromosomeSize 30 -ReturnAllGenerations
Best generation: [10]
Best fitness: [450]
Fitness gain: [69,81 %]
OUT DATA: C:\Users\voytas\AppData\Local\Temp\allGenerations.json
PNG: C:\Users\voytas\AppData\Local\Temp\GA.png

> $GAout[10][2].foreach{"$_"}
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 0 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 0 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0
0 1 1 0 0 1 1 1 0 0

> $GAout[10][1]
450

Feel free to experiment with different parameter values and explore the generated output to analyze the results of the genetic algorithm.

Please note that the module may require additional dependencies, such as the Graphical module for displaying graphs and charts.

genetic-algorithm's People

Contributors

voytas75 avatar

Watchers

 avatar

genetic-algorithm's Issues

Change description

Change description of generatechromosomes function. There is "one is default". Should be "default valuEs are definied"

Change condition

Line 127. Change condition for variable fitness value to use i t olerator not bool "-not"

Function for main code

build main function and move there all main code. Add params for all important variables.

Functions rename

Function "generategene" in fact generates chromosome. And function "generatechromosomes" generates genomes. Match right names.

Chceck module dependiences

I.e.

if (Get-Module -ListAvailable -Name SomeModule) {
    Write-Host "Module exists"
} 
else {
    Write-Host "Module does not exist"
}

create module

Create module form script.

add:

PowerShellVersion = '5.1'
DotNetFrameworkVersion = '4.7.2'
CompatiblePSEditions = 'Desktop'

Add check object param to functions

Add expected data type for population for functions which take the variable $population. The data type of the variable $population is array of arrays. Every genome is array.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.