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