Untitled
unknown
python
a year ago
6.9 kB
8
Indexable
import numpy as np
import polars as pl
from polars import col, DataFrame
from functools import lru_cache
from line_profiler import profile
import logging
from sys import stdout
from os import environ
def load_problem_data():
# Load demand data and other relevant data
return [pl.DataFrame(df) for df in load_problem_data()]
@profile
def get_my_solution(
demand,
assertions_enabled = False,
log_info = False,
parameters = Parameters(),
return_stock_log = False
):
# Step 1: Load problem data
demand = pl.DataFrame(demand)
_, datacenters, servers, selling_prices, elasticity = [pl.DataFrame(df) for df in load_problem_data()]
# Step 2: Calculate need
need = calculate_need(demand, datacenters, servers, elasticity)
# Step 3: Harvest from expiry pool
harvested_servers = harvest_from_expiry_pool(datacenters, servers, need)
# Step 4: Move servers
moved_servers = move_servers(datacenters, servers, demand)
# Step 5: Purchase
purchased_servers = purchase_servers(datacenters, servers, demand)
# Step 6: Simulate profitable scenarios
best_scenario = simulate_profitable_scenarios(demand, datacenters, servers, elasticity, moved_servers, purchased_servers)
# Step 7: Return the best scenario
return best_scenario
def calculate_need(demand, datacenters, servers, elasticity):
# Calculate total capacity needed to meet demand at each time step
need = np.sum(datacenters[t]["capacity"] for t in range(len(datacenters)))
return need
def harvest_from_expiry_pool(datacenters, servers, need):
# Harvest servers from the expired pool based on their capacity and elasticity
harvested_servers = []
for datacenter in datacenters:
for server in datacenter["servers"]:
if server["capacity"] > 0 and is_profitable_server(datacenter, server):
harvested_servers.append({"time_step": server["time_step"], "datacenter_id": datacenter["id"], "server_generation": server["generation"], "server_id": compress(i := i + 1).decode()})
return harvested_servers
def move_servers(datacenters, servers, demand):
# Move servers between datacenters based on their capacity and the demand at each time step
moved_servers = []
for datacenter in datacenters:
for server in datacenter["servers"]:
if is_profitable_server(datacenter, server) and can_move_server(datacenter, server, demand):
moved_servers.append({"time_step": server["time_step"], "datacenter_id": datacenter["id"], "server_generation": server["generation"], "server_id": compress(i := i + 1).decode()})
return moved_servers
def purchase_servers(datacenters, servers, demand):
# Purchase servers from the market based on their capacity and the demand at each time step
purchased_servers = []
for datacenter in datacenters:
for server in datacenter["servers"]:
if is_profitable_server(datacenter, server) and can_purchase_server(datacenter, server, demand):
purchased_servers.append({"time_step": server["time_step"], "datacenter_id": datacenter["id"], "server_generation": server["generation"], "server_id": compress(i := i + 1).decode()})
return purchased_servers
def simulate_profitable_scenarios(demand, datacenters, servers, elasticity, moved_servers, purchased_servers):
# Simulate different scenarios of server purchases and moves to determine the most profitable one
best_scenario = None
for i in range(10):
scenario = simulate_scenario(demand, datacenters, servers, elasticity, moved_servers, purchased_servers)
if is_profitable_scenario(scenario):
best_scenario = scenario
return best_scenario
def is_profitable_server(datacenter, server):
# Check if the server is profitable based on its capacity and the demand at each time step
return server["capacity"] > 0 and can_make_profit(datacenter, server, demand)
def can_move_server(datacenter, server):
# Check if the server can be moved between datacenters based on its capacity and the demand at each time step
return server["capacity"] > 0 and can_move_server_between_datacenters(datacenter, server)
def can_purchase_server(datacenter, server):
# Check if the server can be purchased from the market based on its capacity and the demand at each time step
return server["capacity"] > 0 and can_purchase_server_from_market(datacenter, server)
def is_profitable_scenario(scenario):
# Check if the scenario is profitable based on the demand and capacity of the servers and datacenters
return np.sum(scenario["profit"]) > 0
def simulate_scenario(demand, datacenters, servers, elasticity, moved_servers, purchased_servers):
# Simulate a scenario by running the algorithm for each time step in the scenario
scenarios = []
for t in range(len(demand)):
scenario = {}
for i in range(len(datacenters)):
if datacenters[t][i]["capacity"] > 0:
scenario[t + 1]["profit"] += (datacenters[t][i]["capacity"] * moved_servers[t][i]) / elasticity
for i in range(len(servers)):
if servers[t][i]["capacity"] > 0:
scenario[t + 1]["profit"] += (servers[t][i]["capacity"] * purchased_servers[t][i]) / elasticity
scenarios.append(scenario)
return scenarios
def can_move_server_between_datacenters(datacenter, server):
# Check if the server can be moved between datacenters based on its capacity and the demand at each time step
return datacenter["capacity"] > 0 and server["capacity"] > 0 and can_move_server_within_datacenter(datacenter, server)
def can_move_server_within_datacenter(datacenter, server):
# Check if the server can be moved within a datacenter based on its capacity and the demand at each time step
return server["capacity"] > 0 and datacenter["capacity"] > 0
def can_purchase_server_from_market(datacenter, server):
# Check if the server can be purchased from the market based on its capacity and the demand at each time step
return server["capacity"] > 0 and can_purchase_server_from_market_within_datacenter(datacenter, server)
def can_purchase_server_from_market_within_datacenter(datacenter, server):
# Check if the server can be purchased from the market within a datacenter based on its capacity and the demand at each time step
return datacenter["capacity"] > 0 and server["capacity"] > 0 and can_purchase_server_from_market(datacenter, server)
def main():
# Read input from file
with open("input.txt", "r") as f:
demand = list(map(int, f))
print("Demand:", demand)
if __name__ == "__main__":
main()Editor is loading...
Leave a Comment