darwin 0.4.1

  • README.md
  • Example
  • Installing
  • Versions
  • 49

darwin #

Build Status

A genetic/evolutionary algorithm library for Dart. Given a population of phenotypes, an evaluator (fitness function), and time, the algorithm will evolve the population until it crosses given fitness threshold.

Read more about genetic algorithms on Wikipedia.

Features of this library:

  • Generic approach (anything can be a gene, as long as it can mutate)
  • User can tune crossover probability, mutation rate, mutation strength, etc.
  • Niching via fitness sharing
  • Experimental support for multithreaded computation

For up-to-date example use, please see example/example.dart.

0.4.1 #

  • Upgrade to Dart 2


import 'dart:async';
import 'dart:math';

import 'package:darwin/darwin.dart';

Future<Null> main() async {
  // Create first generation, either by random or by continuing with existing
  // progress.
  var firstGeneration =
      new Generation<MyPhenotype, bool, SingleObjectiveResult>()
            .addAll(new List.generate(10, (_) => new MyPhenotype.Random()));

  // Evaluators take each phenotype and assign a fitness value to it according
  // to some fitness function.
  var evaluator = new MyEvaluator();

  // Breeders are in charge of creating new generations from previous ones (that
  // have been graded by the evaluator). Their only required argument is
  // a function that returns a blank phenotype.
  var breeder = new GenerationBreeder<MyPhenotype, bool, SingleObjectiveResult>(
      () => new MyPhenotype())
    ..crossoverPropability = 0.8;

  var algo = new GeneticAlgorithm<MyPhenotype, bool, SingleObjectiveResult>(

  // Start the algorithm.
  await algo.runUntilDone();

  // Print all members of the last generation when done.
      .forEach((Phenotype ph) => print("${ph.genesAsString}"));

Random random = new Random();

class MyEvaluator
    extends PhenotypeEvaluator<MyPhenotype, bool, SingleObjectiveResult> {
  Future<SingleObjectiveResult> evaluate(MyPhenotype phenotype) {
    // This implementation just counts false values - the more false values,
    // the worse outcome of the fitness function.
    final result = new SingleObjectiveResult();
    result.value =
        phenotype.genes.where((bool v) => v == false).length.toDouble();
    return new Future.value(result);

class MyPhenotype extends Phenotype<bool, SingleObjectiveResult> {
  static int geneCount = 6;


  MyPhenotype.Random() {
    genes = new List<bool>(geneCount);
    for (int i = 0; i < geneCount; i++) {
      genes[i] = random.nextBool();

  bool mutateGene(bool gene, num strength) {
    return !gene;

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  darwin: ^0.4.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:darwin/darwin.dart';
Version Uploaded Documentation Archive
0.4.1 Jul 24, 2018 Go to the documentation of darwin 0.4.1 Download darwin 0.4.1 archive
0.4.0 Jan 27, 2018 Go to the documentation of darwin 0.4.0 Download darwin 0.4.0 archive
0.3.1 Nov 23, 2017 Go to the documentation of darwin 0.3.1 Download darwin 0.3.1 archive
0.2.5 Feb 3, 2016 Go to the documentation of darwin 0.2.5 Download darwin 0.2.5 archive
0.2.4 Jan 17, 2014 Go to the documentation of darwin 0.2.4 Download darwin 0.2.4 archive
0.2.3 Jan 17, 2014 Go to the documentation of darwin 0.2.3 Download darwin 0.2.3 archive
0.2.2 Jan 16, 2014 Go to the documentation of darwin 0.2.2 Download darwin 0.2.2 archive
0.2.1 Jan 14, 2014 Go to the documentation of darwin 0.2.1 Download darwin 0.2.1 archive
0.2.0 Jan 14, 2014 Go to the documentation of darwin 0.2.0 Download darwin 0.2.0 archive
0.1.1 Jan 14, 2014 Go to the documentation of darwin 0.1.1 Download darwin 0.1.1 archive

All 11 versions...

Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:darwin/darwin.dart.

Health suggestions

Fix lib/src/genetic_algorithm.dart. (-1 points)

Analysis of lib/src/genetic_algorithm.dart reported 2 hints:

line 36 col 19: Use = to separate a named parameter from its default value.

line 36 col 40: Use = to separate a named parameter from its default value.

Fix lib/src/phenotype.dart. (-1 points)

Analysis of lib/src/phenotype.dart reported 2 hints:

line 25 col 5: Don't explicitly initialize variables to null.

line 26 col 7: Don't explicitly initialize variables to null.

Fix lib/isolate_worker.dart. (-0.50 points)

Analysis of lib/isolate_worker.dart reported 1 hint:

line 114 col 31: Use = to separate a named parameter from its default value.

Fix lib/src/breeder.dart. (-0.50 points)

Analysis of lib/src/breeder.dart reported 1 hint:

line 135 col 69: Use = to separate a named parameter from its default value.

Maintenance suggestions

The description is too long. (-10 points)

Search engines display only the first part of the description. Try to keep the value of the description field in your package's pubspec.yaml file between 60 and 180 characters.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.24.0 <3.0.0
meta ^1.1.6 1.1.7
Dev dependencies
test >=0.12.30 <2.0.0