-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagents.py
264 lines (226 loc) · 11.1 KB
/
agents.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import random
import math
from const import Action, Strategy
import config
from classes import Negotiable, Offer
random.seed(0)
class Agent:
def __init__(self):
self.id = 0
self.strategy = Strategy
self.inbox = []
self.accepted_offers = []
self.previous_rejected = [] # used for reject first accept second strategy, ids of rejected
def done(self):
return False
def act(self, companies, candidates, compensation_data, time):
return Offer()
# Called after found a job
def happiness(self):
return 0
@staticmethod
def valuation(avg_valuation, std_dev):
std_dev_divisor = config.CANDIDATE_VALUATION_STD_DEV_DIVISOR
if std_dev_divisor is None:
std_dev_divisor = 5
valuation = Negotiable()
valuation.salary = avg_valuation.salary + std_dev*avg_valuation.salary/std_dev_divisor
valuation.retirement += avg_valuation.retirement + std_dev*avg_valuation.retirement/std_dev_divisor
valuation.benefits += avg_valuation.benefits + std_dev*avg_valuation.benefits/std_dev_divisor
valuation.other += avg_valuation.other + std_dev*avg_valuation.other/std_dev_divisor
return valuation
@staticmethod
def negotiate_down(offer):
return offer.increase_by_pct(-1*config.negotiation_pct())
@staticmethod
def negotiate_up(offer):
return offer.increase_by_pct(config.negotiation_pct())
def has_rejected(self, idx):
return idx in self.previous_rejected
def give(self, decision):
self.inbox.append(decision)
def __repr__(self):
return '<{} #{}, {}, offers: {}, done? {}>'.format(type(self).__name__, self.id, self.strategy, len(self.inbox),
self.done())
class Company(Agent):
def __init__(self):
super(Company, self).__init__()
self.candidates_to_hire = 0
self.candidates_hired = []
self.candidate_valuations = []
self.pending_offers = {}
def done(self):
return len(self.candidates_hired) == self.candidates_to_hire
# if <= valuation of candidate, satisfied with offer
def is_satisfied(self, offer):
valuation_total = self.candidate_valuations[offer.candidate][1].total()
# radius = config.ACCEPTANCE_RADIUS_PCT*valuation_total
return offer.total() < valuation_total # (offer.total() - valuation_total) <= radius
# Called after finished to determine success of company
def happiness(self):
happiness = 0.0
for offer in self.accepted_offers:
valuation = self.candidate_valuations[offer.candidate][1]
happiness += (valuation.total() * config.COMPANY_ACCEPTED_OFFER_MULTIPLIER) - offer.total()
lacking = self.candidates_to_hire - len(self.accepted_offers)
if lacking > 0:
happiness -= lacking*config.COMPANY_LOST_HAPPINESS_MISSING_EMPLOYEES
count = len(self.accepted_offers)
if count == 0:
count = 1
return happiness/count
def act(self, companies, candidates, compensation_data, time):
my_offers = []
pending = 0
# remove old offers
to_remove = []
for key, offer in self.pending_offers.items():
expire_time = offer[1]
if expire_time >= time:
to_remove.append(key)
for key in to_remove:
del self.pending_offers[key]
for offer in sorted(self.inbox):
# Acknowledge acceptance
if offer.action == Action.accept:
offer.company_accepted_at = offer.total()
self.candidates_hired.append(offer.candidate)
self.accepted_offers.append(offer)
continue
# Reached limit, reject offers
if len(self.candidates_hired) + pending >= self.candidates_to_hire:
# my_offers.append(offer.change_action(Action.reject))
break
# Handle incoming offers
if offer.action == Action.propose:
if self.strategy == Strategy.accept_first:
my_offers.append(offer.change_action(offer, Action.accept))
elif self.strategy == Strategy.reject_first_accept_second:
if self.has_rejected(offer.candidate):
my_offers.append(offer.change_action(offer, Action.accept))
else:
my_offers.append(self.negotiate_down(offer.change_action(offer, Action.propose)))
self.previous_rejected.append(offer.candidate)
elif self.strategy == Strategy.randomly_accept:
die = random.randint(1, 100)
if die >= config.COMPANY_RANDOM_STRATEGY_THRESHOLD:
my_offers.append(offer.change_action(offer, Action.accept))
else:
my_offers.append(self.negotiate_down(offer.change_action(offer, Action.propose)))
elif self.strategy == Strategy.negotiate_until_satisfied:
if self.is_satisfied(offer):
my_offers.append(offer.change_action(offer, Action.accept))
else:
my_offers.append(self.negotiate_down(offer.change_action(offer, Action.propose)))
elif self.strategy == Strategy.negotiate_once:
if self.has_rejected(offer.candidate):
my_offers.append(offer.change_action(offer, Action.accept))
else:
my_offers.append(self.negotiate_down(offer.change_action(offer, Action.propose)))
self.previous_rejected.append(offer.candidate)
pending += 1
self.inbox.clear()
# If need to fill spots still, send new offers
all_candidates = set([c.id for c in candidates])
hired_candidates = set(self.candidates_hired)
pending_candidates = set([o.candidate for o in my_offers] + list(self.pending_offers.keys()))
potential_candidates = list(all_candidates ^ hired_candidates ^ pending_candidates)
offers_to_send = math.ceil(min(self.candidates_to_hire - len(hired_candidates) - len(pending_candidates),
len(potential_candidates))/2)
for i in range(offers_to_send):
curr = random.choice(potential_candidates)
potential_candidates.remove(curr)
valuation = self.candidate_valuations[curr][1]
new_offer = Offer()
new_offer.salary = valuation.salary
new_offer.retirement = valuation.retirement
new_offer.benefits = valuation.benefits
new_offer.other = valuation.other
new_offer.increase_by_pct(-1*config.negotiation_pct())
new_offer.action = Action.propose
new_offer.candidate = curr
new_offer.company = self.id
my_offers.append(new_offer)
for my_offer in my_offers:
my_offer.sender_is_company = True
expire_time = time + config.offer_expire_time()
self.pending_offers[my_offer.candidate] = (my_offer, expire_time)
return my_offers
def decide_valuations(self, compensation_data, candidates):
for candidate in candidates:
std_dev = random.normalvariate(0, 0.5)
valuation = Agent.valuation(compensation_data[candidate.job_type], std_dev)
self.candidate_valuations.append((std_dev, valuation))
return
class Candidate(Agent):
def __init__(self):
super(Candidate, self).__init__()
self.job_type = ''
self.std_dev = 0.0 # Where candidate places themselves above or below average
self.valuation = Negotiable()
self.avg_valuation = Negotiable()
def done(self):
return len(self.accepted_offers) > 0
# if >= valuation of candidate, satisfied with offer
def is_satisfied(self, offer):
# radius = config.ACCEPTANCE_RADIUS_PCT*self.valuation.total()
return offer.total() > self.valuation.total() # (self.valuation.total() - offer.total()) <= radius
# Called after found a job
def happiness(self):
if len(self.accepted_offers) > 0:
accepted_offer_value = self.accepted_offers[0].total()*config.CANDIDATE_ACCEPTED_OFFER_MULTIPLIER
else:
accepted_offer_value = 0
return accepted_offer_value - self.valuation.total()
def act(self, companies, candidates, compensation_data, time):
if self.done():
return None
my_offers = []
for offer in sorted(self.inbox, reverse=True):
if offer.action == Action.accept:
my_offers.clear()
self.accepted_offers.append(offer)
break
if offer.action == Action.propose:
if self.strategy == Strategy.accept_first:
my_offers.append(offer.change_action(offer, Action.accept))
break
elif self.strategy == Strategy.reject_first_accept_second:
if self.has_rejected(offer.company):
my_offers.append(offer.change_action(offer, Action.accept))
break
else:
my_offers.append(self.negotiate_up(offer.change_action(offer, Action.propose)))
self.previous_rejected.append(offer.company)
elif self.strategy == Strategy.randomly_accept:
die = random.randint(1, 100)
if die >= config.CANDIDATE_RANDOM_STRATEGY_THRESHOLD:
my_offers.append(offer.change_action(offer, Action.accept))
break
else:
my_offers.append(self.negotiate_up(offer.change_action(offer, Action.propose)))
elif self.strategy == Strategy.negotiate_until_satisfied:
if self.is_satisfied(offer):
my_offers.append(offer.change_action(offer, Action.accept))
break
else:
my_offers.append(self.negotiate_up(offer.change_action(offer, Action.propose)))
elif self.strategy == Strategy.negotiate_once:
if self.has_rejected(offer.company):
my_offers.append(offer.change_action(offer, Action.accept))
break
else:
my_offers.append(self.negotiate_up(Offer.change_action(offer, Action.propose)))
self.previous_rejected.append(offer.company)
self.inbox.clear()
for my_offer in my_offers:
my_offer.sender_is_company = False
if my_offer.action == Action.accept:
my_offer.candidate_accepted_at = my_offer.total()
self.accepted_offers.append(my_offer)
return my_offers
def decide_valuation(self, avg_valuation):
self.std_dev = random.normalvariate(0, 0.5)
self.avg_valuation = avg_valuation
self.valuation = Agent.valuation(avg_valuation, self.std_dev)
return