-
Notifications
You must be signed in to change notification settings - Fork 1
/
bidding.py
140 lines (95 loc) · 3.74 KB
/
bidding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import numpy as np
import random
from helpers import cartesian
from genius_template_importer import import_template
from helpers import is_pareto_efficient
class UtilitySpace():
def __init__(self, template, resolution=0.05):
self.utility_space = generate_bids_agent(
template['objectives'], resolution)
self.discount = template['discount']
def get_offers(self, minut=0, maxut=1):
# Filtro por los valores de min/max y se coge random
NotImplementedError
def get_random(self, minut=0, maxut=1):
print('Limits', minut, maxut)
lower = np.ma.masked_less_equal(self.utility_space[:, -1], maxut).mask
upper = np.ma.masked_greater_equal(
self.utility_space[:, -1], minut).mask
# (all_mask = [i for i, x in enumerate(range(len(lower))) if (lower[i] and upper[i])]
mask = (lower == True) & (upper == True)
valids = np.where(mask == True)[0]
print('valids.size', valids.size)
if valids.size == 0:
return None
index = random.choice(np.arange(valids.size))
return Bid(self.utility_space[valids[index]], self.discount, valids[index])
def get_by_index(self, index):
return Bid(self.utility_space[index], self.discount, index)
def get_utility(self):
return self.utility_space[:-1]
class Bid():
INSTANCES = 1
def __init__(self, values, discount=1, us_index=None, domain=None):
self.domain = domain
self.values = values
self.discount = discount
self.index = us_index
self.id = Bid.INSTANCES
Bid.INSTANCES += 1
def get_value(self, index):
return self.values[index]
def get_discrete_issue_function(issue):
names = []
values = []
for i, item in enumerate(issue['items']):
names.append(i)
values.append(item['evaluation'])
names = np.array(names)
values = np.array(values)
values = values / values.max()
return names, values
def get_real_issue_function(issue, resolution):
upperbound = float(issue['upperbound'])
lowerbound = float(issue['lowerbound'])
constant = float(issue['constant'])
slope = float(issue['slope'])
step = (upperbound - lowerbound)*resolution
steps = np.arange(lowerbound, upperbound+step, step)
values = steps * slope + constant
return steps, values
def get_issue_function(issue, resolution):
values = None
steps = None
weight = issue['weight']
if issue['type'].lower() == 'discrete':
steps, values = get_discrete_issue_function(issue)
elif issue['type'].lower() == 'real':
steps, values = get_real_issue_function(issue, resolution)
else:
raise NotImplemented('Issue function not implemented')
return steps, values * weight
def get_utility_function(template, resolution):
asteps = []
avalues = []
for objective in template:
for issue in objective:
steps, values = get_issue_function(issue, resolution)
asteps.append(steps)
avalues.append(values)
return asteps, avalues
def generate_bids_agent(template, resolution):
s1, v1 = get_utility_function(template, resolution)
bids_values = cartesian(s1).transpose()
bids_values1 = cartesian(v1)
u1 = np.sum(bids_values1, axis=1)
return np.vstack([bids_values, u1]).transpose()
def generate_bids(template1, template2, resolution):
s1, v1 = get_utility_function(template1, resolution)
s2, v2 = get_utility_function(template2, resolution)
bids_values = cartesian(s1).transpose()
bids_values1 = cartesian(v1)
bids_values2 = cartesian(v2)
u1 = np.sum(bids_values1, axis=1)
u2 = np.sum(bids_values2, axis=1)
return np.vstack([bids_values, u1, u2]).transpose()