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