準備
console
pip install numpy
ソースコード
sample.py
# -*- coding: utf-8 -*-
##3-clause license ("BSD License 2.0", "Revised BSD License", "New BSD License", or "Modified BSD License")
##修正BSDライセンス・三条項BSDライセンス
##Copyright (c) 2023, a_d_j_u_s_t
##All rights reserved.
##Redistribution and use in source and binary forms, with or without
##modification, are permitted provided that the following conditions are met:
##* Redistributions of source code must retain the above copyright notice,
## this list of conditions and the following disclaimer.
##* Redistributions in binary form must reproduce the above copyright notice,
## this list of conditions and the following disclaimer in the documentation
## and/or other materials provided with the distribution.
##* Neither the name of the <organization> nor the names of its contributors
## may be used to endorse or promote products derived from this software
## without specific prior written permission.
##THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
##ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
##WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
##DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
##DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
##(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
##LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
##(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
##SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import numpy as np
# 従業員のリストと各従業員が週に働く日数
employees = ["従業員1", "従業員2", "従業員3", "従業員4"]
days_per_week = {"従業員1": 5, "従業員2": 4, "従業員3": 3, "従業員4": 5}
#1日あたりに出勤する従業員の最低人数
day_out_employes=[2,2,2,2,3,3,3]
sum_days=0
for employee in employees:
sum_days+=days_per_week[employee]
if sum_days < sum(day_out_employes):
print("please,adjust day_out_employes.")
# シフトの初期化(週7日分)
shift_schedule = {employee: ["休み"] * 7 for employee in employees}
# シフトをランダムに作成する関数
def create_random_shift(employee, days_to_work):
sh=np.array([])
while True:
sh=np.random.randint(0,2,(7,))
if days_to_work==sh.sum():
break
shift=np.array([])
for i in sh:
if i==1:
shift=np.append(shift,"出勤")
else:
shift=np.append(shift,"休み")
return shift
while True:
# シフトを生成
for employee in employees:
days_to_work = days_per_week[employee]
shift_schedule[employee] = create_random_shift(employee, days_to_work)
sh=np.array([])
sh_count=np.full((7,),0)
for employee in employees:
sh=np.array(shift_schedule[employee])
for d in range(7):
if sh[d]=="出勤":
sh_count[d]+=1
out_flag=0
for d in range(7):
if sh_count[d]>=day_out_employes[d]:
out_flag+=1
if out_flag >= 7:
break
# シフトを表示
for employee, shift in shift_schedule.items():
print(f"{employee}: {', '.join(shift)}")