1 | import logging
|
---|
2 | from random import randint
|
---|
3 | import traceback
|
---|
4 | from typing import cast, Dict, List
|
---|
5 |
|
---|
6 | from geniusweb.actions.Accept import Accept
|
---|
7 | from geniusweb.actions.Action import Action
|
---|
8 | from geniusweb.actions.LearningDone import LearningDone
|
---|
9 | from geniusweb.actions.Offer import Offer
|
---|
10 | from geniusweb.actions.PartyId import PartyId
|
---|
11 | from geniusweb.inform.ActionDone import ActionDone
|
---|
12 | from geniusweb.inform.Finished import Finished
|
---|
13 | from geniusweb.inform.Inform import Inform
|
---|
14 | from geniusweb.inform.Settings import Settings
|
---|
15 | from geniusweb.inform.YourTurn import YourTurn
|
---|
16 | from geniusweb.issuevalue.Bid import Bid
|
---|
17 | from geniusweb.issuevalue.Domain import Domain
|
---|
18 | from geniusweb.issuevalue.Value import Value
|
---|
19 | from geniusweb.issuevalue.ValueSet import ValueSet
|
---|
20 | from geniusweb.party.Capabilities import Capabilities
|
---|
21 | from geniusweb.party.DefaultParty import DefaultParty
|
---|
22 | from geniusweb.profile.utilityspace.UtilitySpace import UtilitySpace
|
---|
23 | from geniusweb.profileconnection.ProfileConnectionFactory import ProfileConnectionFactory
|
---|
24 | from geniusweb.progress.ProgressRounds import ProgressRounds
|
---|
25 |
|
---|
26 |
|
---|
27 | class RandomParty (DefaultParty):
|
---|
28 | """
|
---|
29 | Offers random bids until a bid with sufficient utility is offered.
|
---|
30 | """
|
---|
31 | def __init__(self):
|
---|
32 | super().__init__()
|
---|
33 | self.getReporter().log(logging.INFO,"party is initialized")
|
---|
34 | self._profile = None
|
---|
35 | self._lastReceivedBid:Bid = None
|
---|
36 |
|
---|
37 | # Override
|
---|
38 | def notifyChange(self, info: Inform):
|
---|
39 | #self.getReporter().log(logging.INFO,"received info:"+str(info))
|
---|
40 | if isinstance(info,Settings) :
|
---|
41 | settings:Settings=cast(Settings,info)
|
---|
42 | self._me = settings.getID()
|
---|
43 | self._protocol:str = str(settings.getProtocol().getURI())
|
---|
44 | self._progress = settings.getProgress()
|
---|
45 | if "Learn" == self._protocol:
|
---|
46 | self.getConnection().send(LearningDone(self._me))
|
---|
47 | else:
|
---|
48 | self._profile = ProfileConnectionFactory.create(info.getProfile().getURI(), self.getReporter())
|
---|
49 | elif isinstance(info, ActionDone):
|
---|
50 | action:Action=cast( ActionDone,info).getAction()
|
---|
51 | if isinstance(action, Offer):
|
---|
52 | self._lastReceivedBid = cast(Offer, action).getBid()
|
---|
53 | elif isinstance(info, YourTurn):
|
---|
54 | self._myTurn()
|
---|
55 | if isinstance(self._progress, ProgressRounds) :
|
---|
56 | self._progress = self._progress.advance()
|
---|
57 | elif isinstance(info, Finished):
|
---|
58 | self.terminate()
|
---|
59 | else:
|
---|
60 | self.getReporter().log(logging.WARNING, "Ignoring unknown info "+str(info))
|
---|
61 |
|
---|
62 |
|
---|
63 | # Override
|
---|
64 | def getCapabilities(self) -> Capabilities:
|
---|
65 | return Capabilities( set([ "SAOP", "Learn"]), set(['geniusweb.profile.utilityspace.LinearAdditive']))
|
---|
66 |
|
---|
67 | # Override
|
---|
68 | def getDescription(self) -> str:
|
---|
69 | return "Offers random bids until a bid with sufficient utility is offered"
|
---|
70 |
|
---|
71 | # Override
|
---|
72 | def terminate(self):
|
---|
73 | self.getReporter().log(logging.INFO,"party is terminating:")
|
---|
74 | super().terminate()
|
---|
75 | if self._profile != None:
|
---|
76 | self._profile.close()
|
---|
77 | self._profile = None
|
---|
78 |
|
---|
79 |
|
---|
80 | def _myTurn(self):
|
---|
81 | if self._lastReceivedBid != None and \
|
---|
82 | self._profile.getProfile().getUtility(self._lastReceivedBid) > 0.6:
|
---|
83 | action = Accept(self._me, self._lastReceivedBid)
|
---|
84 | else:
|
---|
85 | for _attempt in range(20):
|
---|
86 | bid = self._getRandomBid(self._profile.getProfile().getDomain())
|
---|
87 | if self._isGood(bid):
|
---|
88 | break
|
---|
89 | action = Offer(self._me, bid);
|
---|
90 | self.getConnection().send(action)
|
---|
91 |
|
---|
92 | def _isGood(self, bid:Bid)->bool:
|
---|
93 | if bid == None:
|
---|
94 | return False
|
---|
95 | profile = self._profile.getProfile()
|
---|
96 | if isinstance(profile, UtilitySpace):
|
---|
97 | return profile.getUtility(bid) > 0.6
|
---|
98 | raise Exception("Can not handle this type of profile")
|
---|
99 |
|
---|
100 | def _getRandomBid(self, domain:Domain) -> Bid:
|
---|
101 | values:Dict[str, Value]={iss: self._randValue(domain.getValues(iss)) \
|
---|
102 | for iss in domain.getIssues()}
|
---|
103 | return Bid(values)
|
---|
104 |
|
---|
105 | def _randValue(self, vals:ValueSet) -> Value:
|
---|
106 | values= list(vals)
|
---|
107 | return values[randint(0, len(values)-1)] |
---|