[74] | 1 | from geniusweb.issuevalue.Bid import Bid
|
---|
| 2 | from geniusweb.issuevalue.Domain import Domain
|
---|
| 3 |
|
---|
| 4 | class Opponent:
|
---|
| 5 |
|
---|
| 6 | def __init__(self):
|
---|
| 7 | self._domain: Domain = None
|
---|
| 8 | self._freqDict: dict = {}
|
---|
| 9 | self._allBids = []
|
---|
| 10 | self._lastBid: Bid = None
|
---|
| 11 | self._firstBid: Bid = None
|
---|
| 12 | self._changed_issues = set()
|
---|
| 13 | # self._currentBid: Bid = None
|
---|
| 14 |
|
---|
| 15 | def init_domain(self, domain: Domain):
|
---|
| 16 | """
|
---|
| 17 | Initialize the domain.
|
---|
| 18 |
|
---|
| 19 | :param domain: The domain.
|
---|
| 20 | """
|
---|
| 21 | self._domain = domain
|
---|
| 22 | initValue = 1 / len(self._domain.getIssues())
|
---|
| 23 | for issue in domain.getIssues():
|
---|
| 24 | self._freqDict[issue] = initValue
|
---|
| 25 |
|
---|
| 26 | def log_bid(self, bid: Bid):
|
---|
| 27 | """
|
---|
| 28 | Main method of the opponent class
|
---|
| 29 | it handles logging the bids the opponent made
|
---|
| 30 | for learning purposes.
|
---|
| 31 |
|
---|
| 32 | :param bid: The bid that the agent received.
|
---|
| 33 | """
|
---|
| 34 | if self._firstBid is None:
|
---|
| 35 | self._firstBid = bid
|
---|
| 36 | self._update_freq_dict(bid, 0.1)
|
---|
| 37 | self._allBids.append(bid)
|
---|
| 38 | self._lastBid = bid
|
---|
| 39 |
|
---|
| 40 | def get_issue_weight(self, issue: str) -> float:
|
---|
| 41 | return self._freqDict[issue]
|
---|
| 42 |
|
---|
| 43 | def get_value_weight(self, issue: str, value: str) -> float:
|
---|
| 44 | return 1 / len(self._domain.getValues(issue))
|
---|
| 45 |
|
---|
| 46 | def get_utility(self, bid: Bid) -> float:
|
---|
| 47 | """
|
---|
| 48 | Given a bid return the predicted utility
|
---|
| 49 |
|
---|
| 50 | :param bid: The bid to calculate utility value on.
|
---|
| 51 | """
|
---|
| 52 | pass
|
---|
| 53 |
|
---|
| 54 | def _update_freq_dict(self, received_bid: Bid, step: float):
|
---|
| 55 | if self._lastBid is None or received_bid is None:
|
---|
| 56 | return
|
---|
| 57 |
|
---|
| 58 | for issue in received_bid.getIssues():
|
---|
| 59 | # Might fail if we receive partial bid.
|
---|
| 60 | if issue not in self._changed_issues and received_bid.getValue(issue) == self._firstBid.getValue(issue):
|
---|
| 61 | self._freqDict[issue] += step
|
---|
| 62 | else:
|
---|
| 63 | self._changed_issues.add(issue)
|
---|
| 64 |
|
---|
| 65 | print("=========")
|
---|
| 66 | print("Last bid: " + str(self._lastBid))
|
---|
| 67 | print("Curr bid: " + str(received_bid))
|
---|
| 68 | print("Before: " + str(self._freqDict))
|
---|
| 69 | self._freqDict = self.normalize(self._freqDict, 1)
|
---|
| 70 | print("After: " + str(self._freqDict))
|
---|
| 71 | print("=========")
|
---|
| 72 |
|
---|
| 73 | def normalize(self, d: dict, target=1.0):
|
---|
| 74 | raw = sum(d.values())
|
---|
| 75 | factor = target / raw
|
---|
| 76 | # d.items()
|
---|
| 77 | return {key: value * factor for key, value in d.items()}
|
---|
| 78 |
|
---|