source: exampleparties/randompartypy/src/main/resources/RandomParty.py@ 24

Last change on this file since 24 was 21, checked in by bart, 4 years ago

Version 1.5.

File size: 3.4 KB
Line 
1import time
2import traceback
3
4import geniusweb.inform.ActionDone as ActionDone
5import geniusweb.inform.Inform as Inform
6import geniusweb.inform.Settings as Settings
7import geniusweb.inform.YourTurn as YourTurn
8
9
10import java.math.BigInteger as BigInteger
11import java.net.URI as URI
12import java.util.Arrays as Arrays
13import java.util.HashSet as HashSet
14import java.util.Random as Random
15
16import geniusweb.actions.Accept as Accept
17import geniusweb.actions.Action as Action
18import geniusweb.actions.Offer as Offer
19import geniusweb.actions.PartyId as PartyId
20import geniusweb.bidspace.AllPartialBidsList as AllPartialBidsList
21import geniusweb.issuevalue.Bid as Bid
22import geniusweb.party.Capabilities as Capabilities
23import geniusweb.party.DefaultParty as DefaultParty
24import geniusweb.profile.Profile as Profile
25import geniusweb.profile.PartialOrdering as PartialOrdering
26import geniusweb.profile.utilityspace.UtilitySpace as UtilitySpace
27import geniusweb.references.ProfileRef as ProfileRef
28import geniusweb.references.ProtocolRef as ProtocolRef
29import geniusweb.profileconnection.ProfileConnectionFactory as ProfileConnectionFactory
30import geniusweb.profileconnection.ProfileInterface as ProfileInterface
31import geniusweb.progress.ProgressRounds as ProgressRounds
32
33
34import com.fasterxml.jackson.databind.ObjectMapper as ObjectMapper
35
36class 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")
Note: See TracBrowser for help on using the repository browser.