source: exampleparties/randomparty/randomparty/RandomParty.py@ 88

Last change on this file since 88 was 88, checked in by Bart Vastenhouw, 3 years ago

Added python SimpleRunner GUI

File size: 5.2 KB
Line 
1import logging
2from random import randint
3import traceback
4from typing import cast, Dict, List, Set, Collection
5
6from geniusweb.actions.Accept import Accept
7from geniusweb.actions.Action import Action
8from geniusweb.actions.LearningDone import LearningDone
9from geniusweb.actions.Offer import Offer
10from geniusweb.actions.PartyId import PartyId
11from geniusweb.actions.Vote import Vote
12from geniusweb.actions.Votes import Votes
13from geniusweb.bidspace.AllBidsList import AllBidsList
14from geniusweb.inform.ActionDone import ActionDone
15from geniusweb.inform.Finished import Finished
16from geniusweb.inform.Inform import Inform
17from geniusweb.inform.OptIn import OptIn
18from geniusweb.inform.Settings import Settings
19from geniusweb.inform.Voting import Voting
20from geniusweb.inform.YourTurn import YourTurn
21from geniusweb.issuevalue.Bid import Bid
22from geniusweb.issuevalue.Domain import Domain
23from geniusweb.issuevalue.Value import Value
24from geniusweb.issuevalue.ValueSet import ValueSet
25from geniusweb.party.Capabilities import Capabilities
26from geniusweb.party.DefaultParty import DefaultParty
27from geniusweb.profile.utilityspace.UtilitySpace import UtilitySpace
28from geniusweb.profileconnection.ProfileConnectionFactory import ProfileConnectionFactory
29from geniusweb.progress.ProgressRounds import ProgressRounds
30from geniusweb.utils import val
31
32
33class RandomParty (DefaultParty):
34 """
35 Offers random bids until a bid with sufficient utility is offered.
36 """
37 def __init__(self):
38 super().__init__()
39 self.getReporter().log(logging.INFO,"party is initialized")
40 self._profile = None
41 self._lastReceivedBid:Bid = None
42
43 # Override
44 def notifyChange(self, info: Inform):
45 #self.getReporter().log(logging.INFO,"received info:"+str(info))
46 if isinstance(info,Settings) :
47 self._settings:Settings=cast(Settings,info)
48 self._me = self._settings.getID()
49 self._protocol:str = str(self._settings.getProtocol().getURI())
50 self._progress = self._settings.getProgress()
51 if "Learn" == self._protocol:
52 self.getConnection().send(LearningDone(self._me)) #type:ignore
53 else:
54 self._profile = ProfileConnectionFactory.create(info.getProfile().getURI(), self.getReporter())
55 elif isinstance(info, ActionDone):
56 action:Action=cast( ActionDone,info).getAction()
57 if isinstance(action, Offer):
58 self._lastReceivedBid = cast(Offer, action).getBid()
59 elif isinstance(info, YourTurn):
60 self._myTurn()
61 if isinstance(self._progress, ProgressRounds) :
62 self._progress = self._progress.advance()
63 elif isinstance(info, Finished):
64 self.terminate()
65 elif isinstance(info, Voting):
66 # MOPAC protocol
67 self._lastvotes = self._vote(cast(Voting, info));
68 val(self.getConnection()).send(self._lastvotes)
69 elif isinstance(info, OptIn):
70 val(self.getConnection()).send(self._lastvotes)
71 else:
72 self.getReporter().log(logging.WARNING, "Ignoring unknown info "+str(info))
73
74
75 # Override
76 def getCapabilities(self) -> Capabilities:
77 return Capabilities( set([ "SAOP", "Learn", "MOPAC"]), set(['geniusweb.profile.utilityspace.LinearAdditive']))
78
79 # Override
80 def getDescription(self) -> str:
81 return "Offers random bids until a bid with sufficient utility is offered. Parameters minPower and maxPower can be used to control voting behaviour."
82
83 # Override
84 def terminate(self):
85 self.getReporter().log(logging.INFO,"party is terminating:")
86 super().terminate()
87 if self._profile != None:
88 self._profile.close()
89 self._profile = None
90
91
92 def _myTurn(self):
93 if self._isGood(self._lastReceivedBid):
94 action = Accept(self._me, self._lastReceivedBid)
95 else:
96 for _attempt in range(20):
97 bid = self._getRandomBid(self._profile.getProfile().getDomain())
98 if self._isGood(bid):
99 break
100 action = Offer(self._me, bid);
101 self.getConnection().send(action)
102
103 def _isGood(self, bid:Bid)->bool:
104 if bid == None:
105 return False
106 profile = self._profile.getProfile()
107 if isinstance(profile, UtilitySpace):
108 return profile.getUtility(bid) > 0.6
109 raise Exception("Can not handle this type of profile")
110
111 def _getRandomBid(self, domain:Domain) -> Bid:
112 allBids = AllBidsList(domain)
113 return allBids.get(randint(0, allBids.size()-1))
114
115 def _vote(self, voting:Voting) ->Votes :
116 '''
117 @param voting the {@link Voting} object containing the options
118
119 @return our next Votes.
120 '''
121 val = self._settings.getParameters().get("minPower");
122 minpower:int = val if isinstance(val, int) else 2
123 val = self._settings.getParameters().get("maxPower");
124 maxpower:int = val if isinstance(val,int) else 9999999;
125
126 votes:Set[Vote] = set([Vote(self._me, offer.getBid(), minpower, maxpower)\
127 for offer in voting.getOffers() if self._isGood(offer.getBid())])
128 return Votes(self._me, votes);
Note: See TracBrowser for help on using the repository browser.