Settings

Theme

A Very Simple Genetic Algorithm Written in Swift 3

gist.github.com

105 points by brothrock 9 years ago · 41 comments

Reader

LoSboccacc 9 years ago

> let POP_SIZE = 50

c'mon everyone knows genetic population is 30

(I kid I kid but that number has an interesting story https://statswithcats.wordpress.com/2010/07/11/30-samples-st... )

  • jast 9 years ago

    It's not the population size, but the number of times you run the evolutionary algorithm with different seeds.

hellofunk 9 years ago

> Note -- this is much slower than the python version

Surprising. Swift, a compiled, static language, is slower than python in this use case?

  • mpweiher 9 years ago

    Yeah. Swift is unique in that it features both incredibly slow compile times (in the good case, in bad cases it can abort due to timeouts on one line, 40 character expressions) AND slow runtime speed, especially when not optimising.

    Code without the optimiser can easily be several hundred to more than a thousand times slower than optimised code. And of course the optimiser makes compile times even slower.

    It's really quite a spectacle.

  • nicky0 9 years ago

    This is just speculation, but slowness might be because this is being run as a script via /usr/bin/swift, rather than being precompiled into a binary.

    Certainly I have found my own Swift scripts seem to be a lot slower than compiled programs.

    EDIT: from my own test with 1000 iterations, script version 17s, compiled version (release build) 5.4s

  • rookie 9 years ago

    My guess it is all of the string converting back and forth to character arrays.

  • CorvusCrypto 9 years ago

    stuff like this can't help

    var n = Double(arc4random_uniform(UInt32(weightTotal * 1000000.0))) / 1000000.0

    • brothrockOP 9 years ago

      Yep totally, going to take another pass to try and make it faster. This was just a make it work experiment. Had a few fun troubles with random, but at the moment it just crawls :(.

    • sdegutis 9 years ago

      Out of curiosity, what's wrong with that line? Why is it slow?

      • CorvusCrypto 9 years ago

        starting from inside the arc4random call you have double float multiplication, then cast to uint32, then the arc4rand call, then the double cast for the uint32 result and then a double float division. this adds up in instructions.

cfdrake 9 years ago

Did not expect to see "cfdrake" when I initially clicked the link - nice port! :)

Agreed with e28eta - I actually saw RubikSwift demoed at Swift Summit a few days ago as well and enjoyed it. Definitely check it out if you're interested in the topic.

e28eta 9 years ago

I thought this was going to be a link to [1], from a talk given at Swift Summit a couple days ago. I haven't really looked at the code for either one to compare them, but for people who are interested, here's a second simple genetic algorithm in Swift (found in RubikSwift/GeneticsSolver.swift)

https://github.com/JaviSoto/RubikSwift

  • nojvek 9 years ago

    Swift looks a lot like Typescript. This is the first time I've read swift code and I'm taken by surprise by its elegance.

    I'm gonna give it a shot

    • bsaul 9 years ago

      swift is a fantastic language, but it's absolutely not as mature as other language you're probably used to. In particular, you may experience a lot of compiler crash, typing bugs, and very bad compile time.

      But i'm 100% certain it's going to be one of the most used language server side in the coming years.

    • melling 9 years ago

      Here's a little Swift Cookbook to get more of a flavor:

      http://www.h4labs.com/dev/ios/swift_cookbook.html

proyb2 9 years ago

The other languages are here: https://github.com/jsvazic/GAHelloWorld

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection