1 | import logging
|
---|
2 | import time
|
---|
3 | from typing import cast
|
---|
4 |
|
---|
5 | from geniusweb.actions.Accept import Accept
|
---|
6 | from geniusweb.actions.Action import Action
|
---|
7 | from geniusweb.actions.Offer import Offer
|
---|
8 | from geniusweb.inform.ActionDone import ActionDone
|
---|
9 | from geniusweb.inform.Finished import Finished
|
---|
10 | from geniusweb.inform.Inform import Inform
|
---|
11 | from geniusweb.inform.Settings import Settings
|
---|
12 | from geniusweb.inform.YourTurn import YourTurn
|
---|
13 | from geniusweb.issuevalue.Bid import Bid
|
---|
14 | from geniusweb.party.Capabilities import Capabilities
|
---|
15 | from geniusweb.party.DefaultParty import DefaultParty
|
---|
16 | from geniusweb.progress.Progress import Progress
|
---|
17 |
|
---|
18 | from .acceptance_strategy import CombiAcceptanceStrategy, BetterThanEstimated
|
---|
19 | from .utility import AgentUtility
|
---|
20 | from .bidding_strategy import BiddingStrategyProbalistic, BiddingStrategyDeterministic, \
|
---|
21 | AgressiveBiddingStrategy
|
---|
22 | from geniusweb.profileconnection.ProfileConnectionFactory import (
|
---|
23 | ProfileConnectionFactory,
|
---|
24 | )
|
---|
25 | from geniusweb.progress.ProgressRounds import ProgressRounds
|
---|
26 | from tudelft_utilities_logging.Reporter import Reporter
|
---|
27 |
|
---|
28 |
|
---|
29 | class TemplateAgent(DefaultParty):
|
---|
30 | """
|
---|
31 | Template agent that offers random bids until a bid with sufficient utility is offered.
|
---|
32 | """
|
---|
33 |
|
---|
34 | def __init__(self, reporter: Reporter = None):
|
---|
35 | super().__init__(reporter)
|
---|
36 | self.getReporter().log(logging.INFO, "party is initialized")
|
---|
37 | self._profile = None
|
---|
38 |
|
---|
39 | self._utility = AgentUtility()
|
---|
40 | self._strategy = AgressiveBiddingStrategy(utility=self._utility)
|
---|
41 | self._acceptance = CombiAcceptanceStrategy(utility=self._utility)
|
---|
42 |
|
---|
43 | self._last_received_bid = None
|
---|
44 |
|
---|
45 | def notifyChange(self, info: Inform):
|
---|
46 | """This is the entry point of all interaction with your agent after is has been initialised.
|
---|
47 |
|
---|
48 | Args:
|
---|
49 | info (Inform): Contains either a request for action or information.
|
---|
50 | """
|
---|
51 |
|
---|
52 | # a Settings message is the first message that will be send to your
|
---|
53 | # agent containing all the information about the negotiation session.
|
---|
54 | if isinstance(info, Settings):
|
---|
55 | self._settings: Settings = cast(Settings, info)
|
---|
56 | self._me = self._settings.getID()
|
---|
57 |
|
---|
58 | # progress towards the deadline has to be tracked manually through the use of the Progress object
|
---|
59 | self._progress: Progress = self._settings.getProgress()
|
---|
60 |
|
---|
61 | # the profile contains the preferences of the agent over the domain
|
---|
62 | self._profile = ProfileConnectionFactory.create(
|
---|
63 | info.getProfile().getURI(), self.getReporter()
|
---|
64 | )
|
---|
65 |
|
---|
66 | self._strategy.set_profile(self._profile)
|
---|
67 | self._utility.set_profile(self._profile)
|
---|
68 | self._acceptance.set_profile(self._profile)
|
---|
69 |
|
---|
70 | self._acceptance.set_progress(self._progress)
|
---|
71 | self._utility.set_progress(self._progress)
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 |
|
---|
76 | # ActionDone is an action send by an opponent (an offer or an accept)
|
---|
77 | elif isinstance(info, ActionDone):
|
---|
78 | action: Action = cast(ActionDone, info).getAction()
|
---|
79 |
|
---|
80 | # if it is an offer, set the last received bid
|
---|
81 | if isinstance(action, Offer):
|
---|
82 | self._last_received_bid = cast(Offer, action).getBid()
|
---|
83 | # YourTurn notifies you that it is your turn to act
|
---|
84 | elif isinstance(info, YourTurn):
|
---|
85 | action = self._myTurn()
|
---|
86 | if isinstance(self._progress, ProgressRounds):
|
---|
87 | self._progress = self._progress.advance()
|
---|
88 | self._utility.set_progress(self._progress)
|
---|
89 | self._acceptance.set_progress(self._progress)
|
---|
90 | self.getConnection().send(action)
|
---|
91 |
|
---|
92 | # Finished will be send if the negotiation has ended (through agreement or deadline)
|
---|
93 | elif isinstance(info, Finished):
|
---|
94 | # terminate the agent MUST BE CALLED
|
---|
95 | self.terminate()
|
---|
96 | else:
|
---|
97 | self.getReporter().log(
|
---|
98 | logging.WARNING, "Ignoring unknown info " + str(info)
|
---|
99 | )
|
---|
100 |
|
---|
101 | # lets the geniusweb system know what settings this agent can handle
|
---|
102 | # leave it as it is for this competition
|
---|
103 | def getCapabilities(self) -> Capabilities:
|
---|
104 | return Capabilities(
|
---|
105 | {"SAOP"},
|
---|
106 | {"geniusweb.profile.utilityspace.LinearAdditive"},
|
---|
107 | )
|
---|
108 |
|
---|
109 | # terminates the agent and its connections
|
---|
110 | # leave it as it is for this competition
|
---|
111 | def terminate(self):
|
---|
112 | self.getReporter().log(logging.INFO, "party is terminating:")
|
---|
113 | super().terminate()
|
---|
114 | if self._profile is not None:
|
---|
115 | self._profile.close()
|
---|
116 | self._profile = None
|
---|
117 |
|
---|
118 |
|
---|
119 |
|
---|
120 | # give a description of your agent
|
---|
121 | def getDescription(self) -> str:
|
---|
122 | return "Agent33"
|
---|
123 |
|
---|
124 | # execute a turn
|
---|
125 | def _myTurn(self):
|
---|
126 | if self._last_received_bid is not None:
|
---|
127 | # add opponent bid to bid history
|
---|
128 | self._utility.append_to_bid_history(self._last_received_bid, False)
|
---|
129 | self._utility.update_opponent_weight_heuristic(self._last_received_bid)
|
---|
130 | self._utility.update_opponent_issue_count(self._last_received_bid)
|
---|
131 |
|
---|
132 | # check if the last received offer if the opponent is good enough
|
---|
133 | if self._isGood(self._last_received_bid):
|
---|
134 | # if so, accept the offer
|
---|
135 | action = Accept(self._me, self._last_received_bid)
|
---|
136 | else:
|
---|
137 | # if not, find a bid to propose as counter offer
|
---|
138 | bid = self._findBid()
|
---|
139 | action = Offer(self._me, bid)
|
---|
140 | # add bid to our bid history
|
---|
141 | self._utility.append_to_bid_history(bid, True)
|
---|
142 |
|
---|
143 | # send the action
|
---|
144 | return action
|
---|
145 |
|
---|
146 | def _isGood(self,
|
---|
147 | bid: Bid) -> bool: # adjust acceptance criteria based on how many turns left, accept if last bid is better than our next bid
|
---|
148 | if bid is None:
|
---|
149 | return False
|
---|
150 | return self._acceptance.accept(bid)
|
---|
151 |
|
---|
152 | def _findBid(self) -> Bid:
|
---|
153 | return self._strategy.get_bid()
|
---|
154 |
|
---|
155 |
|
---|
156 | class DeterministicAgent(TemplateAgent):
|
---|
157 |
|
---|
158 | def __init__(self):
|
---|
159 | super(DeterministicAgent, self).__init__()
|
---|
160 | self._strategy = BiddingStrategyDeterministic(utility=self._utility)
|
---|
161 |
|
---|
162 |
|
---|
163 | class ProbabilisticAgent(TemplateAgent):
|
---|
164 |
|
---|
165 | def __init__(self):
|
---|
166 | super(ProbabilisticAgent, self).__init__()
|
---|
167 | self._strategy = BiddingStrategyProbalistic(utility=self._utility)
|
---|
168 |
|
---|
169 |
|
---|
170 | class AgressiveAtStartAgent(TemplateAgent):
|
---|
171 |
|
---|
172 | def __init__(self, reporter: Reporter = None):
|
---|
173 | super().__init__(reporter)
|
---|
174 | self._strategy = AgressiveBiddingStrategy(utility=self._utility)
|
---|
175 | self._acceptance = CombiAcceptanceStrategy(utility=self._utility)
|
---|
176 | self.ratings = 0
|
---|
177 | self.switched = False
|
---|
178 |
|
---|
179 | def _myTurn(self):
|
---|
180 | self._updateStrategy()
|
---|
181 | return super(AgressiveAtStartAgent, self)._myTurn()
|
---|
182 |
|
---|
183 |
|
---|
184 | def _updateStrategy(self):
|
---|
185 | if not self.switched:
|
---|
186 | opponent_issue_percentage = self._utility.get_opponent_issue_count()
|
---|
187 | opponent_issue_weights = self._utility.get_weight_heuristic()
|
---|
188 | if self._last_received_bid is not None:
|
---|
189 | rating = self._utility.rate_bid(self._last_received_bid, opponent_issue_percentage, opponent_issue_weights)
|
---|
190 | if rating > 0.9:
|
---|
191 | self.ratings = 0
|
---|
192 | else:
|
---|
193 | self.ratings += 1
|
---|
194 |
|
---|
195 | if self.ratings == 3 and self._progress.get(time.time() * 1000) > 0.25:
|
---|
196 | self._strategy = BiddingStrategyDeterministic(utility=self._utility, profile=self._profile)
|
---|
197 | self.switched = True
|
---|
198 | else:
|
---|
199 | opponent_issue_percentage = self._utility.get_opponent_issue_count()
|
---|
200 | opponent_issue_weights = self._utility.get_weight_heuristic()
|
---|
201 | if self._last_received_bid is not None:
|
---|
202 | rating = self._utility.rate_bid(self._last_received_bid, opponent_issue_percentage, opponent_issue_weights)
|
---|
203 | if rating > 0.9:
|
---|
204 | self.ratings += 1
|
---|
205 | else:
|
---|
206 | self.ratings = 0
|
---|
207 |
|
---|
208 | if self.ratings == 3:
|
---|
209 | self._strategy = BiddingStrategyDeterministic(utility=self._utility, profile=self._profile)
|
---|
210 |
|
---|
211 | class AgressiveAtStartWithOpponentAcceptance(AgressiveAtStartAgent):
|
---|
212 |
|
---|
213 | def __init__(self):
|
---|
214 | super(AgressiveAtStartWithOpponentAcceptance, self).__init__()
|
---|
215 | self._acceptance = BetterThanEstimated(fall_off_util=1, fall_off_difference=2, utility=self._utility)
|
---|
216 |
|
---|
217 |
|
---|
218 | class Agent33(AgressiveAtStartAgent):
|
---|
219 |
|
---|
220 | def __init__(self, reporter: Reporter = None):
|
---|
221 | super().__init__(reporter)
|
---|
222 | self._strategy = AgressiveBiddingStrategy(utility=self._utility)
|
---|
223 | self._acceptance = BetterThanEstimated(fall_off_util=1, fall_off_difference=2, utility=self._utility)
|
---|
224 |
|
---|
225 |
|
---|
226 | def _findBid(self) -> Bid:
|
---|
227 | bid = self._strategy.get_bid()
|
---|
228 | if not self._isGood(bid):
|
---|
229 | self._strategy = BiddingStrategyProbalistic(utility=self._utility, profile=self._profile)
|
---|
230 | return self._findBid()
|
---|
231 | return bid |
---|