source: src/main/java/agents/anac/y2018/yeela/Learner.java@ 345

Last change on this file since 345 was 343, checked in by Tim Baarslag, 4 years ago

Fixed all errors in all 2018 agents

File size: 3.4 KB
Line 
1package agents.anac.y2018.yeela;
2
3import java.util.List;
4
5import java.util.Collection;
6import java.util.Collections;
7import java.util.Random;
8import java.util.Vector;
9
10import genius.core.Bid;
11import genius.core.issue.Issue;
12import genius.core.parties.NegotiationInfo;
13
14
15public class Learner {
16 private int MAX_EVOLUTIONS = 10; //TODO
17 private int POPULATION_SIZE = 200; // TODO
18 private int MATING_POOL_SIZE = 150; // TODO
19 private double CROSS_RATE = 0.6; // TODO
20 private double MUT_RATE = 0.05; // TODO
21 private double ELITISM_RATE = 0.1; // TODO
22
23 private List<Individual> m_population;
24 private Random m_rand;
25 private NegotiationInfo m_info;
26
27 public Learner(Bid bestBid, NegotiationInfo info)
28 {
29 m_info = info;
30
31 m_population = new Vector<Individual>();
32 m_population.add(new Individual(bestBid, info));
33
34 // generate initial population
35 m_rand = new Random();
36 for (int i = 0; i < POPULATION_SIZE - 1; ++i)
37 {
38 Bid randomBid = info.getUtilitySpace().getDomain().getRandomBid(m_rand);
39 m_population.add(new Individual(randomBid, info));
40 }
41 }
42
43 private List<Individual> Best()
44 {
45 Vector<Individual> out = new Vector<Individual>();
46
47 try
48 {
49 Collections.sort(m_population);
50
51 int popSize = m_population.size();
52 int start = (int)(popSize - (popSize * ELITISM_RATE));
53 int end = popSize;
54
55 for (Individual ind : m_population.subList(start, end))
56 {
57 out.add(ind.Clone());
58 }
59 }
60 catch (Exception e)
61 {
62 e.printStackTrace();
63 }
64
65 return out;
66 }
67
68 private List<Individual> Selection()
69 {
70 List<Individual> matingPool = new Vector<Individual>();
71 int popSize = m_population.size();
72 try
73 {
74 Collections.sort(m_population);
75 boolean[] taken = new boolean[popSize];
76
77 while (matingPool.size() < MATING_POOL_SIZE)
78 {
79 for (int i = 0; i < popSize; ++i)
80 {
81 if(!taken[i])
82 {
83 double pr = m_rand.nextDouble();
84 if ((1.0 / (i + 1)) > pr)
85 {
86 matingPool.add(m_population.get(popSize - 1 - i).Clone());
87 taken[i] = true;
88 }
89 }
90 }
91 }
92 }
93 catch (Exception e)
94 {
95 e.printStackTrace();
96 matingPool = m_population.subList(0, (int)(m_population.size() * 0.75));
97 }
98
99
100 return matingPool;
101 }
102
103 public Bid run(Bid counterOffer)
104 {
105 m_population.forEach(ind->ind.UpdateOpponent(counterOffer));
106 Collection<Individual> new_generation;
107 for (int i = 0; i < MAX_EVOLUTIONS; i++)
108 {
109 new_generation = Best();
110 List<Individual> mating_pool = Selection();
111 while(new_generation.size() < POPULATION_SIZE)
112 {
113 for (int j = 0; j < mating_pool.size() - 1; j += 2)
114 {
115 Individual ind = mating_pool.get(j);
116 Individual ind2 = mating_pool.get(j+1);
117 if (m_rand.nextDouble() > CROSS_RATE)
118 {
119 ind.Crossover(ind2);
120 }
121 if (m_rand.nextDouble() > MUT_RATE)
122 {
123 ind.Mutate();
124 }
125 if (m_rand.nextDouble() > MUT_RATE)
126 {
127 ind2.Mutate();
128 }
129 new_generation.add(ind);
130 new_generation.add(ind2);
131 }
132 }
133 m_population = new Vector<Individual>(new_generation);
134 }
135
136 Bid answer = m_info.getUtilitySpace().getDomain().getRandomBid(m_rand);
137 Individual ind = Best().get(0);
138
139 for (Issue issue : answer.getIssues())
140 {
141 answer.putValue(issue.getNumber(), ind.GetValue(issue.getNumber()));
142 }
143
144 return answer;
145 }
146}
Note: See TracBrowser for help on using the repository browser.