Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- from sklearn.linear_model import LinearRegression
- # Step 1: Define labor hours needed for producing goods
- labor_per_unit = {
- "Wheat": 3, # Labor hours to produce 1 unit of wheat
- "Yeast": 2, # Labor hours to produce 1 unit of yeast
- "Bread": 5, # Labor hours to produce 1 unit of bread
- "Steel": 8, # Labor hours to produce 1 unit of steel
- "Consumer_Goods": 4 # Labor hours to produce 1 unit of consumer goods
- }
- # Step 2: Define initial demand for goods
- initial_demand = {
- "Wheat": 5000,
- "Yeast": 2000,
- "Bread": 4000,
- "Steel": 3000,
- "Consumer_Goods": 6000
- }
- # Step 3: Forecast demand for the next 12 months (simplified linear forecasting)
- # Historical demand data for each product (hypothetical monthly demand over 6 months)
- historical_demand = {
- "Wheat": [5000, 5200, 5300, 5400, 5500, 5600],
- "Yeast": [2000, 2100, 2200, 2300, 2400, 2500],
- "Bread": [4000, 4100, 4200, 4300, 4400, 4500],
- "Steel": [3000, 3100, 3200, 3300, 3400, 3500],
- "Consumer_Goods": [6000, 6100, 6200, 6300, 6400, 6500]
- }
- # Step 4: Use linear regression to forecast demand for the next 12 months
- def forecast_demand(historical_data, months=12):
- forecasted_demand = {}
- for product, demand in historical_data.items():
- # Create an array for the months
- months_array = np.array(range(len(demand))).reshape(-1, 1)
- demand_array = np.array(demand).reshape(-1, 1)
- # Create and train a linear regression model
- model = LinearRegression()
- model.fit(months_array, demand_array)
- # Predict demand for the next 'months' months
- future_months = np.array(range(len(demand), len(demand) + months)).reshape(-1, 1)
- future_demand = model.predict(future_months).flatten()
- forecasted_demand[product] = future_demand
- return forecasted_demand
- # Get the forecasted demand for the next 12 months
- forecasted_demand = forecast_demand(historical_demand)
- # Step 5: Calculate labor time required to meet forecasted demand
- def calculate_labor_time(demand, labor_per_unit):
- labor_time_required = {}
- for product, forecast in demand.items():
- labor_time_required[product] = [labor_per_unit[product] * unit for unit in forecast]
- return labor_time_required
- # Get labor time required for forecasted demand
- labor_time_required = calculate_labor_time(forecasted_demand, labor_per_unit)
- # Step 6: Define Population and Labor Force Parameters
- population = 10000000 # Total population
- labor_force_participation_rate = 0.60 # 60% of the population is in the labor force
- hours_per_worker_per_month = 160 # Assuming 160 working hours per worker per month
- # Step 7: Calculate available labor force
- labor_force_size = population * labor_force_participation_rate # Number of workers
- total_labor_force = labor_force_size * hours_per_worker_per_month # Total available labor hours per month
- # Step 8: Check if labor allocation is feasible
- def check_labor_feasibility(labor_time_required, total_labor_force):
- total_labor_needed = sum([sum(labor_time) for labor_time in labor_time_required.values()])
- if total_labor_needed > total_labor_force:
- print("Warning: Labor shortage! Total labor needed exceeds available labor hours.")
- else:
- print(f"Labor allocation is feasible. Total labor used: {total_labor_needed} hours.")
- return total_labor_needed
- # Check labor feasibility
- total_labor_needed = check_labor_feasibility(labor_time_required, total_labor_force)
- # Step 9: Visualizing the demand forecast and labor allocation
- def plot_forecasts_and_labor(forecasted_demand, labor_time_required):
- months = np.arange(1, 13) # Forecasted months (1-12)
- fig, ax1 = plt.subplots(figsize=(10, 6))
- ax1.set_title("Forecasted Demand and Labor Allocation")
- ax1.set_xlabel("Month")
- ax1.set_ylabel("Forecasted Demand (Units)", color='tab:blue')
- # Plot forecasted demand
- for product, forecast in forecasted_demand.items():
- ax1.plot(months, forecast, label=f"{product} Demand", marker='o')
- ax1.tick_params(axis='y', labelcolor='tab:blue')
- ax2 = ax1.twinx() # Instantiate another y-axis sharing the same x-axis
- ax2.set_ylabel("Labor Time Required (Hours)", color='tab:red')
- # Plot labor time required
- for product, labor_time in labor_time_required.items():
- ax2.plot(months, labor_time, label=f"{product} Labor", linestyle="--", marker='x')
- ax2.tick_params(axis='y', labelcolor='tab:red')
- fig.tight_layout() # Adjust layout to make room for labels
- plt.legend()
- plt.show()
- # Plot the results
- plot_forecasts_and_labor(forecasted_demand, labor_time_required)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement