source: src/main/java/agents/DecUtilAgent.java@ 346

Last change on this file since 346 was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 2.8 KB
Line 
1package agents;
2
3import java.util.ArrayList;
4import java.util.Collections;
5
6import genius.core.Agent;
7import genius.core.Bid;
8import genius.core.BidIterator;
9import genius.core.actions.Accept;
10import genius.core.actions.Action;
11import genius.core.actions.ActionWithBid;
12import genius.core.actions.Offer;
13
14/**
15 * This agent just offers all bids in decreasing utility order to opponent. The
16 * acceptance criterion is the same as with the SimpleAgent.
17 *
18 * @author W.Pasman
19 *
20 */
21public class DecUtilAgent extends Agent {
22 private Action actionOfPartner = null;
23 ArrayList<Bid> bids = new ArrayList<Bid>();
24 int nextBidIndex = 0; // what's the next bid from bids to be done.
25
26 @Override
27 public void init() {
28
29 BidIterator biter = new BidIterator(utilitySpace.getDomain());
30 while (biter.hasNext())
31 bids.add(biter.next());
32 Collections.sort(bids, new BidComparator(utilitySpace));
33 }
34
35 public void ReceiveMessage(Action opponentAction) {
36 actionOfPartner = opponentAction;
37 }
38
39 public Action chooseAction() {
40 Action action = null;
41 try {
42 if (actionOfPartner == null)
43 action = new Offer(getAgentID(), bids.get(nextBidIndex++));
44 if (actionOfPartner instanceof Offer) {
45 action = new Offer(getAgentID(), bids.get(nextBidIndex++));
46 }
47 // Thread.sleep(300); // 3 bids per second is good enough.
48 } catch (Exception e) {
49 System.out.println("Exception in ChooseAction:" + e.getMessage());
50 // best guess if things go wrong.
51 action = new Accept(getAgentID(),
52 ((ActionWithBid) actionOfPartner).getBid());
53 }
54 return action;
55 }
56
57 /**
58 * This function determines the accept probability for an offer. At t=0 it
59 * will prefer high-utility offers. As t gets closer to 1, it will accept
60 * lower utility offers with increasing probability. it will never accept
61 * offers with utility 0.
62 *
63 * @param u
64 * is the utility
65 * @param t
66 * is the time as fraction of the total available time (t=0 at
67 * start, and t=1 at end time)
68 * @return the probability of an accept at time t
69 * @throws Exception
70 * if you use wrong values for u or t.
71 *
72 */
73 double Paccept(double u, double t1) throws Exception {
74 double t = t1 * t1 * t1; // steeper increase when deadline approaches.
75 if (u < 0 || u > 1.05)
76 throw new Exception("utility " + u + " outside [0,1]");
77 // normalization may be slightly off, therefore we have a broad boundary
78 // up to 1.05
79 if (t < 0 || t > 1)
80 throw new Exception("time " + t + " outside [0,1]");
81 if (u > 1.)
82 u = 1;
83 if (t == 0.5)
84 return u;
85 return (u - 2. * u * t + 2. * (-1. + t + Math.sqrt(sq(-1. + t) + u
86 * (-1. + 2 * t))))
87 / (-1. + 2 * t);
88 }
89
90 double sq(double x) {
91 return x * x;
92 }
93}
Note: See TracBrowser for help on using the repository browser.