Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Amaral committed Apr 22, 2015
0 parents commit f877d00
Show file tree
Hide file tree
Showing 31 changed files with 2,837 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#Xcode
*.pbuser
*.mode1v3
*.mode2v3
*.perspectivev3
*.xcuserstate
project.xcworkspace/
xcuserdata/
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: objective-c
script:
- xctool -project Evolve.xcodeproj -sdk iphonesimulator -scheme Evolve build
18 changes: 18 additions & 0 deletions Demo/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// AppDelegate.h
// Evolve
//
// Created by Mike on 3/23/15.
// Copyright (c) 2015 Mike Amaral. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;



@end

30 changes: 30 additions & 0 deletions Demo/AppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// AppDelegate.m
// Evolve
//
// Created by Mike on 3/23/15.
// Copyright (c) 2015 Mike Amaral. All rights reserved.
//

#import "AppDelegate.h"
#import "WeaselProgramViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];

self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[WeaselProgramViewController new]];

[self.window makeKeyAndVisible];

return YES;
}

@end
41 changes: 41 additions & 0 deletions Demo/Other/Base.lproj/LaunchScreen.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6214" systemVersion="14A314h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6207"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB">
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" Copyright (c) 2015 Mike Amaral. All rights reserved." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye">
<rect key="frame" x="20" y="439" width="441" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Evolve" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-c2-rCX">
<rect key="frame" x="20" y="140" width="441" height="43"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="36"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="kId-c2-rCX" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="bottom" multiplier="1/3" constant="1" id="5cJ-9S-tgC"/>
<constraint firstAttribute="centerX" secondItem="kId-c2-rCX" secondAttribute="centerX" id="Koa-jz-hwk"/>
<constraint firstAttribute="bottom" secondItem="8ie-xW-0ye" secondAttribute="bottom" constant="20" id="Kzo-t9-V3l"/>
<constraint firstItem="8ie-xW-0ye" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="MfP-vx-nX0"/>
<constraint firstAttribute="centerX" secondItem="8ie-xW-0ye" secondAttribute="centerX" id="ZEH-qu-HZ9"/>
<constraint firstItem="kId-c2-rCX" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="fvb-Df-36g"/>
</constraints>
<nil key="simulatedStatusBarMetrics"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<point key="canvasLocation" x="548" y="455"/>
</view>
</objects>
</document>
68 changes: 68 additions & 0 deletions Demo/Other/Images.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
45 changes: 45 additions & 0 deletions Demo/Other/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.MikeAmaral.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
16 changes: 16 additions & 0 deletions Demo/Other/Supporting Files/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// main.m
// Evolve
//
// Created by Mike on 3/23/15.
// Copyright (c) 2015 Mike Amaral. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
19 changes: 19 additions & 0 deletions Demo/WeaselProgramViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// WeaselProgramViewController.h
// Evolve
//
// Created by Mike on 4/18/15.
// Copyright (c) 2015 Mike Amaral. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "EvolutionManager.h"

@interface WeaselProgramViewController : UIViewController <EvolutionDelegate>

@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UILabel *fittestOrganismLabel;

@property (nonatomic, strong) EvolutionManager *evolutionManager;

@end
123 changes: 123 additions & 0 deletions Demo/WeaselProgramViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//
// WeaselProgramViewController.m
// Evolve
//
// Created by Mike on 4/18/15.
// Copyright (c) 2015 Mike Amaral. All rights reserved.
//

#import "WeaselProgramViewController.h"

static NSString * const kTargetString = @"METHINKS IT IS LIKE A WEASEL";
static NSString * const kTargetDomain = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
static NSTimeInterval const kTimeIntervalPerGeneration = 0.05;

@interface WeaselProgramViewController ()

@end

@implementation WeaselProgramViewController

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

// Set up the demo UI.
self.title = @"The Weasel Program";

self.fittestOrganismLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 50)];
self.fittestOrganismLabel.center = self.view.center;
self.fittestOrganismLabel.textAlignment = NSTextAlignmentCenter;
self.fittestOrganismLabel.numberOfLines = 2;
[self.view addSubview:self.fittestOrganismLabel];

// Add a nav button to trigger starting the simulation.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStylePlain target:self action:@selector(startSimulation)];
}


