darwin 0.4.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 40

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 pub get

Alternatively, your editor might support pub get or flutter pub 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';
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 Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21


Detected platforms: Flutter, web, other

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

Health suggestions

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

Analysis of lib/src/breeder.dart reported 7 hints, including:

line 3 col 23: Use lowercase_with_underscores when specifying a library prefix.

line 13 col 3: Prefer using /// for doc comments.

line 28 col 3: Prefer using /// for doc comments.

line 71 col 3: Prefer using /// for doc comments.

line 128 col 3: Prefer using /// for doc comments.

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

Analysis of lib/src/genetic_algorithm.dart reported 6 hints, including:

line 14 col 3: Prefer using /// for doc comments.

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.

line 53 col 3: Prefer using /// for doc comments.

line 58 col 3: Prefer using /// for doc comments.

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

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

line 8 col 1: Prefer using /// for doc comments.

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

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

line 36 col 3: Prefer using /// for doc comments.

Fix additional 4 files with analysis or formatting issues. (-3.99 points)

Additional issues in the following files:

  • lib/src/evaluator.dart (3 hints)
  • lib/isolate_worker.dart (2 hints)
  • lib/src/generation.dart (2 hints)
  • lib/src/evaluator_multithreaded.dart (1 hint)

Maintenance suggestions

Package is getting outdated. (-24.38 points)

The package was last published 64 weeks ago.

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