1 | import time
|
---|
2 | import traceback
|
---|
3 |
|
---|
4 | import geniusweb.inform.ActionDone as ActionDone
|
---|
5 | import geniusweb.inform.Inform as Inform
|
---|
6 | import geniusweb.inform.Settings as Settings
|
---|
7 | import geniusweb.inform.YourTurn as YourTurn
|
---|
8 |
|
---|
9 |
|
---|
10 | import java.math.BigInteger as BigInteger
|
---|
11 | import java.net.URI as URI
|
---|
12 | import java.util.Arrays as Arrays
|
---|
13 | import java.util.HashSet as HashSet
|
---|
14 | import java.util.Random as Random
|
---|
15 |
|
---|
16 | import geniusweb.actions.Accept as Accept
|
---|
17 | import geniusweb.actions.Action as Action
|
---|
18 | import geniusweb.actions.Offer as Offer
|
---|
19 | import geniusweb.actions.PartyId as PartyId
|
---|
20 | import geniusweb.bidspace.AllPartialBidsList as AllPartialBidsList
|
---|
21 | import geniusweb.issuevalue.Bid as Bid
|
---|
22 | import geniusweb.party.Capabilities as Capabilities
|
---|
23 | import geniusweb.party.DefaultParty as DefaultParty
|
---|
24 | import geniusweb.profile.Profile as Profile
|
---|
25 | import geniusweb.profile.PartialOrdering as PartialOrdering
|
---|
26 | import geniusweb.profile.utilityspace.UtilitySpace as UtilitySpace
|
---|
27 | import geniusweb.references.ProfileRef as ProfileRef
|
---|
28 | import geniusweb.references.ProtocolRef as ProtocolRef
|
---|
29 | import geniusweb.profileconnection.ProfileConnectionFactory as ProfileConnectionFactory
|
---|
30 | import geniusweb.profileconnection.ProfileInterface as ProfileInterface
|
---|
31 | import geniusweb.progress.ProgressRounds as ProgressRounds
|
---|
32 |
|
---|
33 |
|
---|
34 | import com.fasterxml.jackson.databind.ObjectMapper as ObjectMapper
|
---|
35 |
|
---|
36 | class RandomParty (DefaultParty):
|
---|
37 | """
|
---|
38 | A simple party that places random bids and accepts when it receives an offer
|
---|
39 | with sufficient utility.
|
---|
40 | """
|
---|
41 |
|
---|
42 | jackson = ObjectMapper()
|
---|
43 | random = Random();
|
---|
44 |
|
---|
45 | def __init__(self):
|
---|
46 | self.profile = None
|
---|
47 | self.lastReceivedBid = None
|
---|
48 |
|
---|
49 | # Override
|
---|
50 | def notifyChange(self, info):
|
---|
51 | if isinstance(info, Settings) :
|
---|
52 | self.profile = ProfileConnectionFactory.create(info.getProfile().getURI(), self.getReporter());
|
---|
53 | self.me = info.getID()
|
---|
54 | self.progress = info.getProgress()
|
---|
55 | elif isinstance(info , ActionDone):
|
---|
56 | self.lastActor = info.getAction().getActor()
|
---|
57 | otheract = info.getAction()
|
---|
58 | if isinstance(otheract, Offer):
|
---|
59 | self.lastReceivedBid = otheract.getBid()
|
---|
60 | elif isinstance(info , YourTurn):
|
---|
61 | self._myTurn()
|
---|
62 | if isinstance(self.progress, ProgressRounds) :
|
---|
63 | self.progress = self.progress.advance();
|
---|
64 |
|
---|
65 | # Override
|
---|
66 | def getCapabilities(self): # -> Capabilities
|
---|
67 | return Capabilities(HashSet([ "SAOP"]))
|
---|
68 |
|
---|
69 | # Override
|
---|
70 | def getDescription(self):
|
---|
71 | return "places random bids until it can accept an offer with utility >0.6. Python version"
|
---|
72 |
|
---|
73 | def _myTurn(self):
|
---|
74 | if self.lastReceivedBid != None and self.profile.getProfile().getUtility(self.lastReceivedBid).doubleValue() > 0.6:
|
---|
75 | action = Accept(self.me, self.lastReceivedBid)
|
---|
76 | else:
|
---|
77 | bidspace = AllPartialBidsList(self.profile.getProfile().getDomain())
|
---|
78 | bid = None
|
---|
79 | for attempt in range(20):
|
---|
80 | i = self.random.nextInt(bidspace.size()) # warning: jython implicitly converts BigInteger to long.
|
---|
81 | bid = bidspace.get(BigInteger.valueOf(i))
|
---|
82 | if self._isGood(bid):
|
---|
83 | break
|
---|
84 | action = Offer(self.me, bid);
|
---|
85 | try:
|
---|
86 | self.getConnection().send(action)
|
---|
87 | except:
|
---|
88 | print 'failed to send action '+action.toString()
|
---|
89 | traceback.print_exc()
|
---|
90 |
|
---|
91 | def _isGood(self, bid):
|
---|
92 | if bid == None:
|
---|
93 | return false
|
---|
94 | profile = self.profile.getProfile()
|
---|
95 | if isinstance(profile, UtilitySpace):
|
---|
96 | return profile.getUtility(bid).doubleValue() > 0.6;
|
---|
97 | if isinstance(profile, PartialOrdering):
|
---|
98 | return profile.isPreferredOrEqual(bid, profile.getReservationBid())
|
---|
99 | raise Exception("Can not handle this type of profile")
|
---|