#pragma mark - Starting the simulation

- (void)startSimulation {
// Stop the timer if it's already running.
if (self.timer.isValid) {
[self.timer invalidate];
}

// Reset the UI for the start of the simulation.
self.fittestOrganismLabel.textColor = [UIColor blackColor];

// Reset our population and start.
[self configurePopulationAndStart];
}


#pragma mark - Population initialization

- (void)configurePopulationAndStart {
// Create our population - which will be 50 organisms in size, will have a chromosome the length of our target string,
// and will have the domain defined above, which includes all capital letters and the space character.
Population *startingPopulation = [[Population alloc] initRandomPopulationWithSize:50 geneSequenceLength:kTargetString.length chromosomeDomain:kTargetDomain];

// Create our evolution manager with the starting population and set ourself as the delegate to recieve the appropriate callbacks.
self.evolutionManager = [[EvolutionManager alloc] initWithPopulation:startingPopulation];
self.evolutionManager.delegate = self;

// Start the simulation timer.
self.timer = [NSTimer scheduledTimerWithTimeInterval:kTimeIntervalPerGeneration target:self selector:@selector(continueWithNextGeneration) userInfo:nil repeats:YES];
}

- (void)continueWithNextGeneration {
[self evaluateFitnessForPopulation:self.evolutionManager.population];
[self.evolutionManager processNextGeneration];
}

- (void)evaluateFitnessForPopulation:(Population *)population {
// Pass all the organisms through our fitness function.
for (Organism *organism in population.organisms) {
organism.fitness = [self fitnessFunctionForOrganism:organism];
}
}

- (NSInteger)fitnessFunctionForOrganism:(Organism *)organism {
// Get the chromosome string from this organism.
NSString *chromosomeString = organism.chromosome.geneSequence;

// We're going to keep track of the number of characters in the chromosome that
// match our target string at the correct index. Each match increases the "fitness"
// of this organism.
NSInteger correctCharacters = 0;

for (NSInteger charIndex = 0; charIndex < organism.chromosome.geneSequence.length; charIndex++) {
if ([chromosomeString characterAtIndex:charIndex] == [kTargetString characterAtIndex:charIndex]) {
correctCharacters++;
}
}

return correctCharacters;
}


#pragma mark - Evolution delegate

- (void)population:(Population *)population didCompetedGeneration:(NSUInteger)generation fittestOrganisms:(NSArray *)fittestOrganisms offspring:(NSArray *)offspring completeNextGeneration:(NSArray *)nextGeneration {
// Get the fittest organism for this generation, which will be the first object in the fittest organisms array.
Organism *fittestOrganism = [fittestOrganisms firstObject];

// Get the string representation of the chromosome.
NSString *chromosomeString = fittestOrganism.chromosome.geneSequence;

// If it equals our target string, we can end the simulation.
if ([chromosomeString isEqualToString:kTargetString]) {
self.fittestOrganismLabel.text = [NSString stringWithFormat:@"Target organism acheived in generation %ld:\n%@", generation, chromosomeString];
self.fittestOrganismLabel.textColor = [UIColor redColor];

[self.timer invalidate];
}

// Otherwise just print out the generation number and the chromosome string for a visual representation of this generation.
else {
self.fittestOrganismLabel.text = [NSString stringWithFormat:@"Fittest organism for generation %ld:\n%@", generation, chromosomeString];
}
}

@end
15 changes: 15 additions & 0 deletions Evolve.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Pod::Spec.new do |s|

s.name = "Evolve"
s.version = "0.1"
s.summary = "An Objective-C evolution simulation engine."
s.homepage = "https://github.com/mamaral/Evolve"
s.license = "MIT"
s.author = { "Mike Amaral" => "[email protected]" }
s.social_media_url = "http://twitter.com/MikeAmaral"
s.platform = :ios
s.source = { :git => "https://github.com/mamaral/Evolve.git", :tag => "v0.1" }
s.source_files = "Evolve/Source/EvolutionManager.{h,m}", "Evolve/Source/Population.{h,m}", "Evolve/Source/Organism.{h,m}", "Evolve/Source/Chromosome.{h,m}", "Evolve/Source/Random.{h,m}"
s.requires_arc = true

end
Loading

0 comments on commit f877d00

Please sign in to comment.