source: src/main/java/agents/TestingAgent.java@ 209

Last change on this file since 209 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: 5.7 KB
Line 
1package agents;
2
3import java.util.HashMap;
4import java.util.List;
5import java.util.Random;
6
7import genius.core.Agent;
8import genius.core.Bid;
9import genius.core.Domain;
10import genius.core.actions.Accept;
11import genius.core.actions.Action;
12import genius.core.actions.ActionWithBid;
13import genius.core.actions.EndNegotiation;
14import genius.core.actions.Offer;
15import genius.core.issue.Issue;
16import genius.core.issue.IssueDiscrete;
17import genius.core.issue.IssueInteger;
18import genius.core.issue.IssueReal;
19import genius.core.issue.Value;
20import genius.core.issue.ValueInteger;
21import genius.core.issue.ValueReal;
22
23/**
24 * @author W.Pasman Some improvements over the standard SimpleAgent.
25 */
26public class TestingAgent extends Agent {
27 private Action actionOfPartner = null;
28 private static double MINIMUM_BID_UTILITY = 0;
29
30 /**
31 * init is called when a next session starts with the same opponent.
32 */
33 public void init() {
34 Double reservationValue = utilitySpace.getReservationValue();
35 System.out.println(getName());
36 System.out.println();
37 System.out.println("Discount: " + utilitySpace.getDiscountFactor());
38 System.out.println("RV: " + reservationValue);
39 Domain domain = utilitySpace.getDomain();
40 System.out.println("NumberOfPossibleBids: "
41 + domain.getNumberOfPossibleBids());
42 Bid randomBid = domain.getRandomBid(null);
43 try {
44 System.out.println("Utitility of bid " + randomBid + " = "
45 + utilitySpace.getUtility(randomBid));
46 } catch (Exception e) {
47 e.printStackTrace();
48 }
49 }
50
51 public static void main(String[] args) {
52 double util = 0.24858884644383605;
53 double time = 0.21064712109444447;
54 double discount = 0.5;
55 double discountedUtil = util * Math.pow(discount, time);
56 System.out.println(discountedUtil);
57 }
58
59 @Override
60 public String getName() {
61 return "Testing Agent";
62 }
63
64 @Override
65 public String getVersion() {
66 return "1.3";
67 }
68
69 public void ReceiveMessage(Action opponentAction) {
70 actionOfPartner = opponentAction;
71 }
72
73 public Action chooseAction() {
74 Action action = null;
75 try {
76 if (actionOfPartner == null)
77 action = chooseRandomBidAction();
78 if (actionOfPartner instanceof Offer) {
79 // get current time
80 double time = timeline.getTime();
81 System.out.println("t = " + Math.round(100 * time) / 100.0
82 + ", discountedRV = "
83 + utilitySpace.getReservationValueWithDiscount(time));
84
85 if (time > 0.1) {
86 System.out.println("End, because t = " + time);
87 action = new EndNegotiation(getAgentID());
88 } else
89 action = chooseRandomBidAction();
90 }
91 } catch (Exception e) {
92 System.out.println("Exception in ChooseAction:" + e.getMessage());
93 // best guess if things go wrong
94 action = new Accept(getAgentID(),
95 ((ActionWithBid) actionOfPartner).getBid());
96 }
97 return action;
98 }
99
100 /**
101 * Wrapper for getRandomBid, for convenience.
102 *
103 * @return new Action(Bid(..)), with bid utility > MINIMUM_BID_UTIL. If a
104 * problem occurs, it returns an Accept() action.
105 */
106 private Action chooseRandomBidAction() {
107 Bid nextBid = null;
108 try {
109 nextBid = getRandomBid();
110 } catch (Exception e) {
111 System.out.println("Problem with received bid:" + e.getMessage()
112 + ". cancelling bidding");
113 }
114 if (nextBid == null)
115 return (new Accept(getAgentID(),
116 ((ActionWithBid) actionOfPartner).getBid()));
117 return (new Offer(getAgentID(), nextBid));
118 }
119
120 /**
121 * @return a random bid with high enough utility value.
122 * @throws Exception
123 * if we can't compute the utility (eg no evaluators have been
124 * set) or when other evaluators than a DiscreteEvaluator are
125 * present in the util space.
126 */
127 private Bid getRandomBid() throws Exception {
128 HashMap<Integer, Value> values = new HashMap<Integer, Value>(); // pairs
129 // <issuenumber,chosen
130 // value
131 // string>
132 List<Issue> issues = utilitySpace.getDomain().getIssues();
133 Random randomnr = new Random();
134
135 // createFrom a random bid with utility>MINIMUM_BID_UTIL.
136 // note that this may never succeed if you set MINIMUM too high!!!
137 // in that case we will search for a bid till the time is up (2 minutes)
138 // but this is just a simple agent.
139 Bid bid = null;
140 double utility;
141 do {
142 for (Issue lIssue : issues) {
143 switch (lIssue.getType()) {
144 case DISCRETE:
145 IssueDiscrete lIssueDiscrete = (IssueDiscrete) lIssue;
146 int optionIndex = randomnr.nextInt(lIssueDiscrete
147 .getNumberOfValues());
148 values.put(lIssue.getNumber(),
149 lIssueDiscrete.getValue(optionIndex));
150 break;
151 case REAL:
152 IssueReal lIssueReal = (IssueReal) lIssue;
153 int optionInd = randomnr.nextInt(lIssueReal
154 .getNumberOfDiscretizationSteps() - 1);
155 values.put(
156 lIssueReal.getNumber(),
157 new ValueReal(lIssueReal.getLowerBound()
158 + (lIssueReal.getUpperBound() - lIssueReal
159 .getLowerBound())
160 * (double) (optionInd)
161 / (double) (lIssueReal
162 .getNumberOfDiscretizationSteps())));
163 break;
164 case INTEGER:
165 IssueInteger lIssueInteger = (IssueInteger) lIssue;
166 int optionIndex2 = lIssueInteger.getLowerBound()
167 + randomnr.nextInt(lIssueInteger.getUpperBound()
168 - lIssueInteger.getLowerBound());
169 values.put(lIssueInteger.getNumber(), new ValueInteger(
170 optionIndex2));
171 break;
172 default:
173 throw new Exception("issue type " + lIssue.getType()
174 + " not supported by SimpleAgent2");
175 }
176 }
177 bid = new Bid(utilitySpace.getDomain(), values);
178 utility = getUtility(bid);
179 } while (utility < MINIMUM_BID_UTILITY);
180
181 // System.out.println(this.getName() + " sent " + bid);
182 return bid;
183 }
184}
Note: See TracBrowser for help on using the repository browser.