-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgpdelib.h
1967 lines (1351 loc) · 56.4 KB
/
cgpdelib.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Author: Johnathan M Melo Neto ([email protected])
Related paper: "Hybridization of Cartesian Genetic Programming and Differential Evolution
for Generating Classifiers based on Neural Networks"
This file is an adapted version of CGP-Library
Copyright (c) Andrew James Turner 2014, 2015 ([email protected])
The original CGP-Library is available in <http://www.cgplibrary.co.uk>
*/
/*
Title: API
Description of all the CGP-Library functions and structures.
*/
#ifndef CGPLIB
#define CGPLIB
/*
Under windows NO_DLL must be #defined at compile time when compiling
the cgp_library with other source files.
Under windows BUILD_DLL must be #defined at compile time when compiling
CGP-Library.dll to allow DLL_EXPORT to define functions as library
functions.
Under windows when using the compiled library no #defines are required.
Under Linux no #defines are required ;)
*/
#if defined(_WIN32) && defined(NO_DLL)
#define DLL_EXPORT
#elif defined(_WIN32) && defined(BUILD_DLL)
#define DLL_EXPORT __declspec(dllexport)
#elif defined(_WIN32) && !defined(BUILD_DLL)
#define DLL_EXPORT __declspec(dllimport)
#else
#define DLL_EXPORT
#endif
/*
Function Aliases
handles depreciated functions
should be removed in the next major revision
*/
#define setFitnessFunction setCustomFitnessFunction
#define setSelectionScheme setCustomSelectionScheme
#define setReproductionScheme setCustomReproductionScheme
#define addNodeFunctionCustom addCustomNodeFunction
/*
Deal with c++ compilers
*/
#ifdef __cplusplus
extern "C" {
#endif
/*
Title: Structures
Description of all the structures used by CGP-Library.
*/
/*
variable: parameters
Stores general evolutionary and chromosome <parameters> used by the CGP-Library.
The <parameters> structure is used extensively by the CGP-Library
and controls every aspect of the evolutionary algorithm and solution structure.
The values stored in <parameters> are set to default values when
initialised using <initialiseParameters>. These default values can then be
altered using provided setter functions.
Defaults:
Default parameter values.
> mu: 1
> lambda: 4
> evolutionary strategy: +
> mutation rate: 0.05
> Recurrent Connection Probability 0.00
> Shortcut Connections 1
> connection weight range: 1
> update frequency: 1
> mutation type: probabilistic
> fitness function: supervisedLearning
> selection scheme: selectFittest
> reproduction scheme: mutateRandomParent
> number of threads 1
- Mu, lambda and evolutionary strategy control the type and
parameter values of the evolutionary strategy used. See
<setMu>, <setLambda> and <setEvolutionaryStrategy>.
- The mutation rate controls the level of mutation when
creating a child solution from a parent. See <setMutationRate>.
- The recurrent connection probability controls the probability
of connections being made recurrent when mutating connection
genes. For regular acyclic feed-forward programs leave as zero.
For recurrent programs see <setRecurrentConnectionProbability>.
- The shortcut connections controls whether program outputs can
connect directly to program inputs. 1 (yes) and 0 (no).
See <setShortcutConnections>
- The connection weight range controls the range of values
which the connection weights can take. Connection weights
are only considered when the CGP-Library is used to evolve
artificial neural networks. See <setConnectionWeightRange>.
- The update frequency controls the frequency of updates to
the terminal when using <runCGP>. The value is the number
of generations between updates. See <setUpdateFrequency>.
- The mutation type stores the mutation method used when
mutating chromosomes. See <setMutationType>.
- The fitness function stores the fitness function used when
assigning a fitness to the chromosomes. See <setCustomFitnessFunction>.
- The selection scheme stores the selection scheme used when
selecting the parents from the candidate chromosomes. See
<setCustomSelectionScheme>.
- The reproduction scheme stores how children
chromosomes are created from their parents. See
<setCustomReproductionScheme>.
- The number of threads defines how many threads the CGP library
will use internally. See <setNumThreads>.
See Also:
<initialiseParameters>, <freeParameters>, <printParameters>
*/
struct parameters;
/*
variable: chromosome
Stores a CGP chromosome instances used by the CGP-Library.
See Also:
<initialiseChromosome>, <initialiseChromosomeFromFile> <freeChromosome>, <printChromosome>, <executeChromosome>, <mutateChromosome>
*/
struct chromosome;
/*
variable: dataSet
Stores a data set which can be used by fitness functions when calculating a chromosomes fitness.
Typically contains input output pairs of data used when applying CGP to supervised learning tasks.
See Also:
<initialiseDataSetFromFile>, <initialiseDataSetFromArrays>, <freeDataSet>, <printDataSet>
*/
struct dataSet;
/*
variable: results
Stores the best chromosome found on each run when using <repeatCGP>
See Also:
<repeatCGP>, <freeResults>, <getChromosome>, <getAverageFitness>, <getAverageActiveNodes>, <getAverageGenerations>
*/
struct results;
struct DEChromosome;
/*
Title: Parameters Functions
Description of all the functions related to CGP-Library parameters
*/
/*
Function: initialiseParameters
Initialises a <parameters> structure used throughout the CGP-Library. The arguments describe the structure of the chromosomes created when using <initialiseChromosome>, <runCGP> or <repeatCGP>.
Parameters:
numInputs - the number of chromosome inputs required.
numNodes - the number of chromosome nodes required.
numOutputs - the number of chromosome outputs required.
arity - the arity of each chromosome node required.
Returns:
A pointer to an initialised <parameters> structure.
Example:
Initialising parameters
(begin code)
struct parameters *params;
int numInputs = 3;
int numNodes = 10;
int numOutputs = 2;
int nodeArity = 2;
params = initialiseParameters(numInputs, numNodes, numOutputs, nodeArity);
(end)
See Also:
<freeParameters>, <printParameters>
*/
DLL_EXPORT struct parameters *initialiseParameters(const int numInputs, const int numNodes, const int numOutputs, const int arity);
/*
Function: freeParameters
Frees <parameters> structure instance.
Parameters:
params - pointer to initialised <parameters> structure.
See Also:
<initialiseParameters>
*/
DLL_EXPORT void freeParameters(struct parameters *params);
/*
Function: printParameters
Prints the given <parameters> to the screen in a human readable format.
Parameters:
params - pointer to <parameters> structure.
Example:
Typical <parameters> structure printed using <printParameters>.
(begin code)
---------------------------------------------------
Parameters
---------------------------------------------------
Evolutionary Strategy: (1+4)-ES
Inputs: 1
Nodes: 15
Outputs: 1
Node Arity: 2
Connection weights range: +/- 1.000000
Mutation Type: probabilistic
Mutation rate: 0.050000
Fitness Function: supervisedLearning
Target Fitness: 0.100000
Selection scheme: selectFittest
Reproduction scheme: mutateRandomParent
Update frequency: 500
Function Set: add sub mul div sin (5)
---------------------------------------------------
(end)
*/
DLL_EXPORT void printParameters(struct parameters *params);
/*
Function: addNodeFunction
Adds pre-defined node function(s) to the set of functions stored by a <parameters> structure. These are the node functions available when using <runCGP>, <repeatCGP> and <mutateChromosome>.
If one function name is given that function is added to the function set. If multiple node function names are given then each must be separated by a ','.
If a node function name is given which is not recognised, a warning is given and that function is not added to the function set.
Parameters:
params - pointer to <parameters> structure
functionNames - the name(s) of the function(s) to be added to the function set
Node Functions:
mathematical operations
- add - summation over all inputs.
- sub - subtracts all but the first input from the first input
- mul - multiplies all of the inputs
- div - divides the first input by the second and then the third etc
- abs - the absolute of the first input
- sqrt - the square root of the first input
- sq - the square of the first input
- cube - the cube of the first input
- pow - the first input raised to the power of the second input
- exp - the exponential of the first input
- sin - the sine of the first input
- cos - the cosine of the first input
- tan - the tangent of the first input
logic gates
- and - returns '1' if all inputs are '1', else '0'
- nand - returns '0' if all inputs are '1', else, '1'
- or - returns '0' if all inputs are '0', else, '1'
- nor - returns '1' if all inputs are '0', else, '0'
- xor - returns '1' if only one of the inputs is '1', else, '0'
- xnor - returns '0' if only one of the inputs is '1', else, '1'
- not - returns '1' if first input is '0', else '1'
neuron transfer/activation functions
- sig - the logistic sigmoid of the weighted sum of inputs. Output range [0,1]
- gauss - the Gaussian of the weighted sum of inputs. Output range [0,1]
- step - the heaviside step function of the weighted sum of inputs. Output range [0,1]
- softsign - the softsign of the weighted sum of inputs. Output range [-1,1]
- tanh - the hyperbolic tangent of the weighted sum of inputs. Output range [-1,1]
Other
- rand - produces a different random number in the range [-1,1] each time it is called
- pi - produces the constant pi
- 1 - produces the constant one
- 0 - produces the constant zero
- wire - acts as a simple wire mapping the input to the output
Example:
Add the node functions logical AND OR NAND NOR and XOR to the function set.
(begin code)
addNodeFunction(params, "and,or,nand,nor,xor");
(end)
See Also:
<clearFunctionSet>, <addCustomNodeFunction>
*/
DLL_EXPORT void addNodeFunction(struct parameters *params, char const *functionNames);
/*
Function: addCustomNodeFunction
Adds custom node function to the set of functions stored by a <parameters> structure. See <addNodeFunction>.
The custom fitness function prototype must take the form
(begin code)
double nodeFunctionName(const int numInputs, const double *inputs, const double *weights);
(end)
where the user replaces 'nodeFunctionName' with their own function name.
Parameters:
params - pointer to an initialised <parameters> structure
function - the custom node function
functionName - the name of the added function
maxNumInputs - maximum number of inputs to function (-1 is unlimited)
Example:
Creating a custom node function 'add'
(begin code)
double add(const int numInputs, const double *inputs, const double *connectionWeights){
int i;
double sum = 0;
for(i=0; i<numInputs; i++){
sum += inputs[i];
}
return sum;
}
(end)
Adding the new custom node function to the function set
(begin code)
addCustomNodeFunction(params, add, "add", -1);
(end)
Note:
The connections weights are used for when creating custom node functions for Artificial Neural Networks. If required they are accessed in the same way as the inputs.
See Also:
<clearFunctionSet>, <addNodeFunction>
*/
DLL_EXPORT void addCustomNodeFunction(struct parameters *params, double (*function)(const int numInputs, const double *inputs, const double *weights), char const *functionName, int maxNumInputs);
/*
Function: clearFunctionSet
Resets the function set stored by a <parameters> structure to contain no functions.
Parameters:
params - pointer to an initialised <parameters> structure
See Also:
<addNodeFunction>, <addCustomNodeFunction>
*/
DLL_EXPORT void clearFunctionSet(struct parameters *params);
/*
Function: setNumInputs
Sets the number of chromosome inputs in the given <parameters>.
The given number of chromosome inputs is also parsed to ensure a valid number of chromosome inputs.
A number of chromosome inputs <0 is invalid. If an invalid number of chromosome inputs is give an error is displayed
and CGP-Library terminates.
Parameters:
params - pointer to <parameters> structure.
numInputs - The number of chromosome inputs to be set.
See Also:
<setNumNodes>, <setNumOutputs>, <setArity>
*/
DLL_EXPORT void setNumInputs(struct parameters *params, int numInputs);
/*
Function: setNumNodes
Sets the number of chromosome nodes in the given <parameters>.
The given number of chromosome nodes is also parsed to ensure a valid number of chromosome nodes.
A number of chromosome nodes <0 is invalid. If an invalid number of chromosome nodes is give an error is displayed
and CGP-Library terminates.
Parameters:
params - pointer to <parameters> structure.
nodes - The number of chromosome nodes to be set.
See Also:
<setNumInputs>, <setNumOutputs>, <setArity>
*/
DLL_EXPORT void setNumNodes(struct parameters *params, int numNodes);
/*
Function: setNumOutputs
Sets the number of chromosome outputs in the given <parameters>.
The given number of chromosome outputs is also parsed to ensure a valid number of chromosome outputs.
A number of chromosome outputs <1 is invalid. If an invalid number of chromosome outputs is give an error is displayed
and CGP-Library terminates.
Parameters:
params - pointer to <parameters> structure.
numOutputs - The number of chromosome outputs to be set.
See Also:
<setNumInputs>, <setNumNodes>, <setArity>
*/
DLL_EXPORT void setNumOutputs(struct parameters *params, int numOutputs);
/*
Function: setArity
Sets the arity of the chromosome nodes in the given <parameters>.
The given arity for each chromosome node is also parsed to ensure a valid chromosome node arity.
A chromosome node arity <1 is invalid. If an invalid chromosome node arity is give an error is displayed
and CGP-Library terminates.
Parameters:
params - pointer to <parameters> structure.
arity - The chromosome node arity to be set.
See Also:
<setNumInputs>, <setNumNodes>, <setNumOutputs>
*/
DLL_EXPORT void setArity(struct parameters *params, int arity);
/*
Function: setMu
Sets the mu value in the given <parameters>.
The given mu value is also parsed to ensure a valid mu value.
mu values <1 are invalid. If an invalid mu value is give a
warning is displayed and the mu value is left unchanged.
Parameters:
params - pointer to <parameters> structure.
mu - The value of mu to be set.
*/
DLL_EXPORT void setMu(struct parameters *params, int mu);
/*
Function: setLambda
Sets the lambda value in the given <parameters>.
The given lambda value is also parsed to ensure a valid lambda value.
lambda values <1 are invalid. If an invalid lambda value is give a
warning is displayed and the lambda value is left unchanged.
Parameters:
params - pointer to <parameters> structure.
lambda - The value of lambda to be set.
*/
DLL_EXPORT void setLambda(struct parameters *params, int lambda);
/*
Function: setEvolutionaryStrategy
Sets the evolutionary strategy in the given <parameters>.
The given evolutionary strategy is also parsed to ensure a valid evolutionary strategy.
Evolutionary strategies other than '+' and ',' are invalid. If an invalid evolutionary strategy is give a
warning is displayed and the evolutionary strategy is left unchanged.
Parameters:
params - pointer to <parameters> structure.
evolutionaryStrategy - The evolutionary strategy to be set.
*/
DLL_EXPORT void setEvolutionaryStrategy(struct parameters *params, char evolutionaryStrategy);
/*
Function: setMutationRate
Sets the mutation rate in the given <parameters>.
The given mutation rate is also parsed to ensure a valid mutation rate.
Mutation rate <0 or >1 are invalid. If an invalid mutation rate is give a
warning is displayed and the mutation rate is left unchanged.
Parameters:
params - pointer to <parameters> structure.
mutationRate - The value of the mutation rate to be set.
*/
DLL_EXPORT void setMutationRate(struct parameters *params, double mutationRate);
/*
Function: setRecurrentConnectionProbability
Sets the recurrent connection probability in the given <parameters>.
The recurrent connection probability specifies the probability that a mutation to a connection gene will create a recurrent connection; otherwise a standard feed forward connection is made. The given recurrent connection probability is also parsed to ensure a valid recurrent connection probability.
Recurrent connection probability <0 or >1 are invalid. If an invalid recurrent connection probability is give a
warning is displayed and the recurrent connection probability is left unchanged.
Parameters:
params - pointer to <parameters> structure.
recurrentConnectionProbability - The value of the recurrent connection probability to be set.
*/
DLL_EXPORT void setRecurrentConnectionProbability(struct parameters *params, double recurrentConnectionProbability);
/*
Function: setShortcutConnections
Sets whether shortcut connections are used in the given <parameters>.
Shortcut Connections specifies whether an output can connect directly to an input.
Only Shortcut Connections of values 0 (no) and 1 (yes) are valid. If an invalid value is given, a warning is displayed and the shortcut connections value is left unchanged.
Parameters:
params - pointer to <parameters> structure.
shortcutConnections - whether shortcut connections are used
*/
DLL_EXPORT void setShortcutConnections(struct parameters *params, int shortcutConnections);
/*
Function: setConnectionWeightRange
Sets the connection weight range in the given <parameters>. (only used by NeuroEvolution)
Parameters:
params - pointer to <parameters> structure.
weightRange - The connection weight range to be set. (the range is +/- weightRange)
*/
DLL_EXPORT void setConnectionWeightRange(struct parameters *params, double weightRange);
/*
Function: setCustomFitnessFunction
Set custom fitness function.
By default the CGP-Library used a generic supervised learning fitness function where the fitness assigned to each chromosome is the sum of the absolute differences between the actual and target outputs defined in the given <dataSet>. <setCustomFitnessFunction> is used to redefine the fitness function to be one of the users design.
All custom fitness function prototype must take the following form. Where params is a <parameters> structure, chromo is the <chromosome> to be assigned a fitness and data is a <dataSet> which may be used by the custom fitness function.
(begin code)
double functionName(struct parameters *params, struct chromosome *chromo, struct dataSet *data);
(end)
Parameters:
params - pointer to <parameters> structure.
fitnessFunction - the custom fitness function
fitnessFunctionName - name of custom fitness function
If the fitnessFunction parameter is set as NULL, the fitness function will be reset to the default supervised learning fitness function.
Example:
Defining a custom fitness function, full adder. Note that the <dataSet> does not have to be used.
(begin code)
double fullAdder(struct parameters *params, struct chromosome *chromo, struct data *dat){
int i;
double error = 0;
// full adder truth table
double inputs[8][3] = {{0,0,0},{0,0,1},{0,1,0},{0,1,1},{1,0,0},{1,0,1},{1,1,0},{1,1,1}};
double outputs[8][2] = {{0,0},{1,0},{1,0},{0,1},{1,0},{0,1},{0,1},{1,1}};
//for each line in the truth table
for(i=0; i<8; i++){
// calculate the chromosome outputs for each set of inputs
executeChromosome(chromo, inputs[i]);
// If the first chromosome outputs differ from the correct outputs increment the error
if(outputs[i][0] != getChromosomeOutput(chromo, 0) ){
error++;
}
// If the second chromosome outputs differ from the correct outputs increment the error
if(outputs[i][1] != getChromosomeOutput(chromo, 1) ){
error++;
}
}
return error;
}
(end)
Setting the new custom fitness function as the fitness function to be used
(begin code)
setCustomFitnessFunction(params, fullAdder, "fullAdder");
(end)
*/
DLL_EXPORT void setCustomFitnessFunction(struct parameters *params, double (*fitnessFunction)(struct parameters *params, struct chromosome *chromo, struct dataSet *data), char const *fitnessFunctionName);
/*
Function: setCustomSelectionScheme
Sets custom selection scheme.
By default the selection scheme used by CGP-Library is select fittest, where the fittest members of the candidate chromosomes are always selected as the parents. This type of selection scheme is commonly used by CGP. <setCustomSelectionScheme> is used to redefine the selection scheme to be one of the users design.
The custom selection scheme prototype must take the following form. Where params is a <parameters> structure, parents is an array of <chromosomes> used to store the selected parents, candidateChromos is an array of <chromosomes> containing the pool of <chromosomes> to select the parent from, numParents is the number of parents to be selected and numCandidateChromos is the number of candidate <chromosomes>.
(begin code)
void selectionSchemeNmes(struct parameters *params, struct chromosome **parents, struct chromosome **candidateChromos, int numParents, int numCandidateChromos);
(end)
Note:
The ordering of the candidateChromos is children followed by parents.
Parameters:
params - pointer to <parameters> structure
selectionScheme - the custom selection scheme
selectionSchemeName - name of custom selection scheme
If the selectionScheme parameter is set as NULL, the selection scheme will be reset to the default select fittest selection scheme.
Example:
Defining a custom selection scheme, tournament selection.
(begin code)
void tournament(struct parameters *params, struct chromosome **parents, struct chromosome **candidateChromos, int numParents, int numCandidateChromos){
int i;
// two chromosome pointers to point to the chromosomes in the torment
struct chromosome *candidateA;
struct chromosome *candidateB;
// for the number of required parents
for(i=0; i<numParents; i++){
// randomly select chromosomes for tournament
candidateA = candidateChromos[rand() % numCandidateChromos];
candidateB = candidateChromos[rand() % numCandidateChromos];
// choose the fittest chromosome to become a parent
if(getChromosomeFitness(candidateA) <= getChromosomeFitness(candidateB)){
copyChromosome(parents[i], candidateA);
}
else{
copyChromosome(parents[i], candidateB);
}
}
}
(end)
Setting the new custom selection scheme as the selection scheme to be used
(begin code)
setCustomSelectionScheme(params, tournament, "tournament");
(end)
See Also:
<setCustomFitnessFunction>
*/
DLL_EXPORT void setCustomSelectionScheme(struct parameters *params, void (*selectionScheme)(struct parameters *params, struct chromosome **parents, struct chromosome **candidateChromos, int numParents, int numCandidateChromos), char const *selectionSchemeName);
/*
Function: setCustomReproductionScheme
Sets custom reproduction scheme.
By default the reproduction scheme used by CGP-Library is to create
each child as a mutated version of a randomly selected parent. This
type of reproduction scheme is commonly used by CGP.
<setCustomReproductionScheme> is used to redefine the reproduction scheme
to be one of the users design.
The custom reproduction scheme prototype must take the following
form. Where params is a <parameters> structure, parents is an array
of <chromosomes> which store the parents to select from, children is
an array of <chromosomes> which will contain the children
after reproduction, numParents is the number of parents
available for reproduction and numChildren is the number of
children to be created.
(begin code)
void reproductionScheme(struct parameters *params, struct chromosome **parents, struct chromosome **children, int numParents, int numChildren);
(end)
If the reproductionScheme parameter is set as NULL, the reproduction
scheme will be reset to the default mutate random parent
reproduction scheme.
Example:
Defining a custom reproduction scheme, ...
(begin code)
(end)
Setting the new custom reproduction scheme as the reproduction scheme to be used
(begin code)
setCustomReproductionScheme(params, ..., "...");
(end)
See Also:
<setCustomFitnessFunction>, <setCustomSelectionScheme>
*/
DLL_EXPORT void setCustomReproductionScheme(struct parameters *params, void
(*reproductionScheme)(struct parameters *params, struct chromosome **parents, struct chromosome **children, int numParents, int numChildren, int type, unsigned int * seed), char const *reproductionSchemeName);
/*
Function: setTargetFitness
Sets the target fitness used when running CGP.
In all cases lower fitness values are used to represent fitter chromosomes.
Parameters:
params - pointer to <parameters> structure.
targetFitness - The target fitness to be set.
*/
DLL_EXPORT void setTargetFitness(struct parameters *params, double targetFitness);
/*
Function: setMutationType
Sets the mutation method used when mutating chromosomes.
Used to set the mutation method used when running <runCGP> and <repeatCGP> or when mutating an individual chromosome using <mutateChromosome>. The type of mutation used is set the <parameters> structure.
If an invalid mutation type is given a warning is displayed and the mutation type is left unchanged.
Mutation Methods:
These are the mutation methods which can be selected from.
- "probabilistic". The *default* mutation method. Mutates each chromosome gene with a given probability; set with <setMutationRate>.
- "point". Always mutates the same number of randomly selected genes. The number of mutated genes is the total number of genes times the mutation rate. Note: does not mutate weight genes, see pointANN.
- "pointANN". Point mutation when evolving artificial neural networks; includes mutations to weight genes.
- "onlyActive". Conducts probabilistic mutation on active nodes only. Genes belonging to inactive nodes are not mutated.
- "single". Keeps mutating randomly selected genes until an active gene is mutated to a new allele. Note: this is independent of the mutation rate set. Note: this does not mutate weight genes.
Parameters:
params - pointer to <parameters> structure.
mutationType - char array specifying the mutation type.
Example:
(begin code)
struct parameters *params = NULL;
params = initialiseParameters(1,10,1,2);
setMutationType(params, "point");
setMutationType()
(end)
See Also:
<setMutationRate>
*/
DLL_EXPORT void setMutationType(struct parameters *params, char const *mutationType);
/*
Function: setNumThreads
Sets the number of threads in the given <parameters>.
The given number of threads is also parsed to ensure a valid value.
Values <1 are invalid. If an invalid value is give a
warning is displayed and the value is left unchanged.
Note:
In order for the CGP-Library to use multiple threads it must be compiled
with openMP flag set (-fopenmp for gcc/mingw).
Note:
The CGP-Library ignores the OMP_NUM_THREADS environment variable. The
only method for setting the number of threads is using <setNumThreads>.
Parameters:
params - pointer to <parameters> structure.
numThreads - The number of threads to be set.
*/
DLL_EXPORT void setNumThreads(struct parameters *params, int numThreads);
DLL_EXPORT void setNP_IN(struct parameters *params, int np);
DLL_EXPORT void setNP_OUT(struct parameters *params, int np);
DLL_EXPORT void setMaxIter_IN(struct parameters *params, int maxiter);
DLL_EXPORT void setMaxIter_OUT(struct parameters *params, int maxiter);
DLL_EXPORT void setCR(struct parameters *params, double cr);
DLL_EXPORT void setF(struct parameters *params, double f);
/*
Title: Chromosome Functions
Description of all functions and structures relating to chromosomes
*/
/*
Function: initialiseChromosome
Initialises a chromosome based on the given <parameters>.
Parameters:
params - pointer to <parameters> structure
Returns:
A pointer to an initialised chromosome structure.
See Also:
<freeChromosome>, <initialiseChromosomeFromFile>, <initialiseChromosomeFromChromosome>
*/
DLL_EXPORT struct chromosome *initialiseChromosome(struct parameters *params, unsigned int * seed);
/*
Function: initialiseChromosomeFromFile
Initialises a chromosome from a given previously saved chromosome.
Note:
Only chromosomes which use node functions defined by the CGP-library can be loaded. Chromosomes which use custom node functions cannot be loaded.
Parameters:
file - char array giving the location of the chromosome to be loaded.
Returns:
A pointer to an initialised chromosome structure.
Example:
(begin code)
struct Chromosome *chromo;
char *chromoFile = "location of Chromosome";
chromo = loadChromosome(chromoFile);
(end)
See Also:
<freeChromosome>, <initialiseChromosome>, <initialiseChromosomeFromChromosome>,
*/
DLL_EXPORT struct chromosome* initialiseChromosomeFromFile(char const *file, unsigned int * seed);
/*
Function: initialiseChromosomeFromChromosome
Initialises a chromosome from a given chromosome.
This functions can be used to create a copy of a chromosome.
Parameters:
chromo - pointer to an initialised chromosome structure.
Returns:
A pointer to an initialised chromosome structure.
See Also:
<freeChromosome>, <initialiseChromosome>, <initialiseChromosomeFromFile>
*/
DLL_EXPORT struct chromosome *initialiseChromosomeFromChromosome(struct chromosome *chromo, unsigned int * seed);
/*
Function: freeChromosome
Frees chromosome instance.
Parameters:
chromo - pointer to an initialised chromosome structure.
See Also:
<initialiseChromosome>, <initialiseChromosomeFromFile>
*/
DLL_EXPORT void freeChromosome(struct chromosome *chromo);
DLL_EXPORT void freeDEChromosome(struct DEChromosome *DEChromo);
/*
Function: printChromosome
Displays the given chromosome to the terminal / command prompt in a human readable format.
Parameters:
chromo - pointer to chromosome structure.
weights - if set as 1 display connections weights
Example:
Typical output from <printChromosome>.
Each input and functioning node is labelled with its index in the chromosome. There is a textual description of the node e.g. *input* for input nodes or the operation for the function nodes. Function node operations are followed by space separated values describing each nodes inputs. Active nodes are also labelled with an *. Finally the last line gives the nodes used as chromosome outputs.
(begin code)
(0): input
(1): mul 0 0 *
(2): add 0 1 *
(3): sub 2 0 *
(4): mul 0 1 *
(5): add 4 3 *
(6): sub 4 2 *
(7): mul 6 5 *
(8): add 5 7 *
(9): mul 1 6
(10): mul 5 3
(11): add 4 1
(12): add 10 3
(13): add 5 11
(14): sub 3 6
(15): div 5 13
outputs: 8
(end)
*/
DLL_EXPORT void printChromosome(struct chromosome *chromo, int weights);
/*
Function: executeChromosome
Executes the given chromosome.
Executes the given chromosome with the given inputs. The dimensions of the inputs arrays must match the dimensions of the chromosome inputs. The chromosome outputs are then accessed using <getChromosomeOutput>.
Parameters:
chromo - pointer to an initialised chromosome structure.
inputs - array of doubles used as inputs to the chromosome
Example:
for a chromosome with three inputs and one outputs.