salary02/worker.py

81 lines
2.9 KiB
Python

import math
import random
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 # used in self.update_yield
s_is_hired: bool # updated in self.update_working_firm_is_hired, env.create_and_destroy_bankrupt_firms
# c_work_months: int
s_work_duration: int # updated in self.update_wd_by_is_hired
working_firm: 'FirmAgent' # updated in self.update_working_firm_is_hired, env.create_and_destroy_bankrupt_firms
s_salary: float # updated in self.update_salary
s_yield: float # updated in self.update_yield
# s_w_applied: bool
c_alpha: float
def setup(self, alpha):
self.c_effort = uniform(0, 1)
self.s_is_hired = False
self.s_work_duration = randint(30, 60)
self.c_alpha = alpha
self.update_yield()
self.s_salary = 0
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_value = 0, 0
for f in lst_firms:
if f.c_incentive > max_incentive:
max_incentive = f.c_incentive
if f.s_value > max_value:
max_value = f.s_value
# computer the utility for each firm
if max_value == 0:
return random.choice(lst_firms)
max_utility, best_firm = 0, None
for f in lst_firms:
if self.s_salary < f.s_profit / 10:
u = math.pow(f.c_incentive / max_incentive, self.c_alpha) * math.pow(f.s_value / max_value,
1 - self.c_alpha)
if u > max_utility:
max_utility = u
best_firm = f
best_firm.apply(self)
def update_wd_by_is_hired(self):
if self.s_is_hired:
self.s_work_duration += 1
self.update_yield()
self.update_salary(self.working_firm)
def update_salary(self, the_firm: 'FirmAgent'):
if self.s_salary == 0:
self.s_salary = the_firm.initial_f_salary
else:
self.s_salary = max(min(self.s_salary * (1 + the_firm.c_incentive), the_firm.s_profit / 10), self.s_salary)
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
self.update_wd_by_is_hired()