7 ms·
A Very Simple Genetic Algorithm Written in Swift 3
- cfdrake 10y agoDid 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.
- hellofunk 10y ago> Note -- this is much slower than the python version Surprising. Swift, a compiled, static language, is slower than python in this use case?
- CorvusCrypto 10y agostuff like this can't help var n = Double(arc4random_uniform(UInt32(weightTotal * 1000000.0))) / 1000000.0
- brothrock 10y agoYep 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 10y agoOut of curiosity, what's wrong with that line? Why is it slow?
- CorvusCrypto 10y agostarting 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.
- nicky0 10y agoThis 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
- brothrock 10y agoah great point. I'll time it both ways and see where I'm at.
- pkaler 10y agoI compiled it down and did a quick profile. Looks like a lot of the time is spent on line 96, converting a Character to its ascii value. https://dl.dropboxusercontent.com/u/13740348/Screen%20Shot%202016-11-11%20at%2010.14.33%20AM.png https://dl.dropboxusercontent.com/u/13740348/Screen%20Shot%2...
- Someone 10y agoThat's because Character is very, very different from ASCII in Swift (a character in Swift holds an extended grapheme cluster. See https://developer.apple.com/reference/swift/character https://developer.apple.com/reference/swift/character). I also doubt the way they compute its ASCII value is optimal: extension Character { var asciiValue: UInt32? { return String(self) .unicodeScalars .filter{$0.isASCII} .first? .value } } Certainly if it is optimal, but probably also if it isn't, I would change the program to not do that conversion in inner loops.
- brothrock 10y agoThis is awesome guys. Thanks.
- rookie 10y agoReplaced String with Int array https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b467 https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b...
- mpweiher 10y agoYeah. 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.
- rookie 10y agoMy guess it is all of the string converting back and forth to character arrays.
- rookie 10y agoYep, replaced string with Int array and now it is much faster. https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b467 https://gist.github.com/rookie/3d9a848c6fefacc4254da921da79b... It now runs faster (when compiled, optimized) than the python code. xcrun -sdk macosx swiftc -O gen.swift
- nicky0 10y agoOoh yeah, 0m0.363s for 1000 iterations on mine. So the slowness was in the string manipulation and character conversion operations.
- SOLAR_FIELDS 10y agoI've learned from years of working on making code more performant that the biggest code smells to look at first are string operations.
- LoSboccacc 10y 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-standard-suggestion-or-superstition/ https://statswithcats.wordpress.com/2010/07/11/30-samples-st... )
- jast 10y agoIt's not the population size, but the number of times you run the evolutionary algorithm with different seeds.
- deleted 10y ago[deleted]
- zodPod 10y agoAre we all just sharing everything we write on here now? Is this notable for some reason?
- brothrock 10y agoI wanted an example of a simple genetic algorithm in swift, couldn't find it so I wrote it from something I found in python. 0% special, but thought i'd share. Take it or leave it, dude.
- deleted 10y ago[deleted]
- melling 10y agoShouldn't people should feel free to share whatever they want that's HN material? That's why we vote articles up. Sometimes the comments alone in a basic post add a lot of value.
- UhUhUhUh 10y agoAgreed. And voting the whole thing up.
- alexggordon 10y agoJust to add to this, if you don't like it, don't click on it/comment on it. The front page algorithm put this item here because people upvoted it, not because it queried a random selection of the user base and asked if it was "worthy".
- e28eta 10y agoI 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 10y agoSwift 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
- melling 10y agoHere's a little Swift Cookbook to get more of a flavor: http://www.h4labs.com/dev/ios/swift_cookbook.html http://www.h4labs.com/dev/ios/swift_cookbook.html
- bsaul 10y agoswift 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 10y agoThe other languages are here: https://github.com/jsvazic/GAHelloWorld https://github.com/jsvazic/GAHelloWorld
- proyb2 10y agoThe other languages are here: https://github.com/jsvazic/GAHelloWorld https://github.com/jsvazic/GAHelloWorld