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

Last change on this file since 9 was 9, checked in by bart, 5 years ago

Release 1.1.0

File size: 3.5 KB
Line 
1import time
2import traceback
3
4import geniusweb.party.inform.ActionDone as ActionDone
5import geniusweb.party.inform.Inform as Inform
6import geniusweb.party.inform.Settings as Settings
7import geniusweb.party.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 try:
68 return Capabilities(HashSet([ ProtocolRef(URI("SAOP"))]))
69 except:
70 getReporter().log(Level.SEVERE, "Failed to create capabilities URI",e);
71 return None
72
73 # Override
74 def getDescription(self):
75 return "places random bids until it can accept an offer with utility >0.6. Python version"
76
77 def _myTurn(self):
78 if self.lastReceivedBid != None and self.profile.getProfile().getUtility(self.lastReceivedBid).doubleValue() > 0.6:
79 action = Accept(self.me, self.lastReceivedBid)
80 else:
81 bidspace = AllPartialBidsList(self.profile.getProfile().getDomain())
82 bid = None
83 for attempt in range(20):
84 i = self.random.nextInt(bidspace.size()) # warning: jython implicitly converts BigInteger to long.
85 bid = bidspace.get(BigInteger.valueOf(i))
86 if self._isGood(bid):
87 break
88 action = Offer(self.me, bid);
89 try:
90 self.getConnection().send(action)
91 except:
92 print 'failed to send action '+action.toString()
93 traceback.print_exc()
94
95 def _isGood(self, bid):
96 if bid == None:
97 return false
98 profile = self.profile.getProfile()
99 if isinstance(profile, UtilitySpace):
100 return profile.getUtility(bid).doubleValue() > 0.6;
101 if isinstance(profile, PartialOrdering):
102 return profile.isPreferredOrEqual(bid, profile.getReservationBid())
103 raise Exception("Can not handle this type of profile")
Note: See TracBrowser for help on using the repository browser.