darwin 0.4.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 23

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]
0
Health:
Code health derived from static analysis. [more]
49
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
42
Overall:
Weighted score of the above. [more]
23
Learn more about scoring.

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

  • Dart: 2.7.0
  • pana: 0.13.2

Health suggestions

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

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

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

line 10 col 21: Use generic function type syntax for parameters.

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

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

line 34 col 5: Omit type annotations for local variables.

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

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

line 43 col 33: Unnecessary new keyword.

line 59 col 23: Private field could be final.

line 59 col 37: Use collection literals when possible.

line 59 col 37: Unnecessary new keyword.

line 64 col 8: Private field could be final.

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

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

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

line 28 col 23: Use collection literals when possible.

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.

Fix additional 6 files with analysis or formatting issues. (-22.01 points)

Additional issues in the following files:

  • lib/src/phenotype.dart (12 hints)
  • lib/src/evaluator.dart (11 hints)
  • lib/src/evaluator_multithreaded.dart (11 hints)
  • lib/darwin.dart (6 hints)
  • lib/src/generation.dart (4 hints)
  • lib/src/result.dart (1 hint)

Maintenance suggestions

Package is getting outdated. (-48.49 points)

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