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

Last change on this file since 30 was 30, checked in by bart, 3 years ago

Fixed memory leak. MOPAC2. removed jcenter build dependencies

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