-
Notifications
You must be signed in to change notification settings - Fork 2
/
DrawGene.m
116 lines (94 loc) · 2.82 KB
/
DrawGene.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// DrawGene.m
// Evo1
//
// Created by Tim Hinderliter on 4/25/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "DrawGene.h"
@implementation DrawGene
- (id<Gene>) mutate {
//return self;
return [DrawGene randomGene];
}
- (DrawGene*) init {
if (self = [super init]) {
self->type = NUMBER;
}
return self;
}
@synthesize number;
@synthesize type;
static double s_geneWeights[_SIZE] = {
1, .5, .5, .5, .5, // NUMBER, CLONE1 - CLONE4
.5, // POP
.7, .7, .7, .7, // LOAD_1 - LOAD_4
0, 0, 0, 0, // STORE_1 - STORE_4 (not implemented, what would it do with no RAM?)
.6, .6, .6, .6, // +, -, *, /
1.6, 1.6, 1.6, .9, // translate, rotate, RGBA (color), scale
.5, .5, .9, // CMP, jump, draw
};
static double s_geneWeightsTotal = 0;
+ (DrawGene*) randomGene {
DrawGene * result = [[DrawGene alloc] init];
#if 1 // weighted random type
// Add up all the weights from the above array, then pick a random number
// between 0 and that total weight. pick the "slot" that the random number
// falls in, making higher numbers have bigger slots and therefore be
// more likely.
// TODO: tim, 2010-6-28 - not thread safe!
if (s_geneWeightsTotal == 0) {
for (int i = 0; i < _SIZE; i ++) {
s_geneWeightsTotal += s_geneWeights[i];
}
}
double r = ((CGFloat)random() / (CGFloat)RAND_MAX) * s_geneWeightsTotal;
double totalSoFar = 0;
for (int i = 0; i < _SIZE; i ++) {
totalSoFar += s_geneWeights[i];
if (r < totalSoFar) {
result->type = i + _MIN;
break;
}
}
#else // all types same chance
result->type = random() % (_MAX + 1);
#endif
if (result->type == NUMBER || (result->type >= LOAD_R1 && result->type <= LOAD_R4)) {
result->number = (CGFloat)random() / (CGFloat)RAND_MAX;
}
return result;
}
- (NSString*) description {
NSString * result = nil;
if (type >= _MIN && type <= _MAX) {
static NSString * descriptions[] = {
@"number", @"clone1", @"clone2", @"clone3", @"clone4",
@"pop",
@"load_r1", @"load_r2", @"load_r3", @"load_r4",
@"store_r1", @"store_r2", @"store_r3", @"store_r4",
@"add", @"subtract", @"multi", @"div",
@"translate", @"rotate", @"rgba", @"scale",
@"cmp", @"jmpf", @"draw",
};
result = descriptions[type];
}
return result;
}
- (NSString*) short_description {
NSString * result = nil;
if (type >= _MIN && type <= _MAX) {
static NSString * short_descriptions[] = {
@"##", @"C1", @"C2", @"C3", @"C4",
@" <",
@"L1", @"L2", @"L3", @"L4",
@"S1", @"S2", @"S3", @"S4",
@"+", @"-", @"*", @"/",
@"T", @"R", @"C", @"S",
@"=", @"j", @"D"
};
result = short_descriptions[type];
}
return result;
}
@end