[28] | 1 | import time
|
---|
| 2 | import traceback
|
---|
| 3 |
|
---|
| 4 | import geniusweb.party.inform.ActionDone as ActionDone
|
---|
| 5 | import geniusweb.party.inform.Inform as Inform
|
---|
| 6 | import geniusweb.party.inform.Settings as Settings
|
---|
| 7 | import geniusweb.party.inform.YourTurn as YourTurn
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | import java.math.BigInteger as BigInteger
|
---|
| 11 | import java.net.URI as URI
|
---|
| 12 | import java.util.Arrays as Arrays
|
---|
| 13 | import java.util.HashSet as HashSet
|
---|
| 14 | import java.util.Random as Random
|
---|
| 15 |
|
---|
| 16 | import geniusweb.actions.Accept as Accept
|
---|
| 17 | import geniusweb.actions.Action as Action
|
---|
| 18 | import geniusweb.actions.Offer as Offer
|
---|
| 19 | import geniusweb.actions.PartyId as PartyId
|
---|
| 20 | import geniusweb.bidspace.AllPartialBidsList as AllPartialBidsList
|
---|
| 21 | import geniusweb.issuevalue.Bid as Bid
|
---|
| 22 | import geniusweb.party.Capabilities as Capabilities
|
---|
| 23 | import geniusweb.party.DefaultParty as DefaultParty
|
---|
| 24 | import geniusweb.profile.Profile as Profile
|
---|
| 25 | import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace as LinearAdditiveUtilitySpace
|
---|
| 26 | import genius2.references.ProfileRef as ProfileRef
|
---|
| 27 | import genius2.references.ProtocolRef as ProtocolRef
|
---|
| 28 | import geniusweb.profileconnection.ProfileConnectionFactory as ProfileConnectionFactory
|
---|
| 29 | import geniusweb.profileconnection.ProfileInterface as ProfileInterface
|
---|
| 30 | import genius2.progress.ProgressRounds as ProgressRounds
|
---|
| 31 |
|
---|
| 32 | import com.fasterxml.jackson.databind.ObjectMapper as ObjectMapper
|
---|
| 33 |
|
---|
| 34 | class 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;
|
---|