1 | package agents.anac.y2019.agentperaltav2;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Random;
|
---|
8 | import genius.core.Bid;
|
---|
9 | import genius.core.Domain;
|
---|
10 | import genius.core.actions.Accept;
|
---|
11 | import genius.core.actions.Action;
|
---|
12 | import genius.core.actions.Offer;
|
---|
13 | import genius.core.issue.Issue;
|
---|
14 | import genius.core.issue.Value;
|
---|
15 | import genius.core.parties.AbstractNegotiationParty;
|
---|
16 | import genius.core.uncertainty.AdditiveUtilitySpaceFactory;
|
---|
17 | import genius.core.uncertainty.BidRanking;
|
---|
18 | import genius.core.uncertainty.ExperimentalUserModel;
|
---|
19 | import genius.core.utility.AbstractUtilitySpace;
|
---|
20 |
|
---|
21 |
|
---|
22 | public class AgentPeraltaV2 extends AbstractNegotiationParty {
|
---|
23 |
|
---|
24 | public List<Issue> issues;
|
---|
25 | public ArrayList<Value> bidList = new ArrayList<Value>();
|
---|
26 | public ArrayList<Double> utilityBidList = new ArrayList<Double>();
|
---|
27 | public double sumOfutilitiesSquared = 0;
|
---|
28 | public double meanSquared;
|
---|
29 | public double mean;
|
---|
30 | public double emax;
|
---|
31 | public double target;
|
---|
32 | public double squaredSub = 0;
|
---|
33 | public double prob;
|
---|
34 | public double acceptProb;
|
---|
35 | public double pastMean = 0;
|
---|
36 | public double pastSigma = 0;
|
---|
37 | public int epsilon = 0;
|
---|
38 | public double firstBid;
|
---|
39 | public int counter = 0;
|
---|
40 | public Float startnegotime = (float) 0.9;
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public Action chooseAction(List<Class<? extends Action>> possibleActions) {
|
---|
44 | log("This is the UncertaintyAgentPeralta version 2.0");
|
---|
45 | //log("The user model is: " + userModel);
|
---|
46 | //log("The default estimated utility space is: "+getUtilitySpace());
|
---|
47 | List<Bid> bidOrder = userModel.getBidRanking().getBidOrder();
|
---|
48 | ConstraintsTable table = new ConstraintsTable();
|
---|
49 | try {
|
---|
50 | table.estimateSides(bidOrder);
|
---|
51 | } catch (Exception e) {
|
---|
52 | // TODO Auto-generated catch block
|
---|
53 | e.printStackTrace();
|
---|
54 | }
|
---|
55 | Bid randomBid = getUtilitySpace().getDomain().getRandomBid(rand);
|
---|
56 | Bid forthHighBid = bidOrder.get(bidOrder.size()-4);
|
---|
57 | Bid highBid= bidOrder.get(bidOrder.size()-1);
|
---|
58 | Bid secondHighBid = bidOrder.get(bidOrder.size()-2);
|
---|
59 | Bid thirdHighBid = bidOrder.get(bidOrder.size()-3);
|
---|
60 | Bid [] highestBids = new Bid[]{highBid,secondHighBid,thirdHighBid,forthHighBid};
|
---|
61 | Float time = (float) getTimeLine().getTime();
|
---|
62 | //log("The default estimate of the utility of a random bid + "+randomBid+ " is: " +getUtility(randomBid));
|
---|
63 |
|
---|
64 | /*if (userModel instanceof ExperimentalUserModel) {
|
---|
65 | log("You have given the agent access to the real utility space for debugging purposes.");
|
---|
66 | ExperimentalUserModel e = (ExperimentalUserModel) userModel;
|
---|
67 | AbstractUtilitySpace realUSpace = e.getRealUtilitySpace();
|
---|
68 | log("The real utility space is: " + realUSpace);
|
---|
69 | log("The real utility of the random bid is: "+ realUSpace.getUtility(randomBid));
|
---|
70 | }*/
|
---|
71 |
|
---|
72 |
|
---|
73 | if (getLastReceivedAction() instanceof Offer) {
|
---|
74 | Bid receivedBid = ((Offer) getLastReceivedAction()).getBid();
|
---|
75 | Bid firstOpponentOffer= null;
|
---|
76 |
|
---|
77 | if (receivedBid == firstOpponentOffer) {
|
---|
78 | counter++;
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (counter ==0) {
|
---|
83 | //this is the first offer.
|
---|
84 | //trying to see if this is his first offer.
|
---|
85 | firstOpponentOffer = receivedBid;
|
---|
86 | counter++;
|
---|
87 | }
|
---|
88 | issues = this.getDomain().getIssues();
|
---|
89 | HashMap <Integer, Value> valuelist = receivedBid.getValues();
|
---|
90 | Value[] opvaluearray = new Value[issues.size()];
|
---|
91 | int a = 1;
|
---|
92 | for (@SuppressWarnings("unused") Issue issue : issues) {
|
---|
93 | opvaluearray [a-1] = (Value) valuelist.get(a);
|
---|
94 | a++;
|
---|
95 | }
|
---|
96 | bidList.addAll(Arrays.asList(opvaluearray)); // ekxwrisi sthn top metavliti mas
|
---|
97 | acceptProb = generateOppoent( bidList.size(), table.estimateUtilityBid(receivedBid));
|
---|
98 | log("@@Utility of BID: "+table.estimateUtilityBid(receivedBid));
|
---|
99 | if (time < startnegotime){
|
---|
100 | if (acceptProb < 0.50 && counter > 80) {
|
---|
101 | try {
|
---|
102 | if(table.estimateUtilityBid(receivedBid) == firstBid) {
|
---|
103 | return new Offer (getPartyId(), generateRandomBid(highestBids));
|
---|
104 | }
|
---|
105 | return new Offer(getPartyId(), generateRandomBid(highestBids));
|
---|
106 | } catch (Exception e) {
|
---|
107 | e.printStackTrace();
|
---|
108 | }
|
---|
109 | }
|
---|
110 | else if(acceptProb< 0.50) {
|
---|
111 | try {
|
---|
112 | return new Offer(getPartyId(),randomBid);
|
---|
113 | } catch (Exception e) {
|
---|
114 | e.printStackTrace();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | }
|
---|
119 | else {
|
---|
120 | if (acceptProb > 0.65 && (table.estimateUtilityBid(receivedBid) != firstBid)) {
|
---|
121 | System.out.println(">>DEBUG Accept offer\n");
|
---|
122 | //this.utilitySpace.getUtility(lastReceivedOffer) > this.utilitySpace.getUtility(myLastOffer)
|
---|
123 | return new Accept(getPartyId(), receivedBid);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | }
|
---|
128 | return new Offer(getPartyId(), generateRandomBid(highestBids));
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 | public double generateOppoent (int numOfOffers, double lastOffer) {
|
---|
135 | utilityBidList.addAll(Arrays.asList(lastOffer));
|
---|
136 | int size = utilityBidList.size();
|
---|
137 | Float time = (float) getTimeLine().getTime();
|
---|
138 | double temp=0;
|
---|
139 | double sumOfUtilities = 0;
|
---|
140 | double sigma=0;
|
---|
141 | double delta=0;
|
---|
142 |
|
---|
143 | counter = searchForPareto(size,lastOffer);
|
---|
144 |
|
---|
145 | for (int i=0; i<size; i++) {
|
---|
146 | sumOfUtilities += utilityBidList.get(i);
|
---|
147 |
|
---|
148 | }
|
---|
149 | mean = sumOfUtilities / size;
|
---|
150 | for (int i=0; i<size; i++) {
|
---|
151 | temp = Math.pow(utilityBidList.get(i) - mean, 2);
|
---|
152 | sigma = sigma + temp;
|
---|
153 | }
|
---|
154 | sigma = sigma/size;
|
---|
155 | delta = Math.pow(sigma,0.5) * Math.pow(12, 0.5);
|
---|
156 |
|
---|
157 | epsilon= behavorOfOpponent(sigma,lastOffer);
|
---|
158 |
|
---|
159 | pastMean = mean;
|
---|
160 | pastSigma= sigma;
|
---|
161 |
|
---|
162 |
|
---|
163 | emax= mean + (1- mean) * delta;
|
---|
164 | target= 1 - (1-emax)*(Math.pow(time, epsilon));
|
---|
165 |
|
---|
166 | prob= (Math.pow(time, 5)/5) + (lastOffer-emax) + (lastOffer-target);
|
---|
167 | return prob;
|
---|
168 |
|
---|
169 | }
|
---|
170 |
|
---|
171 | public int behavorOfOpponent(double sigma, double lastoffer){
|
---|
172 | int i = 0;
|
---|
173 | int j = 0;
|
---|
174 |
|
---|
175 | if (lastoffer > mean) {
|
---|
176 | i = 0; //Uncooperative
|
---|
177 | }
|
---|
178 | else if (lastoffer < mean) {
|
---|
179 | i = 1; //cooperative
|
---|
180 | }
|
---|
181 | else
|
---|
182 | {
|
---|
183 | i = 0; //neutral
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (sigma > pastSigma) {
|
---|
187 | j = 0; // passive
|
---|
188 | }
|
---|
189 | else if( sigma < pastSigma) {
|
---|
190 | j = 1; // assertive
|
---|
191 | }
|
---|
192 | else {
|
---|
193 | j=0; //neutral
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | if (i==1 && j==1) {
|
---|
198 | return epsilon - 1;
|
---|
199 | }
|
---|
200 |
|
---|
201 | return epsilon;
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | private void log(String s) {
|
---|
206 | System.out.println(s);
|
---|
207 | }
|
---|
208 | /**
|
---|
209 | * With this method, you can override the default estimate of the utility
|
---|
210 | * space given uncertain preferences specified by the user model. This
|
---|
211 | * example sets every value to zero.
|
---|
212 | */
|
---|
213 | @Override
|
---|
214 | public AbstractUtilitySpace estimateUtilitySpace() {
|
---|
215 |
|
---|
216 | Domain domain = getDomain();
|
---|
217 | AdditiveUtilitySpaceFactory factory = new AdditiveUtilitySpaceFactory(domain);
|
---|
218 | BidRanking bidRanking = userModel.getBidRanking();
|
---|
219 | //List<Bid> bidOrder = userModel.getBidRanking().getBidOrder();
|
---|
220 | factory.estimateUsingBidRanks(bidRanking);
|
---|
221 |
|
---|
222 |
|
---|
223 | return factory.getUtilitySpace();
|
---|
224 | }
|
---|
225 |
|
---|
226 | public int searchForPareto (int size, double lastOffer) {
|
---|
227 | if (size == 1) {
|
---|
228 | firstBid = lastOffer; // his first bid and maybe his maximum bid of utility
|
---|
229 | return 1;
|
---|
230 | }
|
---|
231 | else {
|
---|
232 | if ( lastOffer == firstBid) {
|
---|
233 | counter++;
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | return counter;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | public Bid generateRandomBid(Bid [] highestBids ) {
|
---|
242 | Random rand = new Random();
|
---|
243 | ConstraintsTable table = new ConstraintsTable();
|
---|
244 | if (firstBid== table.estimateUtilityBid(highestBids[rand.nextInt(highestBids.length)])) {
|
---|
245 | return highestBids[rand.nextInt(highestBids.length)];
|
---|
246 | }
|
---|
247 | return highestBids[rand.nextInt(highestBids.length)];
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | @Override
|
---|
252 | public String getDescription() {
|
---|
253 | return "My Uncertain Agent PeraltaV2";
|
---|
254 | }
|
---|
255 |
|
---|
256 | }
|
---|