Skip to content

Commit b3934be

Browse files
committed
Initial commit of building blocks for GA
1 parent e5e06d6 commit b3934be

23 files changed

+1498
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.class
2+
*.iml
3+
target/
4+
target/*.*
5+
.idea/

pom.xml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ArtificalIntelligenceDemo</groupId>
8+
<artifactId>ArtificalIntelligenceDemo</artifactId>
9+
<version>1.0</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.11</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.uncommons.maths</groupId>
19+
<artifactId>uncommons-maths</artifactId>
20+
<version>1.2.2a</version>
21+
</dependency>
22+
</dependencies>
23+
24+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package net.dervism.genericalgorithms;
2+
3+
/**
4+
* A 64-bit immutable Chromosome that contains several genes.
5+
*
6+
* Implementation is based on the encoding proposed at the Chess Programming Wiki:
7+
* https://chessprogramming.wikispaces.com/Encoding+Moves
8+
*
9+
* Created by dervism on 27.12.13.
10+
*/
11+
public class Chromosome {
12+
13+
public long genes;
14+
15+
public Chromosome() {
16+
this(0L);
17+
}
18+
19+
public Chromosome(long genes) {
20+
this.genes = genes;
21+
}
22+
23+
public Chromosome(Chromosome c) {
24+
this.genes |= c.genes;
25+
}
26+
27+
public Chromosome setNthBit(long n) {
28+
Chromosome c = new Chromosome(genes | 1L << n);
29+
return new Chromosome(c);
30+
}
31+
32+
// ^ = exclusive or, xor
33+
public Chromosome flipNthBit(long n) {
34+
Chromosome c = new Chromosome(genes ^ (1L << n));
35+
return new Chromosome(c);
36+
}
37+
38+
// if n'th bit is 1, then return true, otherwise false
39+
public boolean getNthBit(long n) {
40+
return !((genes & (1L << n)) == 0);
41+
}
42+
43+
public Chromosome clearNthBit(long n) {
44+
Chromosome c = new Chromosome(genes & ~(1L << n));
45+
return new Chromosome(c);
46+
}
47+
48+
public byte[] toBytes() {
49+
return longToByte(genes);
50+
}
51+
52+
public static byte[] longToByte(long l) {
53+
// a long occupies 8 bytes
54+
byte[] ret = new byte[8];
55+
56+
// do a unsigned right shift
57+
// - push everything to the right and insert 0's from the left
58+
ret[0] = (byte)(l >>> 56);
59+
ret[1] = (byte)(l >>> 48);
60+
ret[2] = (byte)(l >>> 40);
61+
ret[3] = (byte)(l >>> 32);
62+
ret[4] = (byte)(l >>> 24);
63+
ret[5] = (byte)(l >>> 16);
64+
ret[6] = (byte)(l >>> 8);
65+
ret[7] = (byte)(l >>> 0);
66+
67+
return ret;
68+
}
69+
70+
public static long byteToLong(byte[] bytes) {
71+
return (bytes[0] << 56)
72+
+ ((bytes[1] & 0xFF) << 48)
73+
+ ((bytes[2] & 0xFF) << 40)
74+
+ ((bytes[3] & 0xFF) << 32)
75+
+ ((bytes[4] & 0xFF) << 24)
76+
+ ((bytes[5] & 0xFF) << 16)
77+
+ ((bytes[6] & 0xFF) << 8)
78+
+ (bytes[7] & 0xFF);
79+
}
80+
81+
// Chromosome class is immutable, however, if you want to allow
82+
// mutability then use this setter.
83+
private void setGenes(long genes) {
84+
this.genes = genes;
85+
}
86+
87+
/**
88+
* Prints the binary string representation of this Chromosome's bits
89+
* @return
90+
*/
91+
@Override
92+
public String toString() {
93+
String ret = "";
94+
String group = "";
95+
for(int i = 0; i < Long.numberOfLeadingZeros(genes); i++) ret += '0';
96+
ret += Long.toBinaryString(genes);
97+
for (int i = 1; i <= ret.length(); i++) {
98+
if ((i % 4) == 0) {
99+
group += ret.charAt(i-1) + " ";
100+
}
101+
else group += ret.charAt(i-1);
102+
}
103+
ret = group;
104+
return ret;
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package net.dervism.genericalgorithms;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* Created by dervism on 28.12.13.
7+
*/
8+
public class ChromosomeEncoder implements Encoder {
9+
10+
// the start index
11+
private long first_val_shift = 7;
12+
private long second_val_shift = 14;
13+
14+
// the number of bits to use for encoding
15+
private long index_mask = 0x7FL; // 7 bits
16+
private long first_val_mask = 0x7FL; // 7 bits
17+
private long second_val_mask = 0x7FL; // 7 bits
18+
19+
@Override
20+
public Chromosome createChromosome(long... values) {
21+
return createChromosome(values[0], values[1], values[2]);
22+
}
23+
24+
public Chromosome createChromosome(long index, long first_val, long second_val) {
25+
26+
long genes = 0x0L;
27+
28+
genes = 0 | index | (first_val << first_val_shift) | (second_val << second_val_shift);
29+
30+
Chromosome chromosome = new Chromosome(genes);
31+
32+
return chromosome;
33+
}
34+
35+
@Override
36+
public Chromosome createRandomChromosome(Random random) {
37+
return null;
38+
}
39+
40+
public int getIndex(Chromosome chromosome) {
41+
return (int)(chromosome.genes & index_mask);
42+
}
43+
44+
public int getFirstVal(Chromosome chromosome) {
45+
return (int)((chromosome.genes >> first_val_shift) & first_val_mask);
46+
}
47+
48+
public int getSecondVal(Chromosome chromosome) {
49+
return (int)((chromosome.genes >> second_val_shift) & second_val_mask);
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.dervism.genericalgorithms;
2+
3+
/**
4+
* This class handles the evolution of bit-encoded chromosomes.
5+
*
6+
* Created by dervism on 14/02/14.
7+
*
8+
*/
9+
public abstract class ChromosomeEvolution implements Evolution {
10+
11+
@Override
12+
public Chromosome mutate(Chromosome chromosome) {
13+
return null;
14+
}
15+
16+
@Override
17+
public Chromosome crossover(Chromosome mother, Chromosome father) {
18+
return null;
19+
}
20+
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.dervism.genericalgorithms;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by dervism on 14/02/14.
8+
*/
9+
public abstract class ChromosomePopulation implements Population {
10+
11+
public abstract List<Chromosome> createPopulation(int size);
12+
13+
public List<Chromosome> createEmptyPopulation(int size) {
14+
15+
List<Chromosome> pop = new ArrayList<Chromosome>(size);
16+
17+
for (int i = 0; i < size; i++) {
18+
pop.add(new Chromosome(0L));
19+
}
20+
21+
return pop;
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.dervism.genericalgorithms;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* Created by dervism on 14/02/14.
7+
*/
8+
9+
public interface Encoder {
10+
11+
public Chromosome createChromosome(long... values);
12+
13+
public Chromosome createRandomChromosome(Random random);
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package net.dervism.genericalgorithms;
2+
3+
/**
4+
* Created by dervism on 14/02/14.
5+
*/
6+
public interface Evolution {
7+
8+
public Chromosome mutate(Chromosome chromosome);
9+
10+
public Chromosome crossover(Chromosome left, Chromosome right);
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package net.dervism.genericalgorithms;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Created by dervism on 14/02/14.
7+
*/
8+
public interface Population {
9+
10+
public List<Chromosome> createPopulation(int size);
11+
12+
}

src/main/java/net/dervism/index.html

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package net.dervism.knapsack;
2+
3+
import net.dervism.genericalgorithms.Chromosome;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Random;
8+
9+
/**
10+
* Created by dervism on 19/02/14.
11+
*/
12+
public class Knapsack {
13+
14+
public int[] objectValue;
15+
public int[] objectSpace;
16+
17+
public int[] knapsack;
18+
public int knapsackSpace;
19+
20+
public Random random;
21+
22+
public Knapsack() {
23+
random = new Random(1234);
24+
createObjectsAndValues(64);
25+
}
26+
27+
private void createObjectsAndValues(int objects) {
28+
objectValue = new int[objects];
29+
objectSpace = new int[objects];
30+
knapsack = new int[32];
31+
knapsackSpace = 3000;
32+
33+
for (int i = 0; i < objectValue.length; i++) {
34+
objectValue[i] = random.nextInt(200);
35+
objectSpace[i] = random.nextInt(50);
36+
}
37+
}
38+
39+
public List<Chromosome> createRandomPopulation(int size) {
40+
List<Chromosome> population = new ArrayList<>(size * 2);
41+
Random rand = new Random();
42+
43+
for (int i = 0; i < size; i++) {
44+
Chromosome chromosome = new Chromosome();
45+
for (int j = 0; j < 64; j++) {
46+
if (rand.nextBoolean())
47+
chromosome = chromosome.setNthBit(j);
48+
}
49+
population.add(chromosome);
50+
}
51+
52+
return population;
53+
}
54+
55+
public int calcFitness(Chromosome chromosome) {
56+
int fitness = 0;
57+
58+
for (int i = 0; i < 64; i++) {
59+
if (chromosome.getNthBit(i)) {
60+
fitness += objectValue[i];
61+
}
62+
}
63+
64+
return fitness;
65+
}
66+
67+
public int calcSpace(Chromosome chromosome) {
68+
int totalSpace = 0;
69+
70+
for (int i = 0; i < 64; i++) {
71+
if (chromosome.getNthBit(i)) {
72+
totalSpace += objectSpace[i];
73+
}
74+
}
75+
76+
return totalSpace;
77+
}
78+
79+
public int itemsInside(Chromosome chromosome) {
80+
int items = 0;
81+
82+
for (int i = 0; i < 64; i++) {
83+
if (chromosome.getNthBit(i)) {
84+
items++;
85+
}
86+
}
87+
88+
return items;
89+
}
90+
91+
public void run() {
92+
93+
}
94+
95+
}

0 commit comments

Comments
 (0)