1 | package agents.anac.y2013.MetaAgent;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import java.util.AbstractMap.SimpleEntry;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.Set;
|
---|
7 |
|
---|
8 | import agents.anac.y2013.MetaAgent.agentsData.AgentData;
|
---|
9 | import agents.anac.y2013.MetaAgent.agentsData.agents.DataAgentLG;
|
---|
10 | import agents.anac.y2013.MetaAgent.agentsData.agents.DataBRAMAgent2;
|
---|
11 | import agents.anac.y2013.MetaAgent.agentsData.agents.DataCUHKAgent;
|
---|
12 | import agents.anac.y2013.MetaAgent.agentsData.agents.DataIAMhaggler2012;
|
---|
13 | import agents.anac.y2013.MetaAgent.agentsData.agents.DataOMACagent;
|
---|
14 | import agents.anac.y2013.MetaAgent.agentsData.agents.DataTheNegotiatorReloaded;
|
---|
15 | import agents.anac.y2013.MetaAgent.portfolio.AgentLG.*;
|
---|
16 | import agents.anac.y2013.MetaAgent.portfolio.AgentMR.*;
|
---|
17 | import agents.anac.y2013.MetaAgent.portfolio.BRAMAgent2.*;
|
---|
18 | import agents.anac.y2013.MetaAgent.portfolio.CUHKAgent.*;
|
---|
19 | import agents.anac.y2013.MetaAgent.portfolio.IAMhaggler2012.*;
|
---|
20 | import agents.anac.y2013.MetaAgent.portfolio.OMACagent.*;
|
---|
21 | import agents.anac.y2013.MetaAgent.portfolio.thenegotiatorreloaded.*;
|
---|
22 | import genius.core.*;
|
---|
23 |
|
---|
24 |
|
---|
25 | @SuppressWarnings("serial")
|
---|
26 | public class AgentManager implements Serializable{
|
---|
27 | HashMap<String, SimpleEntry<Integer, SimpleEntry<Double, Double>>> agents; //<Agent name, <countUsages, predictedScore>>
|
---|
28 | int playsCount = 0;
|
---|
29 | int predictionFactor = 5;
|
---|
30 | String selectedAgent = "";
|
---|
31 | SimpleEntry<Integer, SimpleEntry<Double, Double>> selectedInfo = new SimpleEntry<Integer, SimpleEntry<Double, Double>>(0,new SimpleEntry<Double, Double>(0.0,0.0)); //<counter,predicted,previousScores>
|
---|
32 | double averageUtility = 0;
|
---|
33 | double stdev = 0;
|
---|
34 |
|
---|
35 | public AgentManager(){
|
---|
36 | agents = new HashMap<String, SimpleEntry<Integer, SimpleEntry<Double, Double>>>();
|
---|
37 | agents.put("AgentLG", new SimpleEntry<Integer, SimpleEntry<Double, Double>>(0,new SimpleEntry<Double, Double>(0.0,0.0)));
|
---|
38 | //agents.put("AgentMR", new SimpleEntry<Integer, Double>(0, 0.0));
|
---|
39 | agents.put("BRAMAgent2", new SimpleEntry<Integer, SimpleEntry<Double, Double>>(0,new SimpleEntry<Double, Double>(0.0,0.0)));
|
---|
40 | agents.put("CUHKAgent", new SimpleEntry<Integer, SimpleEntry<Double, Double>>(0,new SimpleEntry<Double, Double>(0.0,0.0)));
|
---|
41 | agents.put("IAMhaggler2012", new SimpleEntry<Integer, SimpleEntry<Double, Double>>(0,new SimpleEntry<Double, Double>(0.0,0.0)));
|
---|
42 | agents.put("OMACagent", new SimpleEntry<Integer, SimpleEntry<Double, Double>>(0,new SimpleEntry<Double, Double>(0.0,0.0)));
|
---|
43 | agents.put("TheNegotiatorReloaded", new SimpleEntry<Integer, SimpleEntry<Double, Double>>(0,new SimpleEntry<Double, Double>(0.0,0.0)));
|
---|
44 |
|
---|
45 | }
|
---|
46 |
|
---|
47 | private Agent GetAgent(String name){
|
---|
48 |
|
---|
49 | if (name.equalsIgnoreCase("AgentLG")) return new AgentLG();
|
---|
50 | else if (name.equalsIgnoreCase("AgentMR")) return new AgentMR();
|
---|
51 | else if (name.equalsIgnoreCase("BRAMAgent2")) return new BRAMAgent2();
|
---|
52 | else if (name.equalsIgnoreCase("CUHKAgent")) return new CUHKAgent();
|
---|
53 | else if (name.equalsIgnoreCase("IAMhaggler2012")) return new IAMhaggler2012();
|
---|
54 | else if (name.equalsIgnoreCase("OMACagent")) return new OMACagent();
|
---|
55 | else if (name.equalsIgnoreCase("TheNegotiatorReloaded")) return new TheNegotiatorReloaded();
|
---|
56 |
|
---|
57 | return new CUHKAgent(); //default, as winner of 2012 contest.
|
---|
58 | }
|
---|
59 |
|
---|
60 | public AgentData GetAgentData(String name){
|
---|
61 | if (name.equalsIgnoreCase("AgentLG")) return new DataAgentLG();
|
---|
62 | else if (name.equalsIgnoreCase("AgentMR")) return new DataAgentLG();
|
---|
63 | else if (name.equalsIgnoreCase("BRAMAgent2")) return new DataBRAMAgent2();
|
---|
64 | else if (name.equalsIgnoreCase("CUHKAgent")) return new DataCUHKAgent();
|
---|
65 | else if (name.equalsIgnoreCase("IAMhaggler2012")) return new DataIAMhaggler2012();
|
---|
66 | else if (name.equalsIgnoreCase("OMACagent")) return new DataOMACagent();
|
---|
67 | else if (name.equalsIgnoreCase("TheNegotiatorReloaded")) return new DataTheNegotiatorReloaded();
|
---|
68 |
|
---|
69 | return new DataCUHKAgent(); //default, as winner of 2012 contest.
|
---|
70 | }
|
---|
71 |
|
---|
72 | public Agent SelectBestAgent(){
|
---|
73 | String bestAgent = "";
|
---|
74 | double bestScore = -1;
|
---|
75 |
|
---|
76 | //String fileName = "MetaAgent_Log.csv"; //need to delete!
|
---|
77 | //BufferedWriter out = null; //
|
---|
78 | //try { //
|
---|
79 | // FileWriter fstream = new FileWriter(fileName,true); //
|
---|
80 | // out = new BufferedWriter(fstream); //
|
---|
81 |
|
---|
82 | for (String agent : agents.keySet()) {
|
---|
83 | SimpleEntry<Integer, SimpleEntry<Double, Double>> information = agents.get(agent);
|
---|
84 | double curr = (information.getValue().getValue()*information.getKey() + information.getValue().getKey()*predictionFactor)/(information.getKey()+predictionFactor) ;
|
---|
85 | double curr2 = Math.sqrt(stdev * Math.log(playsCount+agents.size()*predictionFactor)/(information.getKey()+predictionFactor)); //UCB MAB: X_j + sqrt(2ln(n)/n_j)
|
---|
86 | curr += curr2;
|
---|
87 | // out.append(agent + "," + information.getKey() + "," + information.getValue().getValue() + "," + information.getValue().getKey() + "," + curr); //
|
---|
88 | // out.newLine();//
|
---|
89 |
|
---|
90 | if (bestScore < curr || //regular win
|
---|
91 | (bestScore == curr && agents.get(bestAgent).getKey() > information.getKey())){ //tieBreak - take the agent with less performances
|
---|
92 | bestScore = curr;
|
---|
93 | bestAgent = agent;
|
---|
94 | selectedInfo = information;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | selectedAgent = bestAgent;
|
---|
98 |
|
---|
99 |
|
---|
100 | //System.out.println("selected Agent: " + bestAgent + " --> " + bestScore); //
|
---|
101 | //out.append(",,,"+bestAgent+","+bestScore); //
|
---|
102 | //out.newLine(); //
|
---|
103 |
|
---|
104 | //} catch (Exception e) { System.out.println("Error in WriteFile: " + e.toString()); } //
|
---|
105 | //finally{ try { //
|
---|
106 | // out.close(); //
|
---|
107 | //} catch (Exception e2) { }} //
|
---|
108 |
|
---|
109 | return GetAgent(selectedAgent);
|
---|
110 | }
|
---|
111 |
|
---|
112 | public boolean IsUsed(){
|
---|
113 | return playsCount > agents.size();
|
---|
114 | }
|
---|
115 |
|
---|
116 | public Set<String> GetAgents(){
|
---|
117 | return agents.keySet();
|
---|
118 | }
|
---|
119 |
|
---|
120 | public void UpdateUtility (String agent, double util){
|
---|
121 | if (agent == ""){
|
---|
122 | selectedInfo = new SimpleEntry<Integer, SimpleEntry<Double, Double>>
|
---|
123 | (selectedInfo.getKey()+1, new SimpleEntry<Double, Double>
|
---|
124 | (selectedInfo.getValue().getKey(),
|
---|
125 | (selectedInfo.getValue().getValue()*selectedInfo.getKey() + util - averageUtility)/(selectedInfo.getKey() + 1)));
|
---|
126 | agents.put(selectedAgent, selectedInfo);
|
---|
127 | //averageUtility = (averageUtility * playsCount + util) / (playsCount + 1); //receiveMessage the average utility measure
|
---|
128 | playsCount ++;
|
---|
129 | }
|
---|
130 | else
|
---|
131 | {
|
---|
132 | SimpleEntry<Integer, SimpleEntry<Double, Double>> info = new SimpleEntry<Integer, SimpleEntry<Double, Double>>(0, new SimpleEntry<Double, Double>(util, 0.0));
|
---|
133 | agents.put(agent,info);
|
---|
134 | //System.out.println(agent + " --> " + util); //delete
|
---|
135 | }
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | public void SetAvgUtil (double avg, double stdev){
|
---|
140 | averageUtility = avg;
|
---|
141 | this.stdev = stdev;
|
---|
142 | }
|
---|
143 | }
|
---|