Mendel - Swift miliframework for implementing evolutionary/genetic algorithms

Overview

Mendel

Mendel - Swift miliframework for implementing evolutionary/genetic algorithms. Or, you know, Gregor Mendel.

Logo

This started out as an exercise in Swift and Functional Programming, but quickly turned into something bigger.

##Intro The framework provides an Engine protocol, describing the necessary interface for evolutionary/genetic computation using functions. Individual instantiation, population evaluation, selection, genetic operators (like mutation and crossover) and even termination conditions are all described as functions. This allows you to use techniques such as partial application and function composition to a great effect.

Here's a video showing the sample application that's build on top of Mendel in action.

##Canned Functions Mendel provides a number of canned implementations for some of those functions, see:

  • Selections for individual selection functions, such as RouletteWheel and StochasticUniversalSampling
  • Operators for genetic operators, such as Mutation and Crossover
  • TerminationConditions for termination conditions, such as terminating after a number of iterations (NumberOfIterations) or terminating when a given fitness threshold is reached (FitnessThreshold)

##More on Genetic Operators Genetic Operators can be easily piped using the >>> swift operator (which wraps the Pipe genetic operator). E.g. if you want to perform a crossover with probability 0.1 and then a mutation with probability 0.5, you can just pass Crossover(0.1) >>> Mutation(0.5) as your genetic operator.

The Split operator lets you split the genetic operator flow into two paths. E.g., if you want to perform a crossover(p=0.1) only on 30% of the population and a mutation(p=0.5) on the other 70% you can do Split(0.3, Crossover(0.1), Mutation(0.5)).

Using Pipe and Split together lets you build complex evolution schemes easily.

Additionally, there's a Parallel operator, which partitions the population in batches of batchSize and applies a genetic operator to those batches concurrently, returning only after all batches are processed.

##Simple Engine The framework provides a SimpleEngine class which implements the Engine protocol described below. It's a concrete implementation of a simple generational evolution engine and can be used out of the box. The two examples in the sample app were built using SimpleEngine.

//The Genetic Engine protocol
public protocol Engine {
    //The type that's being evolved
    typealias Individual : IndividualType
    //A collection of Individuals
    typealias Population = [Individual]
    //A collection of Individuals and their respective fitness scores
    typealias EvaluatedPopulation = [Score<Individual>]
    
    //MARK: Core function types
    
    //Used to instantiate a new arbitrary Individual
    typealias Factory = () -> Individual
    
    //Used to ge a fitness score for an Individual in a given Population
    typealias Evaluation = (Individual, Population) -> Fitness
    
    //Selection Function - used to select the next iteration's Population from
    //the current EvaluatedPopulation
    typealias Selection = (EvaluatedPopulation, FitnessKind, Int) -> Population
    
    //The Genetic Operator that is going to be called to modify the selected Population
    typealias Operator = Population -> Population
    
    //Termination predicate. When it returns yes, the evolution process is stopped
    typealias Termination = IterationData<Individual> -> Bool
    
    ////////////////////////////////////////////////////////////////////////////
    
    var fitnessKind: FitnessKind { get }
    
    var factory: Factory { get }
    
    var evaluation: Evaluation { get }
    
    var selection: Selection { get }
    
    var op: Operator { get }
    
    var termination: Termination? { get }
    
    //Called after each evolution step. Useful to update UI/inform user.
    var iteration: (IterationData<Individual> -> Void)? { get }
    
    //Starts the evolution process. This is a blocking call, it won't return until
    //`termination` returns true – make sure you aren't blocking UI.
    func evolve() -> Individual
}

Inspired by http://chriscummins.cc/s/genetics/ and the Functional Programming in Swift book. Hat tip to Watchmaker and Nacho Sotos' swift-genetics.

You might also like...
A Generic Priority Queue in Pure Swift

SwiftPriorityQueue SwiftPriorityQueue is a pure Swift (no Cocoa) implementation of a generic priority queue data structure, appropriate for use on all

Super lightweight DB written in Swift.
Super lightweight DB written in Swift.

Use of value types is recommended and we define standard values, simple structured data, application state and etc. as struct or enum. Pencil makes us

A fast Swift diffing library.

HeckelDiff Pure Swift implementation of Paul Heckel's A Technique for Isolating Differences Between Files Features This is a simple diff algorithm tha

