source: src/main/java/agents/anac/y2016/maxoops/TFComponent.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 8.9 KB
Line 
1/*
2 * Author: Max W. Y. Lam (Aug 1 2015)
3 * Version: Milestone 1
4 *
5 * */
6
7package agents.anac.y2016.maxoops;
8
9import java.util.ArrayList;
10import java.util.Random;
11
12import genius.core.timeline.TimeLineInfo;
13
14public class TFComponent {
15
16 // Adjustable Parameters
17 protected final int L;
18 protected double alpha, beta, tau, zeta, lambda, gamma;
19
20 // Fixed Parameters
21 public final static String trendType = "linear damped",
22 costType = "linear";
23
24 private MaxOops agent;
25 private TimeLineInfo timeline;
26 public double[] adapSubInvMean, countSubInv;
27 public double[][] oppAdapSubInvMean, oppCountSubInv;
28 public int l;
29 public double lltime, dltime, competitiveness;
30 public double compaction, lf_slope, lt_slope, lt_filp, lconcess, lt_f;
31 Random rand;
32
33 // For Debug
34 public ArrayList<Double> f_slope_time_series = null;
35 public ArrayList<Double> t_filp_time_series = null;
36 public ArrayList<Double> concess_time_series = null;
37
38 public TFComponent(MaxOops agent, TimeLineInfo timeline) {
39 // Set Adjustable Parameters First
40 agent.params.addParam("TFComponent.L",
41 (int) (30. / Math.pow(agent.delta, 0.35)));
42 this.L = (int) agent.params.getParam("TFComponent.L");
43 agent.params.addParam("TFComponent.alpha", 0);
44 this.alpha = agent.params.getParam("TFComponent.alpha");
45 agent.params.addParam("TFComponent.beta", 1.5);
46 this.beta = agent.params.getParam("TFComponent.beta");
47 agent.params.addParam("TFComponent.tau", 0.75);
48 this.tau = agent.params.getParam("TFComponent.tau");
49 agent.params.addParam("TFComponent.zeta", 1.5);
50 this.zeta = agent.params.getParam("TFComponent.zeta");
51 agent.params.addParam("TFComponent.lambda", 0.0);
52 this.lambda = agent.params.getParam("TFComponent.lambda");
53 agent.params.addParam("TFComponent.gamma", 0.5);
54 this.gamma = agent.params.getParam("TFComponent.gamma");
55
56 this.rand = new Random(agent.hashCode());
57 this.agent = agent;
58 this.l = 0;
59 this.competitiveness = 0;
60 this.lltime = 0;
61 this.dltime = 0;
62 this.compaction = agent.maxUtil - agent.minUtil;
63 this.timeline = timeline;
64 this.lf_slope = agent.secMaxUtil * (1 - agent.delta) + agent.maxUtil
65 * agent.delta;
66 this.lt_slope = 0;
67 this.lt_f = 0;
68 this.lt_filp = agent.delta * 0.85;
69 this.lconcess = 0;
70 this.adapSubInvMean = new double[L];
71 this.countSubInv = new double[L];
72 this.oppAdapSubInvMean = new double[agent.numParties - 1][L];
73 this.oppCountSubInv = new double[agent.numParties - 1][L];
74 for (int i = 0; i < L; i++) {
75 adapSubInvMean[i] = 0;
76 countSubInv[i] = 0;
77 for (int j = 0; j < agent.numParties - 1; j++) {
78 oppAdapSubInvMean[j][i] = 0;
79 oppCountSubInv[j][i] = 0;
80 }
81 }
82 this.f_slope_time_series = null;
83 this.t_filp_time_series = null;
84 this.concess_time_series = null;
85 }
86
87 public void setCompaction(double val) {
88 compaction = val;
89 }
90
91 public double opponentsConcessionSlope() {
92 try {
93 double maxConcessionSlpoe = agent.delta - tau - 2;
94 double minConcessionSlpoe = agent.delta - tau + 1;
95 for (int i = 0; i < agent.numParties - 1; i++) {
96 double slope = oppAdapSubInvMean[i][l]
97 - oppAdapSubInvMean[i][Math.max(0, l - 1)];
98 slope /= Math.max(dltime, 0.001);
99 // maximum of slope = minimum of concession rate
100 maxConcessionSlpoe = Math.min(
101 Math.max(maxConcessionSlpoe, slope), 1.);
102 minConcessionSlpoe = Math.max(
103 Math.min(minConcessionSlpoe, slope), -1.);
104 }
105 double concess = (maxConcessionSlpoe * (1. - agent.delta) + minConcessionSlpoe
106 * agent.delta);
107 if (Double.isNaN(concess)) {
108 concess = 0;
109 }
110 return concess;
111 } catch (Exception e) {
112 return 0;
113 }
114 }
115
116 public int getSubIntvByTime(double time) {
117 int i;
118 for (i = 0; i < L; i++) {
119 if (time < (i + 1) * 1. / L) {
120 break;
121 }
122 }
123 return i;
124 }
125
126 public int getCurrentSubIntv() {
127 return getSubIntvByTime(timeline.getTime());
128 }
129
130 public void recordUtility(double util, int opponent) {
131 double time = timeline.getTime();
132 countSubInv[l] += 1.;
133 if (opponent >= 0) {
134 oppCountSubInv[opponent][l] += 1.;
135 }
136 if (l < 2) {
137 adapSubInvMean[l] += (util - adapSubInvMean[l]) / countSubInv[l];
138 if (opponent >= 0) {
139 oppAdapSubInvMean[opponent][l] += (util - oppAdapSubInvMean[opponent][l])
140 / countSubInv[l];
141 }
142 } else {
143 adapSubInvMean[l] += (util - (0.6 * adapSubInvMean[l] + 0.3
144 * adapSubInvMean[l - 1] + 0.1 * adapSubInvMean[l - 2]))
145 / countSubInv[l];
146 if (opponent >= 0) {
147 oppAdapSubInvMean[opponent][l] += (util - (0.6
148 * oppAdapSubInvMean[opponent][l] + 0.3
149 * oppAdapSubInvMean[opponent][l - 1] + 0.1 * oppAdapSubInvMean[opponent][l - 2]))
150 / oppCountSubInv[opponent][l];
151 }
152 }
153 if (l < getCurrentSubIntv()) {
154 filppingTime();
155 dltime = time - lltime;
156 lltime = time;
157 double u_low = concessionLimit();
158 double concess = opponentsConcessionSlope();
159 competitiveness = (agent.medianUtil / Math.pow(agent.delta, time) - adapSubInvMean[l])
160 * 0.6 + competitiveness * 0.3;
161 double f_slope = lf_slope;
162 if (time < 0.2 - 0.4 * agent.delta) {
163 f_slope *= Math.pow(agent.delta, 0.2 * time);
164 }
165 if (competitiveness > alpha) {
166 lambda = -(lf_slope - adapSubInvMean[l])
167 / Math.sqrt(agent.delta);
168 beta *= 0.98;
169 competitiveness -= time * agent.stdUtil * agent.delta;
170 } else {
171 lambda = (1. - 0.5 / Math.sqrt(agent.delta)) * agent.stdUtil;
172 competitiveness += time / agent.stdUtil * agent.delta
173 * Math.sqrt(1 - time);
174 }
175 if (adapSubInvMean[l] >= agent.uqUtil) {
176 double state1 = (adapSubInvMean[l] - agent.uqUtil)
177 * (time - lt_slope) / agent.stdUtil;
178 MaxOops.log1.println("State 1: " + state1);
179 f_slope += state1;
180 } else if (adapSubInvMean[l] >= agent.medianUtil) {
181 double state2 = (agent.uqUtil - f_slope) * (time - lt_slope)
182 / agent.stdUtil;
183 MaxOops.log1.println("State 2: " + state2);
184 f_slope += state2;
185 } else {
186 double state3 = (adapSubInvMean[l] - f_slope)
187 * (time - lt_slope) / agent.stdUtil;
188 MaxOops.log1.println("State 3: " + state3);
189 f_slope += state3;
190 }
191 f_slope += gamma
192 * ((concess + lambda) - adapSubInvMean[l] + u_low
193 + Math.pow(agent.delta, 2)
194 / (1 + zeta + agent.delta) - (lf_slope - agent.theta)
195 * Math.pow(time, 3 * agent.delta))
196 * (time - lt_slope);
197 if (time > lt_filp) {
198 double u_epl = (adapSubInvMean[l] + beta * agent.stdUtil)
199 * Math.sqrt(1.2 - agent.delta)
200 + (agent.meanUtil + beta * agent.stdUtil)
201 * (1 - Math.sqrt(1.2 - agent.delta));
202 f_slope += (f_slope - u_epl) / (time - 1.) * (time - lt_slope);
203 if (concess > 0) {
204 f_slope += (time - lt_slope) * (1 - time) * concess
205 * agent.delta;
206 }
207 } else {
208 if (competitiveness > 0) {
209 f_slope -= (time - lt_slope) * (1 - time) / agent.stdUtil;
210 }
211 if (concess > 0) {
212 f_slope += (time - lt_slope) * (1 - time) * concess
213 / agent.stdUtil;
214 }
215 }
216 f_slope = Math.max(Math.max(u_low, f_slope), adapSubInvMean[l]);
217 MaxOops.log2.println(competitiveness + ", " + alpha + ", " + beta
218 + ", " + zeta + ", " + lambda + ", " + tau + ", " + gamma
219 + ", " + f_slope);
220 lf_slope = f_slope;
221 lconcess = concess;
222 lt_slope = time;
223 if (agent.DEBUG && time > 0.98) {
224 // PLOTComponent.plotDataPoints(f_slope_time_series,
225 // "f_slope Time Series at l="+String.valueOf(l+1));
226 // PLOTComponent.plotDataPoints(t_filp_time_series,
227 // "t_filp Time Series at l="+String.valueOf(l+1));
228 // PLOTComponent.plotDataPoints(concess_time_series,
229 // "concess Time Series at l="+String.valueOf(l+1));
230 // agent.DEBUG = false;
231 }
232 l++;
233 }
234 }
235
236 public double filppingTime() {
237 double time = timeline.getTime();
238 double concess = opponentsConcessionSlope();
239 double t_filp = lt_filp - Math.pow(1. - agent.delta - lt_filp, 2.)
240 * concess * (time - lt_f);
241 lt_f = time;
242 lt_filp = t_filp;
243 return Math.max(Math.min(t_filp, agent.delta * 1.5), agent.delta / 2.);
244 }
245
246 public double concessionLimit() {
247 double u_least = Math.max(agent.minUtil + agent.delta * agent.stdUtil,
248 agent.theta);
249 double u_mu = agent.meanUtil;
250 double u_med = agent.medianUtil;
251 double u_low = Math
252 .max(Math.max(Math.min(u_mu, u_med), adapSubInvMean[l]
253 * agent.delta)
254 - (0.5 - agent.delta) * agent.stdUtil, u_least);
255 return u_low;
256 }
257
258 public double thresholdFunc() {
259 // double time = timeline.getTime();
260 // MaxOops.log1.println("time = "+time+", f_slope = "+lf_slope+", median = "+agent.medianUtil+", competitiveness = "+competitiveness
261 // +", mean = "+agent.meanUtil+", adap mean = "+adapSubInvMean[l]+", std = "+agent.stdUtil
262 // +", lq = "+agent.lqUtil+", uq = "+agent.uqUtil+", concess = "+lconcess
263 // +", ulow = "+concessionLimit()+", t_filp = "+lt_filp);
264 if (f_slope_time_series == null) {
265 f_slope_time_series = new ArrayList<Double>();
266 t_filp_time_series = new ArrayList<Double>();
267 concess_time_series = new ArrayList<Double>();
268 }
269 f_slope_time_series.add(lf_slope);
270 t_filp_time_series.add(0, lt_filp);
271 concess_time_series.add(0, lconcess);
272 return lf_slope;
273 }
274
275}
Note: See TracBrowser for help on using the repository browser.