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

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

Initial Release

File size: 3.2 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.utilityspace.LinearAdditiveUtilitySpace as LinearAdditiveUtilitySpace
26import genius2.references.ProfileRef as ProfileRef
27import genius2.references.ProtocolRef as ProtocolRef
28import geniusweb.profileconnection.ProfileConnectionFactory as ProfileConnectionFactory
29import geniusweb.profileconnection.ProfileInterface as ProfileInterface
30import genius2.progress.ProgressRounds as ProgressRounds
31
32import com.fasterxml.jackson.databind.ObjectMapper as ObjectMapper
33
34class RandomParty (DefaultParty):
35 """
36 A simple party that places random bids and accepts when it receives an offer
37 with sufficient utility.
38 """
39
40 jackson = ObjectMapper()
41 random = Random();
42
43 def __init__(self):
44 self.profile = None
45 self.lastReceivedBid = None
46
47 # Override
48 def notifyChange(self, info):
49 if isinstance(info, Settings) :
50 self.profile = ProfileConnectionFactory.create(info.getProfile().getURI(), self.getReporter());
51 self.me = info.getID()
52 self.progress = info.getProgress()
53 elif isinstance(info , ActionDone):
54 self.lastActor = info.getAction().getActor()
55 otheract = info.getAction()
56 if isinstance(otheract, Offer):
57 self.lastReceivedBid = otheract.getBid()
58 elif isinstance(info , YourTurn):
59 self._myTurn()
60 if isinstance(self.progress, ProgressRounds) :
61 self.progress = self.progress.advance();
62
63 # Override
64 def getCapabilities(self): # -> Capabilities
65 try:
66 return Capabilities(HashSet([ ProtocolRef(URI("SAOP"))]))
67 except:
68 getReporter().log(Level.SEVERE, "Failed to create capabilities URI",e);
69 return None
70
71 # Override
72 def getDescription(self):
73 return "places random bids until it can accept an offer with utility >0.6. Python version"
74
75 def _myTurn(self):
76 if self.lastReceivedBid != None and self.profile.getProfile().getUtility(self.lastReceivedBid).doubleValue() > 0.6:
77 action = Accept(self.me, self.lastReceivedBid)
78 else:
79 bidspace = AllPartialBidsList(self.profile.getProfile().getDomain())
80 bid = None
81 for attempt in range(20):
82 i = self.random.nextInt(bidspace.size()) # warning: jython implicitly converts BigInteger to long.
83 bid = bidspace.get(BigInteger.valueOf(i))
84 if self._isGood(bid):
85 break
86 action = Offer(self.me, bid);
87 try:
88 self.getConnection().send(action)
89 except:
90 print 'failed to send action '+action.toString()
91 traceback.print_exc()
92
93 def _isGood(self, bid):
94 return bid != None and self.profile.getProfile().getUtility(bid).doubleValue() > 0.6;
Note: See TracBrowser for help on using the repository browser.