darwin 0.4.1

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

example/example.dart

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>()
        ..members
            .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>(
    firstGeneration,
    evaluator,
    breeder,
  );

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

  // Print all members of the last generation when done.
  algo.generations.last.members
      .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();

  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:


dependencies:
  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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
34
Health:
Code health derived from static analysis. [more]
75
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
34
Overall:
Weighted score of the above. [more]
46
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

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

Analysis of lib/isolate_worker.dart reported 17 hints, including:

line 43 col 33: Unnecessary new keyword.

line 59 col 37: Unnecessary new keyword.

line 68 col 19: Use ; instead of {} for empty constructor bodies.

line 71 col 24: Unnecessary new keyword.

line 72 col 21: Unnecessary new keyword.

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

Analysis of lib/src/breeder.dart reported 16 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 34 col 34: Unnecessary new keyword.

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

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

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

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

line 28 col 23: Unnecessary new keyword.

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 42 col 9: Unnecessary new keyword.

Fix additional 5 files with analysis or formatting issues. (-7.95 points)

Additional issues in the following files:

  • lib/src/evaluator.dart (5 hints)
  • lib/src/phenotype.dart (4 hints)
  • lib/src/evaluator_multithreaded.dart (3 hints)
  • lib/src/generation.dart (3 hints)
  • lib/src/result.dart (1 hint)

Maintenance suggestions

Package is getting outdated. (-55.89 points)

The package was last published 81 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.

Dependencies

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