source: CSE3210/agent68/agent68.py

Last change on this file was 74, checked in by wouter, 21 months ago

#6 Added CSE3210 parties

File size: 7.0 KB
Line 
1import logging
2import time
3
4from typing import cast
5
6from geniusweb.actions.Accept import Accept
7from geniusweb.actions.Action import Action
8from geniusweb.actions.Offer import Offer
9from geniusweb.inform.ActionDone import ActionDone
10from geniusweb.inform.Finished import Finished
11from geniusweb.inform.Inform import Inform
12from geniusweb.inform.Settings import Settings
13from geniusweb.inform.YourTurn import YourTurn
14from geniusweb.issuevalue.Bid import Bid
15from geniusweb.party.Capabilities import Capabilities
16from geniusweb.party.DefaultParty import DefaultParty
17from geniusweb.profileconnection.ProfileConnectionFactory import (
18 ProfileConnectionFactory,
19)
20from geniusweb.progress.ProgressRounds import ProgressRounds
21from geniusweb.opponentmodel.FrequencyOpponentModel import FrequencyOpponentModel
22from tudelft_utilities_logging.Reporter import Reporter
23
24# from main.bidding.bidding import Bidding
25# from Group68_NegotiationAssignment_Agent.bidding.bidding import Bidding
26from .bidding.bidding import Bidding
27
28from time import time as clock
29
30
31class Agent68(DefaultParty):
32 """
33 Template agent that offers random bids until a bid with sufficient utility is offered.
34 """
35 PHASE_ONE_ROUNDS = (0, 10) # Reconnaissance
36 PHASE_TWO_ROUNDS = (11, 70) # Main negotiation
37
38 def __init__(self, reporter: Reporter = None):
39 super().__init__(reporter)
40 self._progress = None
41 self.getReporter().log(logging.INFO, "party is initialized")
42 self._profile = None
43 self._last_received_bid: Bid = None
44
45 # New attributes
46 self._opponent = FrequencyOpponentModel.create()
47 self._freqDict = {}
48 self._bidding: Bidding = Bidding()
49 self._selfCurrBid: Bid = None
50
51 self._e1 = 0.3
52 self._e2 = 0.3
53 self._e3 = 0.1
54
55 def notifyChange(self, info: Inform):
56 """This is the entry point of all interaction with your agent after is has been initialised.
57
58 Args:
59 info (Inform): Contains either a request for action or information.
60 """
61
62 # a Settings message is the first message that will be send to your
63 # agent containing all the information about the negotiation session.
64 if isinstance(info, Settings):
65 self._settings: Settings = cast(Settings, info)
66 self._me = self._settings.getID()
67
68 # progress towards the deadline has to be tracked manually through the use of the Progress object
69 self._progress: ProgressRounds = self._settings.getProgress()
70
71 # the profile contains the preferences of the agent over the domain
72 self._profile = ProfileConnectionFactory.create(
73 info.getProfile().getURI(), self.getReporter()
74 )
75
76 self._opponent = self._opponent.With(self._profile.getProfile().getDomain(), None)
77
78 self._getParams()
79 self._bidding.initBidding(cast(Settings, info), self.getReporter())
80
81 # ActionDone is an action send by an opponent (an offer or an accept)
82 elif isinstance(info, ActionDone):
83 action: Action = cast(ActionDone, info).getAction()
84
85 # if it is an offer, set the last received bid
86 if isinstance(action, Offer):
87
88 offer: Offer = cast(Offer, action)
89 # print(self._profile.getProfile().getReservationBid())
90 if offer.getActor() != self._settings.getID():
91 opp_bid = cast(Offer, action).getBid()
92 self._last_received_bid = opp_bid
93 self._opponent = self._opponent.WithAction(action, self._progress)
94 self._bidding.updateOpponentUtility(self._opponent.getUtility)
95 opponent_utility = self._opponent.getUtility(opp_bid)
96 # print("Estimated opponent utility: " + str(opponent_utility))
97 self._bidding.receivedBid(opp_bid)
98
99 # YourTurn notifies you that it is your turn to act
100 elif isinstance(info, YourTurn):
101 action = self._myTurn()
102 if isinstance(self._progress, ProgressRounds):
103 self._progress = self._progress.advance()
104 self.getConnection().send(action)
105
106 # Finished will be send if the negotiation has ended (through agreement or deadline)
107 elif isinstance(info, Finished):
108 # terminate the agent MUST BE CALLED
109 self.terminate()
110 else:
111 self.getReporter().log(
112 logging.WARNING, "Ignoring unknown info " + str(info)
113 )
114 self._updateRound(info)
115
116 # lets the geniusweb system know what settings this agent can handle
117 # leave it as it is for this competition
118 def getCapabilities(self) -> Capabilities:
119 return Capabilities(
120 set(["SAOP"]),
121 set(["geniusweb.profile.utilityspace.LinearAdditive"]),
122 )
123
124 # terminates the agent and its connections
125 # leave it as it is for this competition
126 def terminate(self):
127
128 super().terminate()
129 if self._profile is not None:
130 self._profile.close()
131 self._profile = None
132
133
134
135 def _getParams(self):
136 params = self._settings.getParameters()
137
138 self._e1 = params.getDouble("e1", 0.3, 0.0, 2.0)
139 self._e2 = params.getDouble("e2", 0.3, 0.0, 2.0)
140 self._e3 = params.getDouble("e3", 0.1, 0.0, 2.0)
141
142
143
144 def _updateRound(self, info: Inform):
145 """
146 Update {@link #progress}, depending on the protocol and last received
147 {@link Inform}
148
149 @param info the received info.
150 """
151 if self._settings == None: # not yet initialized
152 return
153
154 if not isinstance(info, YourTurn):
155 return
156
157 # if we get here, round must be increased.
158 if isinstance(self._progress, ProgressRounds):
159 self._progress = self._progress.advance()
160 self._bidding.updateProgress(self._progress)
161
162 # give a description of your agent
163 def getDescription(self) -> str:
164 return "Agent68"
165
166 # execute a turn
167 def _myTurn(self):
168 self._bidding._updateUtilSpace()
169 self._progress.get(round(clock() * 1000))
170 # check if the last received offer if the opponent is good enough
171 #TODO try changing params and integrating with acceptance strat. Try filtered combinations
172 if self._progress.get(round(clock() * 1000)) < 0.4:
173 self._bidding.setE(self._e1)
174 elif self._progress.get(round(clock() * 1000)) < 0.8:
175 self._bidding.setE(self._e2)
176 else:
177 self._bidding.setE(self._e3)
178 if self._bidding._isGood(self._last_received_bid):
179 # if so, accept the offer
180 action = Accept(self._me, self._last_received_bid)
181 else:
182 # if not, find a bid to propose as counter offer
183 bid = self._bidding.makeBid(self._opponent)
184 action = Offer(self._me, bid)
185 opponent_utility = self._opponent.getUtility(bid)
186
187 # send the actionp
188 return action
189
Note: See TracBrowser for help on using the repository browser.