线性规划代码详解(By 小赵先生) modified_simulation_code_corrected.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 import numpy as npimport matplotlib.pyplot as pltimport mathDAYS_IN_YEAR = 365 INITIAL_PENS_AVAILABLE = 168 conception_rate = 0.85 mating_days = 20 pregnancy_days_range = [147 , 150 ] base_lactation_days = 40 base_resting_days = 18 average_lambs_per_ewe = 2.2 lamb_mortality_rate = 0.03 pen_capacity = {'mating' : 14 , 'pregnant' : 8 , 'lactation' : 6 , 'fattening' : 14 } pen_cost = {'unused' : 1 , 'rented' : 3 } ewes_per_batch = 239 import numpy as npimport matplotlib.pyplot as pltimport mathDAYS_IN_YEAR = 365 INITIAL_PENS_AVAILABLE = 168 conception_rate = 0.85 mating_days = 20 pregnancy_days_range = [147 , 150 ] base_lactation_days = 40 base_resting_days = 18 average_lambs_per_ewe = 2.2 lamb_mortality_rate = 0.03 pen_capacity = {'mating' : 14 , 'pregnant' : 8 , 'lactation' : 6 , 'fattening' : 14 } pen_cost = {'unused' : 1 , 'rented' : 3 } ewes_per_batch = 239 N = [0 ] * (DAYS_IN_YEAR + 1 ) P = [0 ] * (DAYS_IN_YEAR + 1 ) L = [0 ] * (DAYS_IN_YEAR + 1 ) F = [0 ] * (DAYS_IN_YEAR + 1 ) loss_day = [0 ] * (DAYS_IN_YEAR + 1 ) pens_required_day = [0 ] * (DAYS_IN_YEAR + 1 ) N[0 ] = ewes_per_batch for t in range (DAYS_IN_YEAR - max (pregnancy_days_range) - base_lactation_days - base_resting_days): newly_pregnant_ewes = np.random.binomial(N[t], conception_rate) N[t + mating_days] += N[t] - newly_pregnant_ewes N[t] -= newly_pregnant_ewes pregnancy_duration = np.random.choice(pregnancy_days_range, newly_pregnant_ewes) unique_durations, counts = np.unique(pregnancy_duration, return_counts=True ) for duration, count in zip (unique_durations, counts): L[t + duration] += count P[t] -= sum (counts) lambs_born = np.random.poisson(average_lambs_per_ewe, L[t]) lambs_surviving = lambs_born - np.random.binomial(lambs_born, lamb_mortality_rate) F[t + base_lactation_days] += sum (lambs_surviving) N[t + base_lactation_days + base_resting_days] += L[t] L[t] = 0 pens_required_day[t] = ( math.ceil(N[t] / pen_capacity['mating' ]) + math.ceil(P[t] / pen_capacity['pregnant' ]) + math.ceil(L[t] / pen_capacity['lactation' ]) + math.ceil(F[t] / pen_capacity['fattening' ]) ) if pens_required_day[t] <= INITIAL_PENS_AVAILABLE: loss_day[t] = (INITIAL_PENS_AVAILABLE - pens_required_day[t]) * pen_cost['unused' ] else : loss_day[t] = (pens_required_day[t] - INITIAL_PENS_AVAILABLE) * pen_cost['rented' ] plt.figure(figsize=(15 , 6 )) plt.subplot(1 , 2 , 1 ) plt.plot(pens_required_day, color='skyblue' ) plt.axhline(y=INITIAL_PENS_AVAILABLE, color='r' , linestyle='--' ) plt.title("Daily Ewes Required" ) plt.xlabel("Days" ) plt.ylabel("Number of Ewes" ) plt.legend(['Ewes Required' , 'Ewes Available' ]) plt.subplot(1 , 2 , 2 ) plt.plot(loss_day, color='lightcoral' ) plt.title("Daily Loss in Ewes" ) plt.xlabel("Days" ) plt.ylabel("Loss Value" ) plt.tight_layout() plt.show()
代码详解
导入所需的库:numpy 用于数学运算,matplotlib.pyplot 用于绘制图表,math 用于基本数学函数。
定义了一系列常数和系数,如 DAYS_IN_YEAR 表示一年的天数,INITIAL_PENS_AVAILABLE 表示初始可用的围栏数量。
conception_rate 是受孕率,表示母羊受孕的概率。
mating_days 是交配期的天数。
pregnancy_days_range 是怀孕期的天数范围。
base_lactation_days 是基础哺乳天数。
base_resting_days 是基础休息天数。
average_lambs_per_ewe 是每只母羊平均产的小羊数量。
lamb_mortality_rate 是小羊的死亡率。
pen_capacity 是一个字典,描述了不同类型围栏的容量。
pen_cost 是一个字典,描述了围栏的费用。
ewes_per_batch 是每批母羊的数量。
运行结果
上面的图表显示了模拟的结果。
modified_code_no_visual(1)(1).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 import numpy as npimport mathDAYS_IN_YEAR = 365 INITIAL_PENS_AVAILABLE = 168 conception_rate = 0.85 mating_days = 20 pregnancy_days_range = [147 , 150 ] base_lactation_days = 40 base_resting_days = 18 average_lambs_per_ewe = 2.2 lamb_mortality_rate = 0.03 pen_capacity = {'mating' : 14 , 'pregnant' : 8 , 'lactation' : 6 , 'fattening' : 14 } pen_cost = {'unused' : 1 , 'rented' : 3 } ewes_per_batch = 239 import numpy as npimport matplotlib.pyplot as pltimport mathDAYS_IN_YEAR = 365 INITIAL_PENS_AVAILABLE = 239 conception_rate = 0.85 mating_days = 20 pregnancy_days_range = [147 , 150 ] base_lactation_days = 40 base_resting_days = 18 average_lambs_per_ewe = 2.2 lamb_mortality_rate = 0.03 pen_capacity = {'mating' : 14 , 'pregnant' : 8 , 'lactation' : 6 , 'fattening' : 14 } pen_cost = {'unused' : 1 , 'rented' : 3 } ewes_per_batch = 239 N = [0 ] * (DAYS_IN_YEAR + 1 ) P = [0 ] * (DAYS_IN_YEAR + 1 ) L = [0 ] * (DAYS_IN_YEAR + 1 ) F = [0 ] * (DAYS_IN_YEAR + 1 ) loss_day = [0 ] * (DAYS_IN_YEAR + 1 ) pens_required_day = [0 ] * (DAYS_IN_YEAR + 1 ) N[0 ] = ewes_per_batch for t in range (DAYS_IN_YEAR - max (pregnancy_days_range) - base_lactation_days - base_resting_days): newly_pregnant_ewes = np.random.binomial(N[t], conception_rate) N[t + mating_days] += N[t] - newly_pregnant_ewes N[t] -= newly_pregnant_ewes pregnancy_duration = np.random.choice(pregnancy_days_range, newly_pregnant_ewes) unique_durations, counts = np.unique(pregnancy_duration, return_counts=True ) for duration, count in zip (unique_durations, counts): L[t + duration] += count P[t] -= sum (counts) lambs_born = np.random.poisson(average_lambs_per_ewe, L[t]) lambs_surviving = lambs_born - np.random.binomial(lambs_born, lamb_mortality_rate) F[t + base_lactation_days] += sum (lambs_surviving) N[t + base_lactation_days + base_resting_days] += L[t] L[t] = 0 pens_required_day[t] = ( math.ceil(N[t] / pen_capacity['mating' ]) + math.ceil(P[t] / pen_capacity['pregnant' ]) + math.ceil(L[t] / pen_capacity['lactation' ]) + math.ceil(F[t] / pen_capacity['fattening' ]) ) if pens_required_day[t] <= INITIAL_PENS_AVAILABLE: loss_day[t] = (INITIAL_PENS_AVAILABLE - pens_required_day[t]) * pen_cost['unused' ] else : loss_day[t] = (pens_required_day[t] - INITIAL_PENS_AVAILABLE) * pen_cost['rented' ]
代码详解
导入所需的库:numpy 用于数学运算,math 用于基本数学函数。
定义了一系列常数和系数,如 DAYS_IN_YEAR 表示一年的天数,INITIAL_PENS_AVAILABLE 表示初始可用的围栏数量。
conception_rate 是受孕率,表示母羊受孕的概率。
mating_days 是交配期的天数。
pregnancy_days_range 是怀孕期的天数范围。
base_lactation_days 是基础哺乳天数。
base_resting_days 是基础休息天数。
average_lambs_per_ewe 是每只母羊平均产的小羊数量。
lamb_mortality_rate 是小羊的死亡率。
pen_capacity 是一个字典,描述了不同类型围栏的容量。
pen_cost 是一个字典,描述了围栏的费用。
ewes_per_batch 是每批母羊的数量。
此代码不生成可视化结果。
modified_user_simulation_code(1).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 import numpy as npimport matplotlib.pyplot as pltimport mathDAYS_IN_YEAR = 365 INITIAL_PENS_AVAILABLE = 168 conception_rate = 0.85 mating_days = 20 pregnancy_days_range = [147 , 150 ] base_lactation_days = 40 base_resting_days = 18 average_lambs_per_ewe = 2.2 lamb_mortality_rate = 0.03 pen_capacity = {'mating' : 14 , 'pregnant' : 8 , 'lactation' : 6 , 'fattening' : 14 } pen_cost = {'unused' : 1 , 'rented' : 3 } ewes_per_batch = 239 import numpy as npimport matplotlib.pyplot as pltimport mathn_simulations = 1000 pens_required_list = [] lamb_output_list = [] ewes_per_batch = 168 conception_rate = 0.85 pregnancy_days_range = range (147 , 151 ) average_lambs_per_ewe = 2.2 lamb_mortality_rate = 0.03 pen_capacity = { 'mating' : 14 , 'pregnant' : 8 , 'lactation' : 6 , 'fattening' : 14 } for _ in range (n_simulations): ewes_mated = ewes_per_batch ewes_pregnant = np.random.binomial(ewes_mated, conception_rate) pregnancy_duration = np.random.choice(pregnancy_days_range, ewes_pregnant) lambs_born = np.random.poisson(average_lambs_per_ewe, ewes_pregnant) lambs_surviving = lambs_born - np.random.binomial(lambs_born, lamb_mortality_rate) pens_mating = math.ceil(ewes_mated / pen_capacity['mating' ]) pens_pregnant = math.ceil(ewes_pregnant / pen_capacity['pregnant' ]) pens_lactation = math.ceil(ewes_pregnant / pen_capacity['lactation' ]) pens_fattening = math.ceil(sum (lambs_surviving) / pen_capacity['fattening' ]) total_pens = pens_mating + pens_pregnant + pens_lactation + pens_fattening pens_required_list.append(total_pens) lamb_output_list.append(sum (lambs_surviving)) plt.figure(figsize=(15 , 6 )) plt.subplot(1 , 2 , 1 ) plt.hist(pens_required_list, bins=30 , color='skyblue' , edgecolor='black' ) plt.title("Distribution of Pens Required" ) plt.xlabel("Number of Pens" ) plt.ylabel("Frequency" ) plt.subplot(1 , 2 , 2 ) plt.hist(lamb_output_list, bins=30 , color='lightcoral' , edgecolor='black' ) plt.title("Distribution of Lamb Output" ) plt.xlabel("Number of Lambs" ) plt.ylabel("Frequency" ) plt.tight_layout() plt.show()
代码详解
导入所需的库:numpy 用于数学运算,matplotlib.pyplot 用于绘制图表,math 用于基本数学函数。
定义了一系列常数和系数,如 DAYS_IN_YEAR 表示一年的天数,INITIAL_PENS_AVAILABLE 表示初始可用的围栏数量。
conception_rate 是受孕率,表示母羊受孕的概率。
mating_days 是交配期的天数。
pregnancy_days_range 是怀孕期的天数范围。
base_lactation_days 是基础哺乳天数。
base_resting_days 是基础休息天数。
average_lambs_per_ewe 是每只母羊平均产的小羊数量。
lamb_mortality_rate 是小羊的死亡率。
pen_capacity 是一个字典,描述了不同类型围栏的容量。
pen_cost 是一个字典,描述了围栏的费用。
ewes_per_batch 是每批母羊的数量。
运行结果
上面的图表显示了模拟的结果。
modified_code_no_visual(1).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 import numpy as npimport mathDAYS_IN_YEAR = 365 INITIAL_PENS_AVAILABLE = 168 conception_rate = 0.85 mating_days = 20 pregnancy_days_range = [147 , 150 ] base_lactation_days = 40 base_resting_days = 18 average_lambs_per_ewe = 2.2 lamb_mortality_rate = 0.03 pen_capacity = {'mating' : 14 , 'pregnant' : 8 , 'lactation' : 6 , 'fattening' : 14 } pen_cost = {'unused' : 1 , 'rented' : 3 } ewes_per_batch = 239 import numpy as npimport matplotlib.pyplot as pltimport mathDAYS_IN_YEAR = 365 INITIAL_PENS_AVAILABLE = 239 conception_rate = 0.85 mating_days = 20 pregnancy_days_range = [147 , 150 ] base_lactation_days = 40 base_resting_days = 18 average_lambs_per_ewe = 2.2 lamb_mortality_rate = 0.03 pen_capacity = {'mating' : 14 , 'pregnant' : 8 , 'lactation' : 6 , 'fattening' : 14 } pen_cost = {'unused' : 1 , 'rented' : 3 } ewes_per_batch = 239 N = [0 ] * (DAYS_IN_YEAR + 1 ) P = [0 ] * (DAYS_IN_YEAR + 1 ) L = [0 ] * (DAYS_IN_YEAR + 1 ) F = [0 ] * (DAYS_IN_YEAR + 1 ) loss_day = [0 ] * (DAYS_IN_YEAR + 1 ) pens_required_day = [0 ] * (DAYS_IN_YEAR + 1 ) N[0 ] = ewes_per_batch for t in range (DAYS_IN_YEAR - max (pregnancy_days_range) - base_lactation_days - base_resting_days): newly_pregnant_ewes = np.random.binomial(N[t], conception_rate) N[t + mating_days] += N[t] - newly_pregnant_ewes N[t] -= newly_pregnant_ewes pregnancy_duration = np.random.choice(pregnancy_days_range, newly_pregnant_ewes) unique_durations, counts = np.unique(pregnancy_duration, return_counts=True ) for duration, count in zip (unique_durations, counts): L[t + duration] += count P[t] -= sum (counts) lambs_born = np.random.poisson(average_lambs_per_ewe, L[t]) lambs_surviving = lambs_born - np.random.binomial(lambs_born, lamb_mortality_rate) F[t + base_lactation_days] += sum (lambs_surviving) N[t + base_lactation_days + base_resting_days] += L[t] L[t] = 0 pens_required_day[t] = ( math.ceil(N[t] / pen_capacity['mating' ]) + math.ceil(P[t] / pen_capacity['pregnant' ]) + math.ceil(L[t] / pen_capacity['lactation' ]) + math.ceil(F[t] / pen_capacity['fattening' ]) ) if pens_required_day[t] <= INITIAL_PENS_AVAILABLE: loss_day[t] = (INITIAL_PENS_AVAILABLE - pens_required_day[t]) * pen_cost['unused' ] else : loss_day[t] = (pens_required_day[t] - INITIAL_PENS_AVAILABLE) * pen_cost['rented' ]
代码详解
导入所需的库:numpy 用于数学运算,math 用于基本数学函数。
定义了一系列常数和系数,如 DAYS_IN_YEAR 表示一年的天数,INITIAL_PENS_AVAILABLE 表示初始可用的围栏数量。
conception_rate 是受孕率,表示母羊受孕的概率。
mating_days 是交配期的天数。
pregnancy_days_range 是怀孕期的天数范围。
base_lactation_days 是基础哺乳天数。
base_resting_days 是基础休息天数。
average_lambs_per_ewe 是每只母羊平均产的小羊数量。
lamb_mortality_rate 是小羊的死亡率。
pen_capacity 是一个字典,描述了不同类型围栏的容量。
pen_cost 是一个字典,描述了围栏的费用。
ewes_per_batch 是每批母羊的数量。
此代码不生成可视化结果。