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

Last change on this file was 52, checked in by ruud, 19 months ago

Fixed small issues in domaineditor.

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