Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem #10

Merged
merged 3 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/engine/reference/builder/#dockerignore-file

**/.DS_Store
**/__pycache__
**/.venv
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Use an official PHP runtime as a parent image
FROM php:8.1-cli

# Set working directory
WORKDIR /usr/src/app

# Install Composer globally
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Adding user for application (non-root)
RUN useradd -m appuser && chown -R appuser:appuser /usr/src/app
USER appuser

# Copy the application files
COPY --chown=appuser:appuser . .

# Install dependencies
RUN composer install

# Command to run the application
CMD [ "php", "./run.php" ]
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
up:
docker-compose up -d

down:
docker-compose down

build:
docker-compose build

logs:
docker-compose logs -f
9 changes: 9 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3.8'

services:
app:
build: .
volumes:
- .:/usr/src/app
ports:
- "8000:8000"
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"symfony/var-dumper": "^5.0",
"phpunit/phpunit": "^9.5"
},
"scripts": {
"test": "phpunit --bootstrap vendor/autoload.php tests/"
},
"config": {
"platform": {
"php": "8.1"
Expand Down
40 changes: 30 additions & 10 deletions run.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
<?php
require_once __DIR__ . '/vendor/autoload.php';

$chromosome = new \Charlie\Chromosome\Chromosome([
new \Charlie\Gene\ByteGene(),
new \Charlie\Gene\ByteGene(),
new \Charlie\Gene\ByteGene(),
(new \Charlie\Gene\ByteGene())->set(true),
(new \Charlie\Gene\ByteGene())->set(true),
(new \Charlie\Gene\ByteGene())->set(true),
$population = \Charlie\Util\StringUtilities::createPopulationWithByteGenes([
'000000000',
'000100000',
'001000000',
'011000000',
'000100000',
'001000000',
'011001000',
'000100000',
'001001000',
'011001000',
'000100000',
'001001000',
'011000000',
'000100100',
'001010000',
'011010000',
]);

dump($chromosome->__toString());
$chromosome->mutate();
dump($chromosome->__toString());
$randomizer = new \Charlie\Randomizer\MtRandomizer();

$problem = new \Charlie\Actions\Problem\Problem();
$problem->setCalculator(new \Charlie\Fitness\DummySumCalculator());
$problem->setCrossOver(new \Charlie\Actions\CrossOver($randomizer));
$problem->setMutator(new \Charlie\Actions\Mutator($randomizer));
$problem->setSelection(new \Charlie\Actions\PairSelection());
$problem->setPopulation($population);
$problem->setMaxEvolveCount(100);

$problem->solve();

echo (string) $problem->getPopulation() . PHP_EOL;
27 changes: 21 additions & 6 deletions src/Charlie/Actions/CrossOver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Charlie\Chromosome\Chromosome;
use Charlie\Exceptions\DifferentLengthException;
use Charlie\Individual\Individual;
use Charlie\Individual\IndividualInterface;
use Charlie\Randomizer\RandomizerInterface;

class CrossOver implements CrossOverInterface
Expand All @@ -24,19 +26,32 @@ public function run(Chromosome $chromosome1, Chromosome $chromosome2): array
$lastIndex = $chromosome1->getLastIndex();

$crossOverPoint = $this->randomizer->getInteger($firstIndex, $lastIndex);
$child1 = new Chromosome([]);
$child2 = new Chromosome([]);
$sequence1 = [];
$sequence2 = [];

for ($i = $firstIndex; $i < $crossOverPoint; $i++) {
$child1->setGene($i, $chromosome1->getGene($i));
$child2->setGene($i, $chromosome2->getGene($i));
$sequence1[$i] = clone $chromosome1->getGene($i);
$sequence2[$i] = clone $chromosome2->getGene($i);
}
for ($i = $crossOverPoint; $i <= $lastIndex; $i++) {
$child1->setGene($i, $chromosome2->getGene($i));
$child2->setGene($i, $chromosome1->getGene($i));
$sequence1[$i] = clone $chromosome2->getGene($i);
$sequence2[$i] = clone $chromosome1->getGene($i);
}

$child1 = new Chromosome($sequence1);
$child2 = new Chromosome($sequence2);

return [$child1, $child2];
}

public function runForIndividuals(IndividualInterface $individual1, IndividualInterface $individual2): array
{
$chromosomes = $this->run($individual1->getChromosome(), $individual2->getChromosome());
$offspring1 = new Individual($chromosomes[0]);
$offspring2 = new Individual($chromosomes[1]);

return [$offspring1, $offspring2];
}


}
3 changes: 3 additions & 0 deletions src/Charlie/Actions/CrossOverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Charlie\Actions;

use Charlie\Chromosome\Chromosome;
use Charlie\Individual\IndividualInterface;

interface CrossOverInterface
{

public function run(Chromosome $chromosome1, Chromosome $chromosome2): array;

public function runForIndividuals(IndividualInterface $individual1, IndividualInterface $individual2): array;

}
2 changes: 1 addition & 1 deletion src/Charlie/Actions/Mutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Mutator implements MutatorInterface
{

public function __construct(private RandomizerInterface $randomizer)
public function __construct(private readonly RandomizerInterface $randomizer)
{
}

Expand Down
28 changes: 13 additions & 15 deletions src/Charlie/Actions/PairSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,51 @@

use Charlie\Fitness\CalculatorInterface;
use Charlie\Individual\IndividualInterface;
use Charlie\Individual\Pair;
use Charlie\Population\PopulationInterface;

class PairSelection implements SelectionInterface
{

/**
* @param PopulationInterface $population
* @param CalculatorInterface $calculator
* @return IndividualInterface[]
*/
public function selectBest(PopulationInterface $population, CalculatorInterface $calculator): array
public function selectBest(PopulationInterface $population, CalculatorInterface $calculator): Pair
{
$fittest1 = PHP_INT_MIN;
$fittest2 = PHP_INT_MIN;
$pair = [];
$individual1 = null;
$individual2 = null;

foreach ($population->getIndividuals() as $individual) {
$fit = $individual->calculateFitness($calculator);
if ($fit > $fittest1) {
$fittest1 = $fit;
$pair[0] = $individual;
$individual1 = $individual;
}
}

foreach ($population->getIndividuals() as $individual) {
$fit = $individual->calculateFitness($calculator);
if ($fit > $fittest2 && $fit < $fittest1) {
$fittest2 = $fit;
$pair[1] = $individual;
$individual2 = $individual;
}
}

return $pair;
return new Pair($individual1, $individual2);
}

public function selectWorst(PopulationInterface $population, CalculatorInterface $calculator): array
public function selectWorst(PopulationInterface $population, CalculatorInterface $calculator): Pair
{
$min1 = PHP_INT_MAX;
$min2 = PHP_INT_MAX;
$pair = [];
$individual1 = null;
$individual2 = null;

foreach ($population->getIndividuals() as $individual) {
$fit = $individual->calculateFitness($calculator);

if ($fit < $min1) {
$min1 = $fit;
$pair[0] = $individual;
$individual1 = $individual;
}
}

Expand All @@ -59,11 +57,11 @@ public function selectWorst(PopulationInterface $population, CalculatorInterface

if ($fit < $min2 && $fit > $min1) {
$min2 = $fit;
$pair[1] = $individual;
$individual2 = $individual;
}
}

return $pair;
return new Pair($individual1, $individual2);
}


Expand Down
Loading
Loading