PHP Classes

PHP Genetic Algorithm Class: Optimize order of sequences with Genetic algorithm

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 54%Total: 416 All time: 6,437 This week: 560Up
Version License PHP version Categories
galgo 1.0GNU General Publi...5.0Algorithms, PHP 5, Artificial intelli...
Description 

Author

This class can be used to optimize order of sequences with Genetic algorithm.

It can take an array with a sequence of objects to optimize and it applies genetic algorithm to evolve the sequence.

The class applies a given number of generations to perform elitistic selection with a given fitness function, applies cross-over and mutation.

The resulting sequence of objects is stored as an array class variable.

An example is provided to optimize a sequence of cities to travel.

Picture of Ravindu Taveesha
  Performance   Level  
Name: Ravindu Taveesha <contact>
Classes: 3 packages by
Country: Sri Lanka Sri Lanka
Age: 34
All time rank: 26784 in Sri Lanka Sri Lanka
Week rank: 187 Up1 in Sri Lanka Sri Lanka Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php

//problem,
//Try to find suitable three cities by given cities using interest percentage of users
//and cities adventure, historical and environmental values to arrange trip.

require_once('galgo.php');

//object class
class City {
    var
$adventure;
    var
$history;
    var
$enviorment;
    
    function
City($adventure=0,$history=0,$enviorment=0) {
       
$this->adventure = $adventure;
       
$this->history = $history;
       
$this->enviorment = $enviorment;
    }
}

//assume total of properties = 10;
$anuradhapura = new City(1,8,1); //32
$nuwaraeliya = new City(4,1,5); //36
$mahanuwara = new City(1,6,2); //27
$sinharaja = new City(0,2,7); //13
$mathara = new City(5,2,3); //44
$kataharagama = new City(2,3,5); //28
$polonnaruwa = new City(1,3,7); //27
$hikkduwa = new City(6,0,4); //46
$galle = new City(5,1,4); //42
$amapara = new City(2,4,4); //30

//town lists
$towns = array($anuradhapura,$nuwaraeliya,$mahanuwara,$sinharaja,$mathara,$kataharagama,$polonnaruwa,$hikkduwa,$galle, $amapara);

//select random population
for ($i = 0; $i < 10 ; $i++)
{
    foreach(
array_rand($towns, 3) as $key){
       
$objects[] = $towns[$key];
    }
   
   
$population[] = array_slice($objects, $i, 3);
}


//This will be the fitness function.
function fitnessFunction($obj) {
   
   
$adventurePrecentage = 7;
   
$enviormentPrecentage = 3;
   
$historyPrecentage = 1;
   
    foreach(
$obj as $key => $objs){
       
$fitnessValue += (($objs->adventure * $adventurePrecentage) + ($objs->history * $historyPrecentage) + ($objs->enviorment * $enviormentPrecentage) );
    }
   
    return
$fitnessValue;
}

$galgo = new GAlgo();

$galgo->population = $population;
$galgo->generations = 10;
$galgo->mutationProbability = 10;
$galgo->fitnessFunction = 'fitnessFunction';
$galgo->evolve();


//no use for genetic
function debug($x) {
    echo
"<pre style='border: 1px solid black'>";
   
print_r($x);
    echo
'</pre>';
}

?>


Details

Requirements

PHP 5 or heigher apache server or WAMP/XAMPP server

Installation

add these two files into www floder which is in your C:/ drive to run script, open your browser and enter your 'localhost/yourflodername' url

Modification

you are free to change modifications, if you find any bug please inform it to this email thaveesha.ravindu2@gmail.com

Thank you.


  Files folder image Files (3)  
File Role Description
Plain text file galgo.php Class the main class
Accessible without login Plain text file readme Doc. read me
Accessible without login Plain text file travel.php Example travel example

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:416
This week:0
All time:6,437
This week:560Up
 User Ratings  
 
 All time
Utility:66%StarStarStarStar
Consistency:75%StarStarStarStar
Documentation:66%StarStarStarStar
Examples:66%StarStarStarStar
Tests:-
Videos:-
Overall:54%StarStarStar
Rank:2121