Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed leaks in MRC genetic algorithm

  • Loading branch information...
commit 97b2311dec1f3338c4913a6ba1508d160cae1f78 1 parent c22259f
@LearnCocos2D LearnCocos2D authored
View
39 Cocos2D-ARC-Performance-Test/Cocos2D-Performance-Test/JASChromosome.m
@@ -52,8 +52,8 @@ - (id)initWithGeneCount:(NSUInteger)count
self = [super init];
if (self)
{
- self.fitnessBuffer = [NSMutableArray arrayWithCapacity:count];
- self.geneBuffer = [NSMutableString stringWithCapacity:count];
+ self.fitnessBuffer = [[NSMutableArray arrayWithCapacity:count] retain];
+ self.geneBuffer = [[NSMutableString stringWithCapacity:count] retain];
for (int geneIndex = 0; geneIndex < count; ++geneIndex)
{
// Append a random character between ' ' and 'Z'.
@@ -66,8 +66,22 @@ - (id)initWithGeneCount:(NSUInteger)count
return self;
}
+-(void) dealloc
+{
+ //NSLog(@"dealloc %@", self);
+ [self.fitnessBuffer release];
+ self.fitnessBuffer = nil;
+ [self.geneBuffer release];
+ self.geneBuffer = nil;
+ self.cachedOverallFitness = nil;
+ [super dealloc];
+}
+
- (JASChromosome *)mateWithChromosome:(JASChromosome *)other
{
+ // SIMULATE ARC BEHAVIOR
+ [other retain];
+
// Create an empty chromosome.
JASChromosome *child = [[JASChromosome alloc] initWithGeneCount:0];
@@ -84,7 +98,11 @@ - (JASChromosome *)mateWithChromosome:(JASChromosome *)other
{
// Get the same gene from both chromosomes.
mine = [self.fitnessBuffer objectAtIndex:i];
- theirs = [other.fitnessBuffer objectAtIndex:i];
+ theirs = [other.fitnessBuffer objectAtIndex:i];
+
+ // SIMULATE ARC BEHAVIOR
+ [mine retain];
+ [theirs retain];
// Determine which chromosome's gene is fitter.
winner = [mine integerValue] > [theirs integerValue]
@@ -93,8 +111,13 @@ - (JASChromosome *)mateWithChromosome:(JASChromosome *)other
// Add the winner's gene to the child chromosome.
geneValue = [winner.geneBuffer characterAtIndex:i];
+
gene = [NSString stringWithFormat:@"%c", geneValue];
[child.geneBuffer appendString:gene];
+
+ // SIMULATE ARC BEHAVIOR
+ [mine release];
+ [theirs release];
}
// Sometimes randomly modify the child's gene sequence.
@@ -103,14 +126,24 @@ - (JASChromosome *)mateWithChromosome:(JASChromosome *)other
[child mutate];
}
+ // SIMULATE ARC BEHAVIOR
+ [other release];
+
return [child autorelease];
}
- (BOOL)isFitterThanChromosome:(JASChromosome *)other
forTargetSequence:(NSString *)seq
{
+ // SIMULATE ARC BEHAVIOR
+ [other retain];
+
NSInteger mine = [self fitnessForTargetSequence:seq];
NSInteger theirs = [other fitnessForTargetSequence:seq];
+
+ // SIMULATE ARC BEHAVIOR
+ [other release];
+
return mine > theirs;
}
View
12 Cocos2D-ARC-Performance-Test/Cocos2D-Performance-Test/JASChromosomeARC.m
@@ -14,8 +14,11 @@
#define MUTATION_DELTA_MAX (6) // Mutate one gene by no more than +/-5.
#define MUTATION_RATE (0.20f) // Mutate about 20% of the time
#define MUTATION_THRESHOLD (ARC4RANDOM_MAX * MUTATION_RATE) // This is used to determine if a mutation should occur.
-#define RANDOM() (arc4random()) // Creates a random positive whole number
-#define RANDOM_MOD(__MOD) (arc4random_uniform(__MOD)) // Creates a random positive whole number no greater than __MOD-1
+//#define RANDOM() (arc4random()) // Creates a random positive whole number
+//#define RANDOM_MOD(__MOD) (arc4random_uniform(__MOD)) // Creates a random positive whole number no greater than __MOD-1
+
+#define RANDOM() (1234567) // Creates a random positive whole number
+#define RANDOM_MOD(__MOD) (__MOD - 1) // Creates a random positive whole number no greater than __MOD-1
@interface JASChromosomeARC ()
@property (nonatomic, strong) NSNumber *cachedOverallFitness;
@@ -70,6 +73,11 @@ - (id)initWithGeneCount:(NSUInteger)count
return self;
}
+-(void) dealloc
+{
+ //NSLog(@"dealloc %@", self);
+}
+
- (JASChromosomeARC *)mateWithChromosome:(JASChromosomeARC *)other
{
// Create an empty chromosome.
View
38 Cocos2D-ARC-Performance-Test/Cocos2D-Performance-Test/JASGeneticAlgo.m
@@ -45,11 +45,21 @@ - (id)initWithTargetSequence:(NSString *)sequence
if (self)
{
self.targetSequence = sequence;
- self.population = [NSMutableArray arrayWithCapacity:POPULATION_SIZE];
+ self.population = [[NSMutableArray arrayWithCapacity:POPULATION_SIZE] retain];
}
return self;
}
+-(void) dealloc
+{
+ //NSLog(@"dealloc %@", self);
+ [self.population release];
+ self.population = nil;
+ [self.targetSequence release];
+ self.targetSequence = nil;
+ [super dealloc];
+}
+
- (void)execute
{
[self populate];
@@ -64,10 +74,10 @@ - (void)populate
JASChromosome *chromo;
for (int i = 0; i < POPULATION_SIZE; ++i)
{
- chromo = [[JASChromosome alloc] initWithGeneCount:geneCount];
+ chromo = [[[JASChromosome alloc] initWithGeneCount:geneCount] autorelease];
[self.population addObject:chromo];
- [chromo release];
}
+ chromo = nil;
}
- (void)run
@@ -101,12 +111,23 @@ - (void)breedNextGeneration
index2 = i + 1;
chromo1 = [self.population objectAtIndex:index1];
chromo2 = [self.population objectAtIndex:index2];
- keepFirst = [chromo1 isFitterThanChromosome:chromo2
+ // SIMULATE ARC BEHAVIOR
+ [chromo1 retain];
+ [chromo2 retain];
+
+ keepFirst = [chromo1 isFitterThanChromosome:chromo2
forTargetSequence:seq];
deadIndex = keepFirst ? index2 : index1;
child = [chromo1 mateWithChromosome:chromo2];
+ // SIMULATE ARC BEHAVIOR
+ [child retain];
[self.population replaceObjectAtIndex:deadIndex
withObject:child];
+
+ // SIMULATE ARC BEHAVIOR
+ [chromo1 release];
+ [chromo2 release];
+ [child release];
}
}
@@ -134,7 +155,13 @@ - (void)analyzePopulation
[contender isFitterThanChromosome:champion
forTargetSequence:seq])
{
+ // SIMULATE ARC BEHAVIOR
+ [champion release];
+
champion = contender;
+
+ // SIMULATE ARC BEHAVIOR
+ [champion retain];
}
}
NSString *fittest = champion.geneSequence;
@@ -148,6 +175,9 @@ - (void)analyzePopulation
{
//NSLog(@"Fittest sequence for generation #%ld: %@", (long)self.generations, fittest);
}
+
+ // SIMULATE ARC BEHAVIOR
+ [champion release];
}
@end
View
5 Cocos2D-ARC-Performance-Test/Cocos2D-Performance-Test/JASGeneticAlgoARC.m
@@ -51,6 +51,11 @@ - (id)initWithTargetSequence:(NSString *)sequence
return self;
}
+-(void) dealloc
+{
+ //NSLog(@"dealloc %@", self);
+}
+
- (void)execute
{
[self populate];
View
3  Cocos2D-ARC-Performance-Test/Cocos2D-Performance-Test/MRCTests.m
@@ -81,9 +81,12 @@ -(BOOL) data:(NSData*)data containsCString:(char *)cmp
-(void) runGeneticAlgorithm
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
+
NSString* sequence = @"lks89Ü?c§cngW3d432d7cf143497fb5a95b280f873b16iysöl5cöä#ke2ß0B% ()=BÄ´Qyaix4@\nElapsou5ö8o7 5V4*j6ß71655b7c7b6cb41128dffc046773acfbc8w3y,ic;9uwo9ly8WV)+:.573j 4jpew Q'C)96ß#8m)$VNwv49";
JASGeneticAlgo *algo = [[JASGeneticAlgo alloc] initWithTargetSequence:sequence];
[algo execute];
+ [algo release];
+
[pool release];
}
View
8 Cocos2D-ARC-Performance-Test/Cocos2D-Performance-Test/PerfTester_ARCvsMRC.m
@@ -18,14 +18,14 @@ -(void) testGeneticAlgorithmARC
{
BEGIN ( 50 )
[arcTest runGeneticAlgorithm];
- NSLog(@"%i out of %i complete", i, iters);
+ //NSLog(@"%i out of %i complete", i, iters);
END()
}
-(void) testGeneticAlgorithmMRC
{
BEGIN ( 50 )
[mrcTest runGeneticAlgorithm];
- NSLog(@"%i out of %i complete", i, iters);
+ //NSLog(@"%i out of %i complete", i, iters);
END()
}
@@ -34,7 +34,7 @@ -(void) testUnoptimizedContainsStringAlgorithmARC
const char* string = "ein string, zwei string, drei string, vier string, fünf string, sechs string, sieben string, acht string\0";
NSData* data = [NSData dataWithBytes:string length:strlen(string)];
- BEGIN ( k10KIterationTestCount )
+ BEGIN ( k1KIterationTestCount * 5 )
[arcTest data:data containsCString:"sieben string"];
END()
}
@@ -44,7 +44,7 @@ -(void) testUnoptimizedContainsStringAlgorithmMRC
const char* string = "ein string, zwei string, drei string, vier string, fünf string, sechs string, sieben string, acht string\0";
NSData* data = [NSData dataWithBytes:string length:strlen(string)];
- BEGIN ( k10KIterationTestCount )
+ BEGIN ( k1KIterationTestCount * 5 )
[mrcTest data:data containsCString:"sieben string"];
END()
}
Please sign in to comment.
Something went wrong with that request. Please try again.