2022-08-29 10:39:29 +08:00
|
|
|
|
import math
|
|
|
|
|
|
|
|
|
|
import agentpy as ap
|
|
|
|
|
from random import uniform, randint
|
|
|
|
|
|
|
|
|
|
# 编程可以自动补充一些东西,减少报错
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from firm import FirmAgent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WorkerAgent(ap.Agent):
|
|
|
|
|
select_firm: object
|
|
|
|
|
c_effort: float
|
|
|
|
|
s_is_hired: bool
|
|
|
|
|
# c_work_months: int
|
|
|
|
|
s_work_duration: int
|
|
|
|
|
working_firm: 'FirmAgent'
|
|
|
|
|
s_salary: float
|
|
|
|
|
s_yield: float
|
|
|
|
|
# s_w_applied: bool
|
|
|
|
|
c_alpha: float
|
|
|
|
|
|
|
|
|
|
def setup(self, alpha):
|
|
|
|
|
# super().__init__(unique_id, model)
|
|
|
|
|
# self.num_workers = 10000
|
|
|
|
|
self.c_effort = uniform(0, 1)
|
|
|
|
|
# self.c_work_months = randint(0, 60)
|
|
|
|
|
self.s_is_hired = False
|
|
|
|
|
self.s_work_duration = randint(0, 60)
|
|
|
|
|
self.c_alpha = alpha
|
|
|
|
|
self.update_yield()
|
|
|
|
|
self.s_salary = 0
|
|
|
|
|
|
|
|
|
|
# return
|
|
|
|
|
|
|
|
|
|
# def A_Utility(self, c_w_weight=0.5):
|
|
|
|
|
# a = np.exp(FirmAgent.c_f_incentive, c_w_weight)
|
|
|
|
|
# b = np.exp(FirmAgent.s_f_profit/max(FirmAgent.s_f_profit), 1-c_w_weight)
|
|
|
|
|
# self.select_firm = a * b
|
|
|
|
|
|
|
|
|
|
def select_firm(self):
|
|
|
|
|
'''
|
|
|
|
|
挑选出来的企业列表
|
|
|
|
|
数量:列表的长度
|
|
|
|
|
'''
|
|
|
|
|
lst_firms = self.model.provide_lst_random_firms(self)
|
|
|
|
|
n_firms = len(lst_firms)
|
|
|
|
|
# find the max incentive and profit among all firms
|
|
|
|
|
max_incentive, max_profit = 0, 0
|
|
|
|
|
for f in lst_firms:
|
|
|
|
|
if f.c_incentive > max_incentive:
|
|
|
|
|
max_incentive = f.c_incentive
|
|
|
|
|
if f.s_profit > max_profit:
|
|
|
|
|
max_profit = f.s_profit
|
|
|
|
|
# computer the utility for each firm
|
|
|
|
|
max_utility, best_firm = 0, None
|
|
|
|
|
for f in lst_firms:
|
|
|
|
|
u = math.pow(f.c_incentive / max_incentive, self.c_alpha) * math.pow(f.s_profit/max_profit, 1-self.c_alpha)
|
|
|
|
|
if u > max_utility:
|
|
|
|
|
max_utility = u
|
|
|
|
|
best_firm = f
|
|
|
|
|
# print(f'{self}: my best firm is {best_firm} from {n_firms} firms with utility {max_utility}')
|
|
|
|
|
# 选出能够给自己带来最好的效用的企业,并输出/返回
|
|
|
|
|
best_firm.apply(self)
|
|
|
|
|
return best_firm
|
|
|
|
|
|
|
|
|
|
def update_wd_by_is_hired(self):
|
|
|
|
|
if self.s_is_hired == 1:
|
|
|
|
|
self.s_work_duration += 1
|
|
|
|
|
self.update_yield()
|
|
|
|
|
self.update_salary()
|
|
|
|
|
|
|
|
|
|
def update_salary(self, the_firm: 'FirmAgent'):
|
|
|
|
|
if self.s_salary == 0:
|
|
|
|
|
self.s_salary = the_firm.initial_f_salary
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
self.s_salary = self.s_salary * (1 + the_firm.c_incentive)
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def update_yield(self):
|
|
|
|
|
self.s_yield = 2 / (1 + math.exp(-0.01 * self.s_work_duration * self.c_effort)) - 1
|
|
|
|
|
|
|
|
|
|
def update_working_firm_is_hired(self, f: 'FirmAgent'):
|
|
|
|
|
self.s_is_hired = True
|
|
|
|
|
self.working_firm = f
|
|
|
|
|
|
|
|
|
|
def step(self):
|
|
|
|
|
self.update_wd_by_is_hired()
|
|
|
|
|
'''
|
|
|
|
|
是否更换公司成功的状态转换?
|
2022-08-01 10:04:35 +08:00
|
|
|
|
'''
|