Advertisement
Guest User

cybernetic planning

a guest
May 3rd, 2025
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.76 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. from sklearn.linear_model import LinearRegression
  5.  
  6. # Step 1: Define labor hours needed for producing goods
  7. labor_per_unit = {
  8.     "Wheat": 3,  # Labor hours to produce 1 unit of wheat
  9.     "Yeast": 2,  # Labor hours to produce 1 unit of yeast
  10.     "Bread": 5,  # Labor hours to produce 1 unit of bread
  11.     "Steel": 8,  # Labor hours to produce 1 unit of steel
  12.     "Consumer_Goods": 4  # Labor hours to produce 1 unit of consumer goods
  13. }
  14.  
  15. # Step 2: Define initial demand for goods
  16. initial_demand = {
  17.     "Wheat": 5000,
  18.     "Yeast": 2000,
  19.     "Bread": 4000,
  20.     "Steel": 3000,
  21.     "Consumer_Goods": 6000
  22. }
  23.  
  24. # Step 3: Forecast demand for the next 12 months (simplified linear forecasting)
  25. # Historical demand data for each product (hypothetical monthly demand over 6 months)
  26. historical_demand = {
  27.     "Wheat": [5000, 5200, 5300, 5400, 5500, 5600],
  28.     "Yeast": [2000, 2100, 2200, 2300, 2400, 2500],
  29.     "Bread": [4000, 4100, 4200, 4300, 4400, 4500],
  30.     "Steel": [3000, 3100, 3200, 3300, 3400, 3500],
  31.     "Consumer_Goods": [6000, 6100, 6200, 6300, 6400, 6500]
  32. }
  33.  
  34. # Step 4: Use linear regression to forecast demand for the next 12 months
  35. def forecast_demand(historical_data, months=12):
  36.     forecasted_demand = {}
  37.     for product, demand in historical_data.items():
  38.         # Create an array for the months
  39.         months_array = np.array(range(len(demand))).reshape(-1, 1)
  40.         demand_array = np.array(demand).reshape(-1, 1)
  41.  
  42.         # Create and train a linear regression model
  43.         model = LinearRegression()
  44.         model.fit(months_array, demand_array)
  45.  
  46.         # Predict demand for the next 'months' months
  47.         future_months = np.array(range(len(demand), len(demand) + months)).reshape(-1, 1)
  48.         future_demand = model.predict(future_months).flatten()
  49.  
  50.         forecasted_demand[product] = future_demand
  51.     return forecasted_demand
  52.  
  53. # Get the forecasted demand for the next 12 months
  54. forecasted_demand = forecast_demand(historical_demand)
  55.  
  56. # Step 5: Calculate labor time required to meet forecasted demand
  57. def calculate_labor_time(demand, labor_per_unit):
  58.     labor_time_required = {}
  59.     for product, forecast in demand.items():
  60.         labor_time_required[product] = [labor_per_unit[product] * unit for unit in forecast]
  61.     return labor_time_required
  62.  
  63. # Get labor time required for forecasted demand
  64. labor_time_required = calculate_labor_time(forecasted_demand, labor_per_unit)
  65.  
  66. # Step 6: Define Population and Labor Force Parameters
  67. population = 10000000  # Total population
  68. labor_force_participation_rate = 0.60  # 60% of the population is in the labor force
  69. hours_per_worker_per_month = 160  # Assuming 160 working hours per worker per month
  70.  
  71. # Step 7: Calculate available labor force
  72. labor_force_size = population * labor_force_participation_rate  # Number of workers
  73. total_labor_force = labor_force_size * hours_per_worker_per_month  # Total available labor hours per month
  74.  
  75. # Step 8: Check if labor allocation is feasible
  76. def check_labor_feasibility(labor_time_required, total_labor_force):
  77.     total_labor_needed = sum([sum(labor_time) for labor_time in labor_time_required.values()])
  78.    
  79.     if total_labor_needed > total_labor_force:
  80.         print("Warning: Labor shortage! Total labor needed exceeds available labor hours.")
  81.     else:
  82.         print(f"Labor allocation is feasible. Total labor used: {total_labor_needed} hours.")
  83.     return total_labor_needed
  84.  
  85. # Check labor feasibility
  86. total_labor_needed = check_labor_feasibility(labor_time_required, total_labor_force)
  87.  
  88. # Step 9: Visualizing the demand forecast and labor allocation
  89. def plot_forecasts_and_labor(forecasted_demand, labor_time_required):
  90.     months = np.arange(1, 13)  # Forecasted months (1-12)
  91.    
  92.     fig, ax1 = plt.subplots(figsize=(10, 6))
  93.  
  94.     ax1.set_title("Forecasted Demand and Labor Allocation")
  95.     ax1.set_xlabel("Month")
  96.     ax1.set_ylabel("Forecasted Demand (Units)", color='tab:blue')
  97.    
  98.     # Plot forecasted demand
  99.     for product, forecast in forecasted_demand.items():
  100.         ax1.plot(months, forecast, label=f"{product} Demand", marker='o')
  101.  
  102.     ax1.tick_params(axis='y', labelcolor='tab:blue')
  103.  
  104.     ax2 = ax1.twinx()  # Instantiate another y-axis sharing the same x-axis
  105.     ax2.set_ylabel("Labor Time Required (Hours)", color='tab:red')
  106.    
  107.     # Plot labor time required
  108.     for product, labor_time in labor_time_required.items():
  109.         ax2.plot(months, labor_time, label=f"{product} Labor", linestyle="--", marker='x')
  110.  
  111.     ax2.tick_params(axis='y', labelcolor='tab:red')
  112.  
  113.     fig.tight_layout()  # Adjust layout to make room for labels
  114.     plt.legend()
  115.     plt.show()
  116.  
  117. # Plot the results
  118. plot_forecasts_and_labor(forecasted_demand, labor_time_required)
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement