Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Project 3. iDNA #38

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
478 changes: 478 additions & 0 deletions iDNA.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions iDNA.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "1.0">
</Bucket>
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6373D4F71689E47B00EE7CDA"
BuildableName = "iDNA.app"
BlueprintName = "iDNA"
ReferencedContainer = "container:iDNA.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.GDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.GDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6373D51B1689E47C00EE7CDA"
BuildableName = "iDNATests.octest"
BlueprintName = "iDNATests"
ReferencedContainer = "container:iDNA.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6373D4F71689E47B00EE7CDA"
BuildableName = "iDNA.app"
BlueprintName = "iDNA"
ReferencedContainer = "container:iDNA.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.GDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.GDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6373D4F71689E47B00EE7CDA"
BuildableName = "iDNA.app"
BlueprintName = "iDNA"
ReferencedContainer = "container:iDNA.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6373D4F71689E47B00EE7CDA"
BuildableName = "iDNA.app"
BlueprintName = "iDNA"
ReferencedContainer = "container:iDNA.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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>SchemeUserState</key>
<dict>
<key>iDNA.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>6373D4F71689E47B00EE7CDA</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>6373D51B1689E47C00EE7CDA</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
15 changes: 15 additions & 0 deletions iDNA/Cell+Mutate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Cell+Mutate.h
// DNA
//
// Created by D_Unknown on 11/6/12.
// Copyright (c) 2012 D_Unknown. All rights reserved.
//

#import "Cell.h"

@interface Cell (Mutate)

-(void) mutate:(int) x;

@end
36 changes: 36 additions & 0 deletions iDNA/Cell+Mutate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Cell+Mutate.m
// DNA
//
// Created by D_Unknown on 11/6/12.
// Copyright (c) 2012 D_Unknown. All rights reserved.
//

#import "Cell+Mutate.h"

@implementation Cell (Mutate)

-(void) mutate:(int)x {
NSArray *nucleotides=[NSArray arrayWithObjects: @"A", @"T", @"G", @"C", nil]; //array of nucleotides
int DNALen = [DNA count];
int NucsToMut=x*DNALen/100;
int rand_ind, rand_nuc;
NSMutableArray *mutated = [[NSMutableArray alloc] init];

for (int i=0; i<NucsToMut; i++) {
while (true) {
rand_ind = arc4random()%DNALen; //get random index in DNA for replacement
if([mutated containsObject:[NSNumber numberWithInt:rand_ind]])
continue; //check if it has been already replaced, if so get another index
do {
rand_nuc = arc4random()%4;//get random nuc
}
while([[DNA objectAtIndex:rand_ind] isEqualTo: [nucleotides objectAtIndex:rand_nuc]]); //check if nucleotide has changed. If it hasn't, repeat replacing
[DNA replaceObjectAtIndex:rand_ind withObject:[nucleotides objectAtIndex:rand_nuc]];//mutate!
[mutated addObject:[NSNumber numberWithInt:rand_ind]];
break;
}
}
}

@end
24 changes: 24 additions & 0 deletions iDNA/Cell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Cell.h
// DNA
//
// Created by D_Unknown on 11/6/12.
// Copyright (c) 2012 D_Unknown. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Cell : NSObject
{
NSMutableArray *DNA;
NSArray *nucleotidesArray;

}

-(id) initWithLength:(int)DNALength;
-(NSString*) getDNAString;
-(int) hammingDistance:(Cell*) c;
-(Cell*) crossbreedWith:(Cell*) partner;
-(NSString*) getNucleotide;

@end
101 changes: 101 additions & 0 deletions iDNA/Cell.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// Cell.m
// DNA
//
// Created by D_Unknown on 11/6/12.
// Copyright (c) 2012 D_Unknown. All rights reserved.
//

#import "Cell.h"

@implementation Cell

-(id) init {
self=[super init];
if (self) {
DNA=[[NSMutableArray alloc] init];
}
return self;
}

