-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenetics
133 lines (115 loc) · 5.38 KB
/
Genetics
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Rawan
*/
import java.util.Vector;
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import org.neuroph.core.NeuralNetwork;
public class Genetics {
public final double mutationprobability = .1;
public final double maximumfitness = .6;
public Vector[] Genetic_Algorithm(Vector[] population){
double populationfitness = 0; //average population fitness
//repeat until population is fit enough
do{
Vector[] newpop = new Vector[TestGenetics.populationsize]; //create a new population
for(int i=0; i<newpop.length; i++)
newpop[i] = new Vector<Song>();
double fitnessvalue=0; //the new population's fitness value
//Song[][] newpop = new Song[TestGenetics.populationsize][TestGenetics.playlistsize];//create a new population
for(int i=0; i<TestGenetics.populationsize; i++)
{
Vector<Song> x = randomselection(population); //parent1
Vector<Song> y = randomselection(population); //parent2
Vector<Song> child = reproduce(x, y); //returns a child (A Vector of song objects)
double cmprob = Math.random(); //generate a random child mutation probability
if(cmprob <mutationprobability)
mutate(child);
fitnessvalue += fitnessfunction(child);
newpop[i].add(child);
}
populationfitness = fitnessvalue/TestGenetics.populationsize; //average fitness (0-1)
population = newpop;
}while(populationfitness<maximumfitness); //repeat until population fitness exceeds maximumfitness
return population;
}
//takes two vectors x and y. Produces a child (child) by concating x and y at a random point
public Vector<Song> reproduce(Vector<Song> x, Vector<Song> y){
int c = (int) (TestGenetics.playlistsize*Math.random()); //select a random concatination index
Vector<Song> child = new Vector<Song>();
//Substring(x, 1 to c)
for(int i=0; i<c; i++)
{
Song temp = x.elementAt(i);
child.add(new Song(temp.getTitle(), temp.getArtist(), temp.getPlaycount()));
}
//Substring(y, c to size)
for(int i=c; i<y.size(); i++)
{
Song temp = y.elementAt(i);
child.add(new Song(temp.getTitle(), temp.getArtist(), temp.getPlaycount()));
}
return child;
}
//Select a random Vector of songs from a population and return it
public Vector<Song> randomselection(Vector[] pop)
{
Vector<Song> random = new Vector<Song>();
int index = (int)(TestGenetics.populationsize*Math.random()); //creates a random index from 0-population size
//copy the song objects inside the randomly selected population
for(int i=0; i<TestGenetics.playlistsize; i++)
{
Song temp = (Song)pop[index].elementAt(i);//gets the song at index i
random.add(new Song(temp.getTitle(), temp.getArtist(), temp.getPlaycount())) ; //copy the song's values into a new object. Then add the object ot the vector
}
return random;
}
public void mutate(Vector<Song> c){
Song randsong = null; //the random song to be extracted from the file
try{
Scanner input = new Scanner(new File("musicdata.txt"));
int filerandom = (int) (Math.random()*TestGenetics.filesize); //generate a random index within the file
int count = 0;
//until we reach our random index or the end of the file
while(count<filerandom && input.hasNext()){
String line = input.nextLine();
}
//read the line at index random
String line = input.nextLine();
String title="" ;
int intindex=0;
for(int i=0; i<line.length(); i++)
{
if(Character.isDigit(line.charAt(i)))
{intindex = i;
break;
}
title+=line.charAt(i); //extract the title
}
randsong = new Song(title, line.charAt(intindex), line.charAt(intindex+2)); //the random song extracted from the file
}catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
int mutationindex = (int)(Math.random()*TestGenetics.playlistsize);// random number from 0-playlistsize
c.set(mutationindex, randsong); //replace the song at the mutation index with the random song from the file
}
//calculates the individual's fitness (0-1)
//using the NN
public double fitnessfunction(Vector<Song> x){
double fitness=0;//an average of the NN's output
NeuralNetwork nn = NeuralNetwork.load("MLP11.nnet"); //initilize the NN
for(Song song: x){
nn.setInput(song.getArtist(), song.getPlaycount());//sets the NN input to song's (artist id, playcount)
nn.calculate();
double[] output = nn.getOutput();
fitness += output[0]; //adds the output(the degree of loving a track)
}
return fitness/x.size(); //returns the average
}
}