NSCoding's counterpart for Swift structs.
NSCoding's counterpart for Swift structs.

Dekoter Why You Might Be Interested How Much Familiar It Feels One More Example What We've Learned from It Features Save an Object to UserDefaults Arc

A Distributed Value Store in Swift.
A Distributed Value Store in Swift.

Impeller is a Distributed Value Store (DVS) written in Swift. It was inspired by successful Distributed Version Control Systems (DVCSes) like Git and

🦀Amazingly incredible extraordinary lightning fast diffing in Swift
🦀Amazingly incredible extraordinary lightning fast diffing in Swift

DeepDiff ❤️ Support my apps ❤️ Push Hero - pure Swift native macOS application to test push notifications PastePal - Pasteboard, note and shortcut man

Swift library to generate differences and patches between collections.

Differ Differ generates the differences between Collection instances (this includes Strings!). It uses a fast algorithm (O((N+M)*D)) to do this. Featu

A Swift probability and statistics library

Probably Probably is a set of Swift structures for computing the probability and cumulative distributions of different probablistic functions. Right n

The simplest abstraction to synchronize local data with remote source. For iOS, wirtten in swift.

Purpose The simplest abstraction to synchronize local data with remote source. For iOS, written in swift. Overview Many applications uses remote serve

Comments
  • Simplified Operators

    Simplified Operators

    Just streamlined Operators. IMHO the struct is not really needed. The case of collisions could be already managed by the module. The functional operator >>> is now public thus there's no need to reimplement it in Operators.swift.

    Since the functional operators are exposed as part of the API, maybe they should be made all public. But I understand that as of now there might be a lot of confusion generated by different operators overload conventions :-/

    opened by perlfly 1
  • Fix broken headings in Markdown files

    Fix broken headings in Markdown files

    GitHub changed the way Markdown headings are parsed, so this change fixes it.

    See bryant1410/readmesfix for more information.

    Tackles bryant1410/readmesfix#1

    opened by bryant1410 0
Owner
saniul
saniul
Algorithms and data structures in Swift, with explanations!

Welcome to the Swift Algorithm Club! Here you'll find implementations of popular algorithms and data structures in everyone's favorite new language Sw

raywenderlich 27.3k Jan 8, 2023
EKAlgorithms contains some well known CS algorithms & data structures.

EKAlgorithms EKAlgorithms is a set of computer exercises implemented in Objective-C. Data structures, well known algorithms, CS curiosities, you name

Evgeny Karkan 2.4k Jan 4, 2023
Swift-extensions - Swift package extending the Swift programming language.

swift-extensions A package containing extensions for the Swift programming language. Contribution Reporting a bug If you find a bug, please open a bug

Alexandre H. Saad 2 Jun 12, 2022
Commonly used data structures for Swift

Swift Collections is an open-source package of data structure implementations for the Swift programming language.

Apple 2.7k Jan 5, 2023
Fast sorted collections for Swift using in-memory B-trees

Fast Sorted Collections for Swift Using In-Memory B-Trees Overview Reference Documentation Optimizing Collections: The Book What Are B-Trees? Why In-M

null 1.3k Dec 20, 2022
Simple diff library in pure Swift

Diff Simple diffing library in pure Swift. Installing You can use Carthage or Swift Package Manager to install Diff. Usage Start by importing the pack

Sam Soffes 120 Sep 9, 2022
A functional tool-belt for Swift Language similar to Lo-Dash or Underscore.js in Javascript

Dollar Dollar is a Swift library that provides useful functional programming helper methods without extending any built in objects. It is similar to L

Ankur Patel 4.2k Jan 4, 2023
Swift type modelling the success/failure of arbitrary operations.

Result This is a Swift µframework providing Result<Value, Error>. Result<Value, Error> values are either successful (wrapping Value) or failed (wrappi

Antitypical 2.5k Dec 26, 2022
Swift μ-framework for efficient array diffs and datasource adapters.

Buffer Swift μ-framework for efficient array diffs, collection observation and data source implementation. C++11 port here Installation cd {PROJECT_RO

Alex Usbergo 348 Aug 2, 2022
A Graph Data Structure in Pure Swift

SwiftGraph SwiftGraph is a pure Swift (no Cocoa) implementation of a graph data structure, appropriate for use on all platforms Swift supports (iOS, m

David Kopec 700 Dec 16, 2022