-(id) initWithLength:(int)DNALength {
self = [super init];

if (self) {
nucleotidesArray = [NSArray arrayWithObjects: @"A", @"T", @"G", @"C", nil]; //array of nucleotides
DNA = [[NSMutableArray alloc] initWithCapacity:DNALength];

for(int i=0; i<DNALength; i++)
[DNA insertObject:[self getNucleotide] atIndex:i]; //fill DNA array with nucleotides
}
return self;
}

-(NSString*) getDNAString {
NSMutableString *DNAString=[[NSMutableString alloc] init];
for (NSString *element in DNA){
[DNAString appendString:element];
}
return DNAString;
}

-(NSString*) getNucleotide {
return [nucleotidesArray objectAtIndex:(arc4random()%4)]; //get random nucleotide
}

-(int) hammingDistance:(Cell*) c {
int dist=0;
for (NSUInteger i=0; i<[self->DNA count]; i++)
if([[self->DNA objectAtIndex:i] isNotEqualTo: [c->DNA objectAtIndex:i]])
dist++; //++ hamming distance if nucleotides are not equal
return dist;
}

-(Cell*) crossbreedWith:(Cell*) partner {
Cell *child=[[Cell alloc] init];
NSInteger DNALength=[self->DNA count];
int rand=arc4random()%3;
switch (rand) {
case 0: {
for (int i=0; i<DNALength/2; i++) {
[child->DNA addObject:[self->DNA objectAtIndex:i]];
}
for (int i=DNALength/2; i<DNALength; i++) {
[child->DNA addObject:[partner->DNA objectAtIndex:i]];
}
break;
}

case 1: {
BOOL first=YES;
for (int i=0; i<DNALength; i++) {
if (first) {
[child->DNA addObject:[self->DNA objectAtIndex:i]];
first=NO;
}
else {
[child->DNA addObject:[partner->DNA objectAtIndex:i]];
first=YES;
}
}
break;
}
case 2: {
for (int i=0; i<DNALength*0.2; i++) {
[child->DNA addObject:[self->DNA objectAtIndex:i]];
}
for (int i=DNALength*0.2; i<DNALength*0.8; i++) {
[child->DNA addObject:[partner->DNA objectAtIndex:i]];
}
for (int i=DNALength*0.8; i<DNALength; i++) {
[child->DNA addObject:[self->DNA objectAtIndex:i]];
}
break;
}

}

return child;
}

@end
60 changes: 60 additions & 0 deletions iDNA/Document.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Document.h
// iDNA
//
// Created by D_unknown on 12/25/12.
// Copyright (c) 2012 D_unknown. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "Cell.h"
#import "Cell+Mutate.h"
@interface Document : NSDocument {
NSMutableArray *population;
Cell *goalDNA;

NSInteger populationSize;
NSInteger DNALength;
NSInteger mutationRate;

NSInteger generation;
NSInteger bestMatch;
BOOL paused;
BOOL stopped;

IBOutlet NSTextField *popSizeField;
IBOutlet NSTextField *DNALenField;
IBOutlet NSTextField *mutRateField;

IBOutlet NSSlider *popSizeSlider;
IBOutlet NSSlider *DNALenSlider;
IBOutlet NSSlider *mutRateSlider;


IBOutlet NSTextField *genLabel;
IBOutlet NSTextField *bestMatchLabel;
IBOutlet NSProgressIndicator *bestMatchProg;

IBOutlet NSTextField *goalDNAField;

IBOutlet NSButton *startBut;
IBOutlet NSButton *loadBut;
IBOutlet NSButton *stopBut;

}

- (IBAction)startEvolution:(id)sender;
- (IBAction)populationSizeChange:(id)sender;
- (IBAction)DNALengthChange:(id)sender;
- (IBAction)mutationRateChange:(id)sender;
- (IBAction)stopEvol:(id)sender;


- (void) evolution;
- (void) setPopSize;
- (void) setDNALen;
- (void) setMutRate;
- (void) setGen;
- (void) setBestMatch;
- (void) createGoal;
@end
Loading