
A Very Simple Genetic Algorithm Written in Swift 3 - brothrock
https://gist.github.com/blainerothrock/efda6e12fe10792c99c990f8ff3daeba
======
LoSboccacc
> 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...](https://statswithcats.wordpress.com/2010/07/11/30-samples-standard-
suggestion-or-superstition/) )

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

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

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

~~~
nicky0
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

~~~
CorvusCrypto
good stuff. tbh though, only ~3x slowdown in scripting imo is pretty damn good
8)

~~~
lobster_johnson
Isn't the "scripting" part just automatically compiling behind the scenes
before running? It's not an interpreter or a VM. So the code runs just as fast
as the compiled version, it's just that you pay a startup cost.

~~~
tarentel
That's true between a debug build and just running it as a script I believe.
But a release version will be optimized, I imagine if they compared a debug
build to the scripted version they'd perform similarly.

~~~
nicky0
Can confirm, Debug build runs in similar time to script.

~~~
Someone
Difference is that Release builds do whole-module optimization in Swift 3.

That allows for more aggressive code specialization and inlining and can
remove more reference count updates ([https://swift.org/blog/whole-module-
optimizations/](https://swift.org/blog/whole-module-optimizations/))

------
cfdrake
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
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](https://github.com/JaviSoto/RubikSwift)

~~~
nojvek
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
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.

------
proyb2
The other languages are here:
[https://github.com/jsvazic/GAHelloWorld](https://github.com/jsvazic/GAHelloWorld)

