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 all 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
472 changes: 472 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>
27 changes: 27 additions & 0 deletions iDNA/Cell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// 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;
-(void) mutate:(int) percent;
-(const NSString*) getNucleotide;
-(const NSString*) getNewNucleotide:(NSString*) oldNuc;
-(void) DNAFromString:(NSString*)source withMaxLen:(int) maxLen;

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

#import "Cell.h"

@implementation Cell

const NSString * nucleotides[4] = {@"A", @"T", @"G", @"C"}; //array of nucleotides

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

//initialization whith DNA of given length
-(id) initWithLength:(int)DNALength {
self = [super init];
if (self) {
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;
}

//get DNA string to show in textfield
-(NSString*) getDNAString {
NSMutableString *DNAString=[[NSMutableString alloc] init];
for (NSString *element in DNA){
[DNAString appendString:element];
}
return DNAString;
}

//get random nucleotide from nucs array
-(const NSString*) getNucleotide {
return nucleotides[arc4random()%4]; //return random nucleotide
}

//count hamming distance to another cell
-(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;
}

//crossbreed with anoter cell
-(Cell*) crossbreedWith:(Cell*) partner {
Cell *child=[[Cell alloc] init]; //create a child cell
NSInteger DNALength=[self->DNA count];
int rand=arc4random()%3;
switch (rand) { //choose 1 of 3 crossbreeed ways
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;
}

//mutate cell
-(void) mutate:(int) percent {
int dnaLength = [DNA count];
if (percent == 0) // if percent = 0, do nothing
return;
if (dnaLength != 100) // if DNA length isn't 100, recount
percent = percent * dnaLength / 100;

// create an array of BOOL, where there are as many YES elems, as number of nucs we need to mutate
BOOL mas [dnaLength];
int count = dnaLength;
while (count--)
mas [count] = count < percent? YES : NO;

// shuffle BOOL array
count = dnaLength;
int tempIndex; // buffer to save index of changed elem
BOOL tempValue; // buffer to save value of changed elem
while (count--) {
tempIndex = arc4random() % dnaLength; // get random index
tempValue = mas [tempIndex]; // get value of BOOL at random index
mas [tempIndex] = mas [count]; // exchange elem at index "count"
mas [count] = tempValue; // whith elem at random index
}

// mutation itself
count = dnaLength;
while (count--)
if (mas[count]) // if mas[count] = YES, mutate nuc
[DNA replaceObjectAtIndex:count withObject:[self getNewNucleotide:[DNA objectAtIndex:count]]];
}

//get new random nuc
-(const NSString*) getNewNucleotide:(NSString *)oldNuc {
int count;
for (count = 0; count < 4; count++)
if (nucleotides[count] == oldNuc)
break; // get nuc index from 0 to 3 in nucs array, save it in count
count = (count + 1 + arc4random() % 3) % 4; // change index to random index not equal to it
return nucleotides[count]; // return elem of nucs array at this index
}

//create DNA from string
-(void) DNAFromString:(NSString*)source withMaxLen:(int) maxLen {
[self->DNA removeAllObjects];
NSString *nuc;
for (int i=0; i<[source length]||i<maxLen; i++) {
nuc = [NSString stringWithFormat:@"%c", [source characterAtIndex:i]]; //get char from string
[self->DNA addObject:nuc]; //add char to DNA string
}
}

@end
62 changes: 62 additions & 0 deletions iDNA/Document.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// 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"

@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 NSTextField *bestDNAField;

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;
- (IBAction)loadGoal:(id)sender;


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