salary02/firm.py

143 lines
5.8 KiB
Python
Raw Normal View History

2022-08-29 10:39:29 +08:00
import math
2023-01-18 22:21:22 +08:00
# from typing import Union, Any
2022-08-29 10:39:29 +08:00
import agentpy as ap
from random import uniform, randint
2023-01-18 22:21:22 +08:00
# from typing import TYPE_CHECKING
2022-08-29 10:39:29 +08:00
2023-01-18 22:21:22 +08:00
# import numpy as np
2022-08-29 10:39:29 +08:00
2023-01-18 22:21:22 +08:00
# if TYPE_CHECKING:
# from worker import WorkerAgent
2022-08-29 10:39:29 +08:00
class FirmAgent(ap.Agent):
c_incentive: float
s_IsRH: bool
2023-01-15 21:51:47 +08:00
s_IsFH: bool
2023-01-18 22:21:22 +08:00
s_avg_senior_yield: float # updated in self.update_yields
s_avg_junior_yield: float # updated in self.update_yields
s_a_yield: float # updated in self.update_yields
2022-08-29 10:39:29 +08:00
# s_salary: float
initial_f_salary: float
2023-01-18 22:21:22 +08:00
s_revenue: float # updated in env.provide_logit_share
s_profit: float # updated in self.update_s_profit
s_value: float # updated in self.update_s_profit
# firing_worker: 'WorkerAgent'
l_senior_workers: list # updated in self.update_two_worker_list
l_junior_workers: list # updated in self.update_two_worker_list
l_applied_workers: list # updated in self.apply, self.empty_apply
def setup(self, is_rh, is_fh):
2022-08-29 10:39:29 +08:00
self.c_incentive = uniform(0, 1)
# self.s_profit = randint(10, 20)
2022-10-31 15:05:22 +08:00
self.l_senior_workers, self.l_junior_workers = [], []
2023-01-18 22:21:22 +08:00
# self.l_all_workers = []
2022-08-29 10:39:29 +08:00
self.l_applied_workers = []
2023-01-18 22:21:22 +08:00
self.s_IsRH = is_rh
self.s_IsFH = is_fh
2022-08-29 10:39:29 +08:00
self.initial_f_salary = randint(8000, 10000)
2022-10-31 15:05:22 +08:00
self.s_profit = 0
self.s_value = 0
2023-02-12 23:43:40 +08:00
self.s_a_yield = 0
2022-08-29 10:39:29 +08:00
def apply(self, the_worker):
self.l_applied_workers.append(the_worker)
def empty_apply(self):
self.l_applied_workers = []
def select_worker(self):
2023-01-18 22:21:22 +08:00
"""
2022-08-29 10:39:29 +08:00
企业找到最想招聘的员工
:return:
2023-01-18 22:21:22 +08:00
"""
2022-08-29 10:39:29 +08:00
n_workers = len(self.l_applied_workers)
if n_workers > 0:
2023-01-18 22:21:22 +08:00
# selected_worker = None
2022-08-29 10:39:29 +08:00
if n_workers > 1:
# 对员工产出进行排队和比较
# 先判断是什么选择方式
if self.s_IsRH:
max_s_yield, best_worker = 0.0, None
for the_worker in self.l_applied_workers:
if the_worker.s_yield > max_s_yield:
max_s_yield = the_worker.s_yield
best_worker = the_worker
selected_worker = best_worker
else:
2023-01-18 22:21:22 +08:00
# selected_worker = self.l_applied_workers[0]
2022-10-31 15:05:22 +08:00
# 加入worker进入到某个企业企业更新了自己的利润利润差值作为该名员工能带来的单位利润排序利润
# 但是代码中对于员工产出是01需要先更新员工所在列表然后更新企业产出、利润、工资总额再用假设利润-上期利润
# 问题是当期的估算不能考虑到有员工会在下一期离指的问题或许考虑换成max_单位产出成本
2023-01-18 22:21:22 +08:00
max_unit_yield_salary, p_salary, best_worker = 0.0, 0.0, None
2022-10-31 15:05:22 +08:00
for the_worker in self.l_applied_workers:
if the_worker.s_salary == 0:
2023-01-18 22:21:22 +08:00
p_salary = self.initial_f_salary
2022-10-31 15:05:22 +08:00
else:
2023-01-18 22:21:22 +08:00
p_salary = the_worker.s_salary * (1 + self.c_incentive)
unit_yield_salary = the_worker.s_yield * 10000 / p_salary
if unit_yield_salary > max_unit_yield_salary:
max_unit_yield_salary = unit_yield_salary
2022-10-31 15:05:22 +08:00
best_worker = the_worker
selected_worker = best_worker
2022-08-29 10:39:29 +08:00
else:
selected_worker = self.l_applied_workers[0]
selected_worker.update_working_firm_is_hired(self)
self.update_two_worker_list(selected_worker)
def update_two_worker_list(self, new_worker):
lst_all_worker = self.l_senior_workers + self.l_junior_workers + [new_worker]
2023-01-18 22:21:22 +08:00
lst_all_worker.sort(key=lambda x: x['s_yield'], reverse=True) # from the highest yield to lowest
2022-08-29 10:39:29 +08:00
n_all_worker = len(lst_all_worker)
# 向上取整数值
n_senior_worker = math.ceil(n_all_worker * 0.2)
n_junior_worker = n_all_worker - n_senior_worker
2022-10-31 15:05:22 +08:00
self.l_senior_workers = lst_all_worker[:n_senior_worker]
2022-08-29 10:39:29 +08:00
if n_junior_worker == 0:
self.l_junior_workers = []
else:
2022-10-31 15:05:22 +08:00
self.l_junior_workers = lst_all_worker[n_senior_worker:]
2022-08-29 10:39:29 +08:00
2022-10-31 15:05:22 +08:00
def update_yields(self):
2022-08-29 10:39:29 +08:00
n_sw, n_jw = len(self.l_senior_workers), len(self.l_junior_workers)
# acc_all_yield = 0
if n_sw == 0:
self.s_avg_senior_yield = 0
else:
s_acc_senior_yield = 0
for sw in self.l_senior_workers:
# 加入工人主体的产出属性
s_acc_senior_yield += sw.s_yield
# acc_all_yield += sw.s_yield
self.s_avg_senior_yield = s_acc_senior_yield / n_sw
if n_jw == 0:
self.s_avg_junior_yield = 0
else:
s_acc_junior_yield = 0
for jw in self.l_junior_workers:
# 加入工人主体的产出属性
s_acc_junior_yield += jw.s_yield
self.s_avg_junior_yield = s_acc_junior_yield / n_jw
self.s_a_yield = 0.8 * self.s_avg_senior_yield + 0.2 * self.s_avg_junior_yield
2022-10-31 15:05:22 +08:00
def get_sum_salary(self):
2023-01-18 22:21:22 +08:00
"""
2022-08-29 10:39:29 +08:00
计算某公司整体的薪金水平
2023-01-18 22:21:22 +08:00
"""
2022-10-31 15:05:22 +08:00
l_all_workers = self.l_senior_workers + self.l_junior_workers
sum_salary = 0.0
2022-08-29 10:39:29 +08:00
for the_worker in l_all_workers:
2022-10-31 15:05:22 +08:00
sum_salary += the_worker.s_salary
return sum_salary
2022-08-29 10:39:29 +08:00
2022-10-31 15:05:22 +08:00
def update_s_profit(self):
self.s_profit = self.s_revenue - self.get_sum_salary()
self.s_value += self.s_profit
2022-08-29 10:39:29 +08:00
2023-01-18 22:21:22 +08:00
# def step(self):
# pass