DarwinGA 1.3.0
dotnet add package DarwinGA --version 1.3.0
NuGet\Install-Package DarwinGA -Version 1.3.0
<PackageReference Include="DarwinGA" Version="1.3.0" />
<PackageVersion Include="DarwinGA" Version="1.3.0" />
<PackageReference Include="DarwinGA" />
paket add DarwinGA --version 1.3.0
#r "nuget: DarwinGA, 1.3.0"
#:package DarwinGA@1.3.0
#addin nuget:?package=DarwinGA&version=1.3.0
#tool nuget:?package=DarwinGA&version=1.3.0
𧬠DarwinGA
A High-Performance Genetic Algorithm Engine for .NET
DarwinGA is a modular, extensible genetic algorithm framework for .NET 8.
Define your problem. Let evolution find the solution.
π Key Differentiator: DarwinGA supports AI-driven initial population creation and AI-driven population crossover, enabling hybrid evolutionary + LLM optimization workflows out of the box.
Author / Contact
- Email:
gerardotous@gmail.com - LinkedIn: https://www.linkedin.com/in/gerardo-tous-vallespir-42491636/
Quick Start Β· Features Β· Operators Β· Examples Β· API Reference
β What Is DarwinGA?
DarwinGA is a genetic algorithm (GA) library that lets you solve optimization problems using the principles of natural evolution: selection, crossover, mutation, and survival of the fittest.
Instead of coding a solver, you describe:
- What a solution looks like (chromosome)
- How to evaluate it (fitness function)
- How it mutates and combines (genetic operators)
The engine evolves a population of candidate solutions across generations until it converges on the best one.
Unlike most .NET GA libraries, DarwinGA can delegate both generation-0 seeding and population-level crossover to AI providers (OpenAI-compatible), combining classic genetic operators with intelligent guidance.
π€ Why DarwinGA Is Different
Most GA frameworks focus only on traditional operators. DarwinGA adds an AI-native evolution flow:
- AI Initial Population Seeding via
PopulationCreator - AI Population Crossover via
IPopulationCrosser/AICrosser - Works with OpenAI-compatible providers (OpenAI, DeepSeek, LM Studio) and with Claude through the Claude Code CLI (
ClaudeCodeProvider) - Can be enabled independently (only seeding, only crossover, or both)
This enables faster convergence in many complex search spaces by combining exploration from GA with structured guidance from LLMs.
β¨ Features at a Glance
| Feature | DarwinGA | Other .NET GA libs |
|---|---|---|
| π― Fully generic β evolve any type | β | Partial |
| β‘ Built-in parallel fitness evaluation | β | β / External |
| 𧬠11 mutation operators (binary) | β | 2β4 typical |
| π 8 crossover operators (binary) | β | 2β3 typical |
| π 7 selection strategies | β | 2β3 typical |
| π Built-in diversity preservation | β | β |
| π§ Age-based selection to prevent stagnation | β | β |
| π² Deterministic seed for reproducible runs | β | Partial |
| πΎ Checkpoint / Resume support | β | Rare |
| π Adaptive mutation/crossover with stagnation tracking | β | Rare |
| π§ Neuroevolution support (ActivationNetwork) | β | β |
| π Generation statistics (avg/min/max/stddev + diversity index) | β | β |
| ποΈ Island Model (multi-population + migration) | β | β |
| β»οΈ Reinsertion strategies (carry elites to next generation) | β | β |
π€ AI initial population seeding (PopulationCreator) |
β | β |
π€ AI population crossover (IPopulationCrosser) |
β | β |
| β CancellationToken support (stop runs safely) | β | β |
| π Extensible via interfaces | β | Partial |
| πͺΆ Lightweight β minimal dependencies | β | Varies |
ποΈ Modern .NET 8 with required properties |
β | Often .NET Standard / Legacy |
π¦ Installation
dotnet add package DarwinGA
Or via the NuGet Package Manager in Visual Studio:
Install-Package DarwinGA
π Quick Start
Three steps to evolve a solution:
1οΈβ£ Define Your Chromosome
Implement IGAEvolutional<T> on any class that represents a candidate solution:
using DarwinGA.Interfaces;
public class PriceSolution : IGAEvolutional<PriceSolution>
{
public double Price { get; set; }
}
2οΈβ£ Implement Mutation and Crossover
using DarwinGA;
using DarwinGA.Interfaces;
public class PriceMutation : IMutation<PriceSolution>
{
public void Apply(PriceSolution evo, double mutationProb)
{
if (MyRandom.NextDouble() < mutationProb)
evo.Price += MyRandom.NextDouble() * 2 - 1;
}
}
public class PriceCrossover : ICross<PriceSolution>
{
public PriceSolution Apply(PriceSolution a, PriceSolution b)
{
return new PriceSolution
{
Price = (a.Price + b.Price) / 2.0
};
}
}
3οΈβ£ Configure and Run
using DarwinGA;
using DarwinGA.Selections;
using DarwinGA.Terminations;
var ga = new GeneticAlgorithm<PriceSolution>
{
PopulationCreator = size => Enumerable.Range(0, size)
.Select(_ => new PriceSolution { Price = MyRandom.NextDouble() * 100 })
.ToList(),
Fitness = solution =>
{
double demand = 100 - solution.Price;
return solution.Price * demand; // maximize profit
},
Mutation = new PriceMutation(),
Cross = new PriceCrossover(),
MutationProbability = 0.05,
Selection = new TournamentSelection(tournamentSize: 5),
Termination = new GenerationNumTermination(maxGenerations: 300),
OnNewGeneration = result =>
{
Console.WriteLine($"Gen {result.GenerationNum}: Best Fitness = {result.BestFitness:F2}");
}
};
ga.Run(populationSize: 100);
That's it. The algorithm handles the rest.
π€ AI: Initial Population + Population Crossover
DarwinGA permite integrar IA en dos puntos clave del ciclo evolutivo:
- CreaciΓ³n de poblaciΓ³n inicial por IA
- Asigna
PopulationCreatorpara generar la generaciΓ³n 0 con un proveedor IA.
- Asigna
- Crossover de poblaciΓ³n por IA
- Asigna
PopulationCrosser(por ejemploAICrosser) para generar descendencia a nivel de poblaciΓ³n.
- Asigna
Ejemplo conceptual:
using DarwinGA;
using DarwinGA.AI;
using DarwinGA.Evolutionals.BinaryEvolutional;
using DarwinGA.Evolutionals.BinaryEvolutional.Crossers;
var ga = new GeneticAlgorithm<BinaryEvolutional>
{
PopulationCreator = size =>
{
// Crear poblaciΓ³n inicial (puede ser IA + fallback aleatorio)
return CreateInitialPopulationWithAI(size);
},
PopulationCrosser = new AICrosser(
aiProvider,
offspringCount: populationSize / 10, // el resto lo completa el Cross clΓ‘sico
serializePopulation,
deserializePopulation,
systemMessage: "You are an expert GA crossover operator"
),
// Cross es obligatorio: completa la poblaciΓ³n con los hijos que no genera el PopulationCrosser
// (y la llena entera si la IA falla o devuelve hijos con una longitud de cromosoma incorrecta)
Cross = new UniformCross(),
Fitness = EvaluateFitness,
Mutation = mutation,
Selection = selection,
Termination = termination,
OnNewGeneration = _ => { }
};
ga.Run(populationSize);
AICrosser recibe los padres seleccionados ordenados de mejor a peor, descarta los hijos con una longitud de cromosoma distinta y, tras MaxConsecutiveFailures fallos seguidos (3 por defecto), se desactiva (IsDisabled) y se usa solo el cruce clΓ‘sico. Los proveedores no guardan historial: cada llamada envΓa el mensaje de sistema actual y un ΓΊnico prompt.
En DarwinGA.Example, este flujo ya estΓ‘ integrado para activar de forma independiente (ambas opciones estΓ‘n activadas por defecto; responde n para desactivarlas):
- Use AI for initial population creation? (Y/n)
- Use AI for crossover? (Y/n)
π§° Built-in Operators
π Selection Strategies (7)
Choose how parents are picked for breeding:
| Strategy | Class | Description |
|---|---|---|
| Tournament | TournamentSelection |
Picks the best of k random contenders. Good general-purpose default. |
| Elite | EliteSelecction |
Takes the top fraction by fitness deterministically. |
| Roulette Wheel | RouletteWheelSelection |
Probability proportional to fitness value when every fitness is finite and >= 0 (zero is never picked); otherwise (negative, NaN, infinite or all zero) rank weights. |
| Rank | RankSelection |
Probability proportional to rank, not raw fitness (ties share the average rank). Avoids super-fit dominance. |
| Truncation | TruncationSelection |
Deterministically takes the top N%. Simple and fast. |
| Stochastic Universal Sampling | StochasticUniversalSamplingSelection |
Evenly-spaced pointers over the fitness distribution (proportional when every fitness is finite and >= 0, otherwise rank weights). Less bias than roulette. |
| Age-Based (decorator) | AgeBasedSelection |
Wraps any selection and penalizes individuals that survive too many generations. |
Every fraction-based selection returns at least two parents (min(2, n)). Negative or zero fitness is fine everywhere (NaN counts as the worst), and the age and similarity penalties never reward a negative fitness.
// Tournament selection keeping top 50% via 5-way tournaments
Selection = new TournamentSelection(tournamentSize: 5, selectionFraction: 0.5)
// Age-based on top of tournament (5% penalty per generation)
EnableAgeBasedSelection = true,
AgePenaltyFactor = 0.05,
Selection = new TournamentSelection(5)
EnableAgeBasedSelection wraps Selection in an AgeBasedSelection only while the algorithm runs; the Selection property itself is not modified.
𧬠Mutation Operators β Binary (11)
Control how chromosomes are randomly altered:
| Mutation | Class | Behavior |
|---|---|---|
| Random | RandomMutation |
Flips each bit independently with given probability. |
| K-Flip | KFlipMutation |
Flips exactly k distinct random bits. |
| Block Flip | BlockFlipMutation |
Flips one random contiguous block of bits. |
| Multi-Block Flip | MultiBlockFlipMutation |
Flips the union of several random contiguous blocks (overlapping bits flip once). |
| Bit Mask | BitMaskMutation |
Applies a random bitmask (each bit set with the given probability) to flip bits. |
| Geometric Block | GeometricBlockMutation |
Block size sampled from a geometric distribution. |
| Non-Uniform | NonUniformMutation |
Flips k ~ Binomial(n, p) distinct random bits. The intensity does not change over time. |
| Run Flip | RunFlipMutation |
Flips a run of consecutive identical bits. |
| Scramble | ScrambleMutation |
Shuffles the order of bits in a random segment. |
| Shift Rotation | ShiftRotationMutation |
Rotates the whole chromosome left or right by a random shift. |
| Swap Pairs | SwapPairsMutation |
Swaps values of randomly chosen pairs of genes. |
The meaning of MutationProbability depends on the operator: RandomMutation, BitMaskMutation and NonUniformMutation apply it per gene; the other binary mutations apply it once per chromosome (the probability that the chromosome is mutated at all).
π Crossover Operators β Binary (8)
Define how two parents produce offspring:
| Crossover | Class | Behavior |
|---|---|---|
| One-Point | OnePointCross |
Single cut point; left from parent A, right from parent B. |
| Two-Point | TwoPointCross |
Two cut points; middle segment swapped. |
| N-Point | NPointCross |
Generalized n cut points. |
| Uniform | UniformCross |
Each gene chosen from either parent with a given probability. |
| Arithmetic | ArithmeticCross |
Majority vote over windows of genes (ties give 0); the child is constant within each window. |
| HUX (Half-Uniform) | HUXCross |
Swaps exactly half of the differing bits. |
| Partial | PartialCross |
Crosses a partial random segment. |
| Segment Swap | SegmentSwapCross |
Swaps a random contiguous segment between parents. |
π Termination Conditions (3)
Stop evolution when a condition is met:
| Condition | Class | Description |
|---|---|---|
| Generation Limit | GenerationNumTermination |
Stops after N generations. |
| Fitness Threshold | FitnessThresholdTermination |
Stops when the best (raw) fitness reaches a target value. |
| Any | AnyTermination |
Stops when any of the given conditions is met. |
// Stop after 500 generations
Termination = new GenerationNumTermination(500)
// Stop when fitness >= 0.98
Termination = new FitnessThresholdTermination(0.98)
// Stop at fitness >= 0.98 or after 500 generations, whichever comes first
Termination = new AnyTermination(new FitnessThresholdTermination(0.98), new GenerationNumTermination(500))
β»οΈ Reinsertion Strategies (1)
Control which individuals from the current generation survive directly into the next (elitism):
| Strategy | Class | Description |
|---|---|---|
| Best | ReinsertBest<T> |
Carries the top N fittest distinct individuals unchanged into the next generation. |
// Keep the single best individual across generations (count defaults to 1)
Reinsert = new ReinsertBest<MyChromosome>(count: 1)
// Keep the top 3 elites
Reinsert = new ReinsertBest<MyChromosome>(count: 3)
Reinsertion prevents the best solution found so far from being lost to random variation, whatever selection you use (elitism is guaranteed). Reinsert is null by default (no reinsertion). When it is set, breeding produces populationSize - survivors children and the survivors are appended at the end of the next population, after the bred children: they are not mutated and they never replace population-crosser (AI) children.
You can define custom strategies by implementing IReinsert<T>:
public interface IReinsert<T> where T : IGAEvolutional<T>
{
// population: the whole evaluated population, with its raw (unpenalized) fitness
T[] GetSurvivors(FitnessResult[] population);
}
π Diversity Preservation
Prevent premature convergence by penalizing similar individuals:
EnableDiversity = true,
// Define how to measure distance between two individuals
DiversityMetric = new DelegateDiversityMetric<MyChromosome>(
(a, b) => /* return a double representing distance */
),
// Penalize fitness of similar individuals (higher factor = stronger penalty)
DiversityStrategy = new SimilarityPenaltyStrategy<MyChromosome>(penaltyFactor: 0.5)
You can also implement IDiversityMetric<T> and IDiversityStrategy<T> for custom behavior.
The penalized fitness is used only for selection: BestFitness, BestElement, the statistics, the termination and the reinsertion always see the raw fitness.
β‘ Parallel Processing
Speed up evolution on multi-core machines:
// Evaluate fitness in parallel across the population
EnableParallelEvaluation = true,
// Optionally configure parallelism limits (MaxDegreeOfParallelism, TaskScheduler and CancellationToken are honored)
ParallelOptions = new ParallelOptions { MaxDegreeOfParallelism = 8 }
Breeding (selection, crossover and mutation) always runs sequentially. A run can be cancelled with Run(populationSize, cancellationToken) or with ParallelOptions.CancellationToken; both are honored.
π² Reproducibility, Adaptive Rates, and Checkpoints
Use a fixed seed for reproducible runs:
RandomSeed = 12345,
The seed is applied to the shared MyRandom source, which stays thread-safe when seeded (so it also works with EnableParallelEvaluation). The seed is process-wide: concurrent runs share and interleave the same sequence. new BinaryEvolutional(size) and the random ActivationNetworkEvolutional constructors draw from MyRandom, so the seed applies to them too.
Enable adaptive mutation/crossover when fitness stagnates:
EnableAdaptiveRates = true,
// Stagnation = generations since the best fitness so far improved by more than ImprovementEpsilon
StagnationThreshold = 10,
ImprovementEpsilon = 1e-12,
// After StagnationThreshold stagnant generations at the base rates, mutation rises by one step
// (and crossover falls by one step) every StagnationThreshold generations, at most MaxEscalationSteps steps
AdaptiveMutationStep = 0.01,
AdaptiveCrossoverStep = 0.02,
MaxEscalationSteps = 5,
AdaptiveMutationMin = 0.001,
AdaptiveMutationMax = 0.5,
AdaptiveCrossoverMin = 0.2,
AdaptiveCrossoverMax = 1.0,
An improvement restores the base rates (the configured MutationProbability and CrossoverProbability). Mutation never goes above AdaptiveMutationMax and crossover stays within AdaptiveCrossoverMin/AdaptiveCrossoverMax. If an escalation brings no improvement, the rates go back to the base values for twice as long as the previous base phase before escalating again. After Run returns, the MutationProbability and CrossoverProbability properties hold the configured values again.
During OnNewGeneration, each GenerationResult<T> now includes:
MutationProbability(rate used in this generation)CrossoverProbability(rate used in this generation)StagnationGenerations(generations since the best fitness so far last improved by more thanImprovementEpsilon)
Checkpoint and resume a run:
var ga = new GeneticAlgorithm<MyChromosome>
{
// ... required setup ...
Termination = new GenerationNumTermination(100)
};
ga.Run(populationSize: 100);
// Save in-memory snapshot (population + generation + adaptive state)
var checkpoint = ga.CreateCheckpoint();
// Later, continue from the checkpoint. Its generation is evaluated again, so a run that already met
// its termination stops immediately unless the termination is extended first.
ga.Termination = new GenerationNumTermination(200);
ga.Run(checkpoint);
RandomSeed (if set) is applied again on resume, so the continuation is reproducible but not identical to an uninterrupted run.
Checkpoints copy the individuals with CloneElement when it is set; otherwise elements that implement ICloneable (such as BinaryEvolutional and ActivationNetworkEvolutional) are cloned, and other types are kept by reference. For your own types, provide a clone function:
CloneElement = x => /* return a deep copy of x */
π§ Neuroevolution Support
DarwinGA includes built-in support for evolving neural networks via ActivationNetworkEvolutional:
using DarwinGA;
using DarwinGA.Evolutionals.ActivationNetworkEvolutional;
using DarwinGA.Selections;
using DarwinGA.Terminations;
var ga = new GeneticAlgorithm<ActivationNetworkEvolutional>
{
PopulationCreator = size => Enumerable.Range(0, size)
.Select(_ => new ActivationNetworkEvolutional(
neuronsCount: new[] { 5, 3, 1 },
inputsCount: 4))
.ToList(),
Fitness = individual =>
{
// Evaluate the neural network on your dataset
double[] output = individual.NeuralNetwork.Compute(inputData);
return CalculateAccuracy(output, expected);
},
Mutation = new ActivationNetworkMutation(),
Cross = new ActivationNetworkCrossover(),
Selection = new TournamentSelection(5),
Termination = new GenerationNumTermination(1000),
OnNewGeneration = r =>
Console.WriteLine($"Gen {r.GenerationNum}: {r.BestFitness:F4}")
};
ga.Run(populationSize: 50);
Evolve neural network weights and thresholds without backpropagation β ideal for reinforcement learning, game AI, and control systems.
new ActivationNetworkEvolutional(neuronsCount, inputsCount[, activationFunction])randomizes weights and thresholds in [-1, 1) withMyRandom(soRandomSeedapplies). The activation function (Sigmoid by default) is preserved by crossover and mutation.ActivationNetworkCrossoverstarts from a copy of one parent and takes each weight and threshold from the other parent with probability 0.5.ActivationNetworkMutationperturbs weights and thresholds by up to Β±MutationRange. WithDynamicLayers = trueit can also add or remove one neuron of a hidden layer (bounded byMaxNeuronsPerLayer); the number of layers never changes.
π Full Example
The 0/1 Knapsack problem: choose a subset of items to maximize total value without exceeding a weight capacity.
using DarwinGA;
using DarwinGA.Diversity;
using DarwinGA.Evolutionals.BinaryEvolutional;
using DarwinGA.Evolutionals.BinaryEvolutional.Crossers;
using DarwinGA.Evolutionals.BinaryEvolutional.Mutations;
using DarwinGA.Selections;
using DarwinGA.Terminations;
var items = new (int weight, double value)[]
{
(12, 60), (7, 34), (11, 55), (8, 40),
(9, 42), (6, 30), (13, 70), (5, 25)
};
int capacity = 30;
var ga = new GeneticAlgorithm<BinaryEvolutional>()
{
PopulationCreator = size =>
{
var population = new List<BinaryEvolutional>(size);
for (int p = 0; p < size; p++)
{
var chr = new BinaryEvolutional(items.Length);
for (int i = 0; i < items.Length; i++)
chr.SetGen(i, MyRandom.NextDouble() < 0.5);
population.Add(chr);
}
return population;
},
Fitness = individual =>
{
int w = 0;
double v = 0;
for (int i = 0; i < individual.Size; i++)
{
if (!individual.GetGen(i)) continue;
w += items[i].weight;
v += items[i].value;
}
if (w <= capacity) return v;
int extra = w - capacity;
return v - (extra * extra * 5.0); // penalty
},
MutationProbability = 0.10,
Mutation = new KFlipMutation(2),
Cross = new UniformCross(0.5),
Selection = new TournamentSelection(8),
// Fitness is the total value (not normalized), so stop by generations (the optimum of this instance is 155)
Termination = new GenerationNumTermination(100),
EnableParallelEvaluation = true,
EnableDiversity = true,
DiversityMetric = new DelegateDiversityMetric<BinaryEvolutional>((a, b) =>
{
int diff = 0;
for (int i = 0; i < a.Size; i++)
if (a.GetGen(i) != b.GetGen(i)) diff++;
return diff;
}),
DiversityStrategy = new SimilarityPenaltyStrategy<BinaryEvolutional>(penaltyFactor: 0.6),
OnNewGeneration = result =>
{
Console.WriteLine(
$"Gen {result.GenerationNum,-4} | Best: {result.BestFitness:F2} | Avg: {result.AverageFitness:F2} | Div: {result.DiversityIndex:F2}");
}
};
ga.Run(populationSize: 120);
π API Reference
GeneticAlgorithm<T> β Main Engine
| Property | Type | Description |
|---|---|---|
PopulationCreator |
Func<int, List<T>> |
Creates the full initial population (can be random, heuristic, or AI-generated). Required. |
Fitness |
Func<T, double> |
Evaluates how good a solution is. Higher = better. Required. |
Mutation |
IMutation<T> |
Mutation operator. Required. |
Cross |
ICross<T> |
Pairwise crossover operator. Required, also with a PopulationCrosser: it fills every slot the population crosser leaves empty. |
PopulationCrosser |
IPopulationCrosser<T>? |
Optional population-level crossover strategy (ideal for AI-guided crossover). Receives the selected parents sorted best first; its children come first in the next population. |
Selection |
ISelection |
Parent selection strategy. Required. |
Termination |
ITermination |
When to stop evolving. Required. |
OnNewGeneration |
Action<GenerationResult<T>> |
Callback invoked after each generation (BestFitness/BestElement are the population maximum of the raw fitness). Required. |
Reinsert |
IReinsert<T>? |
Reinsertion strategy. Survivors are appended after the bred children, unchanged and not mutated. Optional. |
MutationProbability |
double |
Passed to the mutation operator: per gene for RandomMutation, BitMaskMutation and NonUniformMutation, once per chromosome for the other binary mutations. Default: 0.01. |
CrossoverProbability |
double |
Probability of applying crossover when creating a child (otherwise the child is a copy of one parent). Default: 1.0. |
RandomSeed |
int? |
Fixed seed for reproducible runs (process-wide; applied again on resume). |
CloneElement |
Func<T, T>? |
Optional deep copy, used for children that skip crossover, checkpoints and migrants. When null, ICloneable elements are cloned. |
EnableParallelEvaluation |
bool |
Evaluate fitness in parallel. Default: false. |
ParallelOptions |
ParallelOptions |
Configure parallelism (max threads, task scheduler, cancellation token). |
EnableDiversity |
bool |
Enable diversity preservation (penalized fitness used only for selection). Default: false. |
DiversityMetric |
IDiversityMetric<T>? |
Distance function between individuals. |
DiversityStrategy |
IDiversityStrategy<T>? |
Strategy to adjust fitness based on diversity. |
EnableAgeBasedSelection |
bool |
Penalize long-lived individuals (wraps Selection during the run without modifying the property). Default: false. |
AgePenaltyFactor |
double |
Fitness penalty per generation of age. Default: 0.05. |
EnableAdaptiveRates |
bool |
Enable adaptive mutation/crossover. Default: false. |
StagnationThreshold |
int |
Stagnant generations at the base rates before escalating, and generations between escalation steps. Default: 10. |
ImprovementEpsilon |
double |
Minimum increase of the best fitness so far that counts as an improvement. Default: 1e-12. |
AdaptiveMutationStep |
double |
Mutation increase per escalation step. Default: 0.01. |
AdaptiveCrossoverStep |
double |
Crossover decrease per escalation step. Default: 0.02. |
MaxEscalationSteps |
int |
Maximum escalation steps before the rates go back to the base values. Default: 5. |
AdaptiveMutationMin/Max |
double |
Mutation bounds while adapting. Default: 0.001 / 0.5. |
AdaptiveCrossoverMin/Max |
double |
Crossover bounds while adapting. Default: 0.2 / 1.0. |
LastCheckpoint |
GeneticAlgorithmCheckpoint<T>? |
Last available in-memory checkpoint snapshot. |
Main Methods
| Method | Description |
|---|---|
Run(int populationSize) |
Starts a new evolution run (populationSize >= 2). |
Run(int populationSize, CancellationToken) |
Starts a new evolution run with cancellation support (ParallelOptions.CancellationToken is honored too). |
Run(GeneticAlgorithmCheckpoint<T> checkpoint) |
Resumes from a checkpoint (its generation is evaluated again: extend Termination to continue a finished run). |
Run(GeneticAlgorithmCheckpoint<T> checkpoint, CancellationToken) |
Resumes from a checkpoint with cancellation support. |
CreateCheckpoint() |
Returns a snapshot of the latest resumable state. |
Key Interfaces
| Interface | Purpose |
|---|---|
IGAEvolutional<T> |
Marker for any evolvable type (your chromosome). |
IMutation<T> |
void Apply(T evo, double mutationProb) β mutate an individual. |
ICross<T> |
T Apply(T a, T b) β produce a child from two parents. |
IPopulationCrosser<T> |
List<T> CrossPopulation(List<T> parents) β generate offspring from the selected parents (sorted best first). A cancellable overload CrossPopulation(parents, CancellationToken) has a default implementation. |
ISelection |
IEnumerable<FitnessResult> Select(...) β choose parents from the population. |
ITermination |
bool ShouldTerminate(GenerationResultBase result) β stop condition. |
IDiversityMetric<T> |
double Distance(T a, T b) β measure distance between individuals. |
IDiversityStrategy<T> |
Adjust fitness scores to reward diversity. |
IReinsert<T> |
T[] GetSurvivors(FitnessResult[] population) β select individuals of the whole evaluated population (raw fitness) to carry unchanged into the next generation. |
π§ How to Think in Evolutionary Terms
Instead of asking:
β "How do I solve this problem?"
Ask:
β "What does a solution look like?" β Define your chromosome
β "How do I measure if it's good?" β Write a fitness function
β "How can I slightly modify it?" β Choose mutation & crossover operators
The algorithm handles the rest.
π‘ Tips for Good Results
| β Do | β Avoid |
|---|---|
| Design a meaningful fitness function | Fitness too flat β no evolutionary pressure |
| Use a representation that can evolve smoothly | Mutation too aggressive β chaotic search |
| Enable diversity for complex landscapes | No diversity β premature convergence |
| Tune mutation probability (start ~0.01β0.10) | Overfitting to a single test case |
| Try different selection strategies | Using only elitism (loses diversity) |
π₯ Use Case Ideas
DarwinGA can optimize any problem you can encode as a chromosome:
| Domain | Example |
|---|---|
| π° Pricing | Find the price that maximizes profit |
| π¦ Logistics | Optimize warehouse placement or bin packing |
| π Routing | Evolve delivery routes (TSP variants) |
| π§ Neuroevolution | Evolve neural network weights for game AI |
| π― Strategy | Optimize game strategies or decision trees |
| π Feature Selection | Select the best subset of features for ML models |
| ποΈ Scheduling | Job-shop scheduling, resource allocation |
| π§ Parameter Tuning | Hyperparameter optimization for any system |
π€ Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
π Repository: https://github.com/G3r4rd00/DarwinGA
π« Contact
- Email:
gerardotous@gmail.com - LinkedIn: https://www.linkedin.com/in/gerardo-tous-vallespir-42491636/
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
π License
This project is licensed under the MIT License.
<div align="center">
Made with 𧬠by Gerardo Tous
If DarwinGA helped your project, consider giving it a β on GitHub!
</div>
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Accord.Neuro (>= 3.8.0)
- Accord.Statistics (>= 3.8.0)
- OpenAI (>= 2.10.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Correct best-individual and statistics reporting (raw fitness, also with diversity), elitism over the whole population, sign-safe selections and penalties, reworked adaptive rates, fixed island migration, thread-safe seeded random source, OpenAI-compatible providers (DeepSeek no longer depends on DeepSeek.NET) and hardened Claude Code provider.