diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..8f40f12ab --- /dev/null +++ b/.gitignore @@ -0,0 +1,68 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# Virtual environment +.venv/ +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Flask specific +instance/ +.webassets-cache + +# Pytest +.cache/ +.pytest_cache/ + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ + +# Jupyter Notebook +.ipynb_checkpoints + +# Environments +.env +.env.* + +# IDEs +.vscode/ +.idea/ +*.swp +*~ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..51445d300 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Use the official Python image from the Docker Hub +FROM python:3.9-slim + +# Set the working directory in the container +WORKDIR /app + +# Copy the requirements file into the container +COPY requirements.txt . + +# Install the dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the rest of the application code into the container +COPY . . + +# Expose the port the app runs on +EXPOSE 8888 + +# Run the application +CMD ["python", "run.py"] \ No newline at end of file diff --git a/README.md b/README.md index 44c93d608..f5e1455b1 100644 --- a/README.md +++ b/README.md @@ -1,99 +1,94 @@ -# powerplant-coding-challenge - - -## Welcome ! - -Below you can find the description of a coding challenge that we ask people to perform when applying for a job in our team. - -The goal of this coding challenge is to provide the applicant some insight into the business we're in and as such provide the applicant an indication about the challenges she/he will be confronted with. Next, during the first interview we will use the applicant's implementation as a seed to discuss all kinds of interesting software engineering topics. - -Time is scarce, we know. Therefore we ask you not to spend more than 4 hours on this challenge. We know it is not possible to deliver a finished implementation of the challenge in only four hours. Even though your submission will not be complete, it will provide us plenty of information and topics to discuss later on during the talks. - -This coding-challenge is part of a formal process and is used in collaboration with the recruiting companies we work with. Submitting a pull-request will not automatically trigger the recruitement process. -## Who are we - -We are the IS team of the 'Short-term Power as-a-Service' (a.k.a. SPaaS) team within [GEM](https://gems.engie.com/). - -[GEM](https://gems.engie.com/), which stands for 'Global Energy Management', is the energy management arm of [ENGIE](https://www.engie.com/), one of the largest global energy players, -with access to local markets all over the world. - -SPaaS is a team consisting of around 100 people with experience in energy markets, IT and modeling. In smaller teams consisting of a mix of people with different experiences, we are active on the [day-ahead](https://en.wikipedia.org/wiki/European_Power_Exchange#Day-ahead_markets) market, [intraday markets](https://en.wikipedia.org/wiki/European_Power_Exchange#Intraday_markets) and [collaborate with the TSO to balance the grid continuously](https://en.wikipedia.org/wiki/Transmission_system_operator#Electricity_market_operations). - -## The challenge - -### In short -Calculate how much power each of a multitude of different [powerplants](https://en.wikipedia.org/wiki/Power_station) need to produce (a.k.a. the production-plan) when the [load](https://en.wikipedia.org/wiki/Load_profile) is given and taking into account the cost of the underlying energy sources (gas, kerosine) and the Pmin and Pmax of each powerplant. - -### More in detail - -The load is the continuous demand of power. The total load at each moment in time is forecasted. For instance for Belgium you can see the load forecasted by the grid operator [here](https://www.elia.be/en/grid-data/load-and-load-forecasts). - -At any moment in time, all available powerplants need to generate the power to exactly match the load. The cost of generating power can be different for every powerplant and is dependent on external factors: The cost of producing power using a [turbojet](https://en.wikipedia.org/wiki/Gas_turbine#Industrial_gas_turbines_for_power_generation), that runs on kerosine, is higher compared to the cost of generating power using a gas-fired powerplant because of gas being cheaper compared to kerosine and because of the [thermal efficiency](https://en.wikipedia.org/wiki/Thermal_efficiency) of a gas-fired powerplant being around 50% (2 units of gas will generate 1 unit of electricity) while that of a turbojet is only around 30%. The cost of generating power using windmills however is zero. Thus deciding which powerplants to activate is dependent on the [merit-order](https://en.wikipedia.org/wiki/Merit_order). - -When deciding which powerplants in the merit-order to activate (a.k.a. [unit-commitment problem](https://en.wikipedia.org/wiki/Unit_commitment_problem_in_electrical_power_production)) the maximum amount of power each powerplant can produce (Pmax) obviously needs to be taken into account. Additionally gas-fired powerplants generate a certain minimum amount of power when switched on, called the Pmin. - - -### Performing the challenge - -Build a REST API exposing an endpoint `/productionplan` that accepts a POST of which the body contains a payload as you can find in the `example_payloads` directory and that returns a json with the same structure as in `example_response.json` and that manages and logs run-time errors. - -For calculating the unit-commitment, we prefer you not to rely on an existing (linear-programming) solver but instead write an algorithm yourself. - -Implementations can be submitted in either C# (on .Net 5 or higher) or Python (3.8 or higher) as these are (currently) the main languages we use in SPaaS. Along with the implementation should be a README that describes how to compile (if applicable) and launch the application. - -- C# implementations should contain a project file to compile the application. -- Python implementations should contain a `requirements.txt` or a `pyproject.toml` (for use with poetry) to install all needed dependencies. - -#### Payload - -The payload contains 3 types of data: - - load: The load is the amount of energy (MWh) that need to be generated during one hour. - - fuels: based on the cost of the fuels of each powerplant, the merit-order can be determined which is the starting point for deciding which powerplants should be switched on and how much power they will deliver. Wind-turbine are either switched-on, and in that case generate a certain amount of energy depending on the % of wind, or can be switched off. - - gas(euro/MWh): the price of gas per MWh. Thus if gas is at 6 euro/MWh and if the efficiency of the powerplant is 50% (i.e. 2 units of gas will generate one unit of electricity), the cost of generating 1 MWh is 12 euro. - - kerosine(euro/Mwh): the price of kerosine per MWh. - - co2(euro/ton): the price of emission allowances (optionally to be taken into account). - - wind(%): percentage of wind. Example: if there is on average 25% wind during an hour, a wind-turbine with a Pmax of 4 MW will generate 1MWh of energy. - - powerplants: describes the powerplants at disposal to generate the demanded load. For each powerplant is specified: - - name: - - type: gasfired, turbojet or windturbine. - - efficiency: the efficiency at which they convert a MWh of fuel into a MWh of electrical energy. Wind-turbines do not consume 'fuel' and thus are considered to generate power at zero price. - - pmax: the maximum amount of power the powerplant can generate. - - pmin: the minimum amount of power the powerplant generates when switched on. - -#### response - -The response should be a json as in `example_payloads/response3.json`, which is the expected answer for `example_payloads/payload3.json`, specifying for each powerplant how much power each powerplant should deliver. The power produced by each powerplant has to be a multiple of 0.1 Mw and the sum of the power produced by all the powerplants together should equal the load. - -### Want more challenge? - -Having fun with this challenge and want to make it more realistic. Optionally, do one of the extra's below: - -#### Docker - -Provide a Dockerfile along with the implementation to allow deploying your solution quickly. - -#### CO2 - -Taken into account that a gas-fired powerplant also emits CO2, the cost of running the powerplant should also take into account the cost of the [emission allowances](https://en.wikipedia.org/wiki/Carbon_emission_trading). For this challenge, you may take into account that each MWh generated creates 0.3 ton of CO2. - -## Acceptance criteria - -For a submission to be reviewed as part of an application for a position in the team, the project needs to: - - contain a README.md explaining how to build and launch the API - - expose the API on port `8888` - -Failing to comply with any of these criteria will automatically disqualify the submission. - -## More info - -For more info on energy management, check out: - - - [Global Energy Management Solutions](https://www.youtube.com/watch?v=SAop0RSGdHM) - - [COO hydroelectric power station](https://www.youtube.com/watch?v=edamsBppnlg) - - [Management of supply](https://www.youtube.com/watch?v=eh6IIQeeX3c) - video made during winter 2018-2019 - -## FAQ - -##### Can an existing solver be used to calculate the unit-commitment -Implementations should not rely on an external solver and thus contain an algorithm written from scratch (clarified in the text as of version v1.1.0) - +# Power Plant Production Plan API + +This project is a ptyhon Flask-based API used in the code challenge intending to calculate power distribution using request parameters such as: load, fuels, and powerplants. + +## Prerequisites + +We assume that you need to have the following components in order to build and launch this application: + +* Docker +* Docker Compose + +## Project Structure +. +├── app/ +│ ├── \_\_init\_\_.py # Initializes the Flask application +│ ├── routes.py # Defines the API routes +│ ├── services.py # Contains the logic for calculating power distribution +│ ├── utils.py # Defines the utils methods +├── tests/ +│ ├── \_\_init\_\_.py # Initializes the unit tests module +│ ├── test_routes.py # Tests the API routes +│ ├── test_services.py # Tests the logic for calculating power distribution +├── .gitignore # Defines patterns to be ignored in the github repository +├── config.py # Defines the Flask application configuration +├── docker-compose.yml # Docker Compose file for setting up the services +├── Dockerfile # Dockerfile for building the Docker image +└── README.md # Documentation for the project +├── requirements.txt # Lists the project dependencies +├── run.py # Entry point for running the Flask application + +## Setup and Installation + +1. **Build the Docker image:** + + ```bash + docker-compose build + ``` + +1. Run the Docker container + + ```bash + docker-compose build + ``` + +1. The API will be available at http://localhost:8888. + * port 8888 is used following the requirement + +## API Endpoints + +```/productionplan``` + +* **Method**: ```POST``` +* **Description**: Calculates power distribution based on load, fuels, and powerplants. +* **Request Body (schema)**: + ```json + { + "load": "<< Integer containing the amount of energy (MWh) that need to be generated during one hour >>", + "fuels": { + "gas(euro/MWh)": "<< Float number containing the price of gas per MWh >>", + "kerosine(euro/MWh)": "<< Float number containing the price of kerosine per MWh >>", + "co2(euro/ton)": "<< Integer number containing the price of emission allowances >>", + "wind(%)": "<< Integer number containing the percentage of wind >>" + }, + "powerplants": [ + { + "name": "<< String containing the powerplant name >>", + "type": "<< Type on the powerplante (gasfired, turbojet or windturbine) >>", + "efficiency": "<< Float number containing the efficiency at which they convert a MWh of fuel into a MWh of electrical energy >>", + "pmin": "<< Number with the maximum amount of power the powerplant can generate >>", + "pmax": "<< Number with the minimum amount of power the powerplant generates when switched on >>" + } + ] + } + ``` +* **Response (schema)**: + ```json + [ + { + "name": "<< string representing powerplant name >>", + "p": "<< float number representing how much power was produced >>" + } + ] + ``` + +## Running unit tests: +For unit tests executions, please run the following command: + +```bash +docker-compose run test +``` + +## Author + +* **Guilherme Cardoso Fernandes** - [GitHub Profile](https://github.com/guicfernandes) \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 000000000..388d79198 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,12 @@ +from flask import Flask +from config import ProductionConfig, DevelopmentConfig + + +def create_app(config_class=ProductionConfig): + app = Flask(__name__) + app.config.from_object(config_class) + + with app.app_context(): + from . import routes + + return app diff --git a/app/routes.py b/app/routes.py new file mode 100644 index 000000000..f12c729bd --- /dev/null +++ b/app/routes.py @@ -0,0 +1,23 @@ +import logging +from flask import request, jsonify, current_app as app +from .services import calculate_power_distribution + + +@app.route("/productionplan", methods=["POST"]) +def production_plan(): + """Calculate power distribution based on input payload.""" + + try: + # Parse JSON payload + payload = request.get_json() + load = payload["load"] + fuels = payload["fuels"] + powerplants = payload["powerplants"] + + # Calculate power distribution + distribution = calculate_power_distribution(load, fuels, powerplants) + + return jsonify(distribution), 200 + except (KeyError, TypeError, ValueError) as e: + logging.error("Error processing request: %s", e) + return jsonify({"error": str(e)}), 400 diff --git a/app/services.py b/app/services.py new file mode 100644 index 000000000..d14b60cc2 --- /dev/null +++ b/app/services.py @@ -0,0 +1,61 @@ +from app.utils import allocate_powerplant_load + + +def calculate_power_distribution(load: int, fuels: dict, powerplants: list) -> list: + """Calculate power distribution based on load, fuels and powerplants. + + Args: + load (int): The amount of energy (MWh) that need to be generated during one hour. + fuels (dict): A dictionary containing the cost of different fuels. + powerplants (list): A list of powerplants with their respective properties. + + Raises: + ValueError: If a property value is not accepted. + + Returns: + list: A list of powerplants with their respective power output. + """ + # Parse fuels + gas_cost = fuels["gas(euro/MWh)"] / 0.5 # efficiency 50% + kerosine_cost = fuels["kerosine(euro/MWh)"] / 0.3 # efficiency 30% + wind_percentage = fuels["wind(%)"] / 100 + + # Calculate cost per MWh per powerplant + powerplant_costs = [] + for plant in powerplants: + if plant["type"] == "gasfired": + cost = gas_cost / plant["efficiency"] + elif plant["type"] == "turbojet": + cost = kerosine_cost / plant["efficiency"] + elif plant["type"] == "windturbine": + cost = 0 # wind is free + else: + raise ValueError("Unknown powerplant type") + + # Calculate effective max power for wind turbines + if plant["type"] == "windturbine": + max_power = plant["pmax"] * wind_percentage + else: + max_power = plant["pmax"] + + powerplant_costs.append( + { + "name": plant["name"], + "type": plant["type"], + "cost": cost, + "pmin": plant["pmin"], + "pmax": max_power, + } + ) + + # Sort powerplants by cost (merit order) + sorted_plants = sorted(powerplant_costs, key=lambda x: x["cost"]) + + # Allocate load based on merit order + power_distribution = allocate_powerplant_load(load, sorted_plants) + + # Ensure the total load matches exactly + if abs(sum(item["p"] for item in power_distribution) - load) > 0.1: + raise ValueError("Unable to satisfy load exactly with given powerplants.") + + return power_distribution diff --git a/app/utils.py b/app/utils.py new file mode 100644 index 000000000..50ff1a17d --- /dev/null +++ b/app/utils.py @@ -0,0 +1,37 @@ +def allocate_powerplant_load(load: float, sorted_plants: list) -> list: + """Allocate load to powerplants based on merit order. + + Args: + load (float): The total load to be allocated. + sorted_plants (list): A list of powerplants sorted by cost. + + Raises: + ValueError: If Pmin exceeds Pmax for a powerplant. + + Returns: + list: A list of powerplants with their respective power output. + """ + # Allocate load based on merit order + remaining_load = load + power_distribution = [] + for plant in sorted_plants: + if remaining_load <= 0: + # If load is already satisfied, set output to 0 for remaining plants + power_distribution.append({"name": plant["name"], "p": 0.0}) + continue + + # Determine how much power the plant can contribute + if plant["pmax"] < plant["pmin"]: + raise ValueError(f"Pmin cannot exceed Pmax for plant {plant['name']}.") + + power = 0 + if remaining_load >= plant["pmin"]: + power = min(plant["pmax"], remaining_load) + remaining_load -= power + + # If plant is not needed, output 0.0 + if power < plant["pmin"]: + power = 0.0 + + power_distribution.append({"name": plant["name"], "p": round(power, 1)}) + return power_distribution diff --git a/config.py b/config.py new file mode 100644 index 000000000..c2b1f0369 --- /dev/null +++ b/config.py @@ -0,0 +1,26 @@ +class Config: + """Set Flask configuration vars.""" + + DEBUG = False + + +class DevelopmentConfig(Config): + """Development configuration. + + Args: + Config: The base configuration class. + """ + + DEBUG = True + ENV = "development" + + +class ProductionConfig(Config): + """Production configuration. + + Args: + Config: The base configuration class. + """ + + DEBUG = False + ENV = "production" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..c7c3be523 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +services: + web: + build: . + ports: + - "8888:8888" + volumes: + - .:/app + environment: + - FLASK_ENV=development + + test: + build: . + command: ["pytest"] + volumes: + - .:/app \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..5bdf57f5b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,11 @@ +blinker==1.9.0 +click==8.1.7 +colorama==0.4.6 +flask==3.1.0 +importlib-metadata==8.5.0 +itsdangerous==2.2.0 +jinja2==3.1.4 +MarkupSafe==3.0.2 +pytest +werkzeug==3.1.3 +zipp==3.21.0 diff --git a/run.py b/run.py new file mode 100644 index 000000000..b7131181d --- /dev/null +++ b/run.py @@ -0,0 +1,10 @@ +from app import create_app +from config import ProductionConfig, DevelopmentConfig + +app = create_app(config_class=ProductionConfig) +# For development tests we should change ProductionConfig to DevelopmentConfig +# app = create_app(config_class=DevelopmentConfig) + + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=8888) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_routes.py b/tests/test_routes.py new file mode 100644 index 000000000..d8071e6ee --- /dev/null +++ b/tests/test_routes.py @@ -0,0 +1,62 @@ +import unittest +import json +from app import create_app + + +class ProductionPlanTestCase(unittest.TestCase): + """Test case for the production_plan route. + + Args: + unittest (TestCase): The base test case class. + """ + + def setUp(self): + self.app = create_app() + self.app.testing = True + self.client = self.app.test_client() + self.app_context = self.app.app_context() + self.app_context.push() + + def tearDown(self): + self.app_context.pop() + + def test_production_plan_success(self): + """Test a successful power distribution scenario.""" + + payload = { + "load": 480, + "fuels": {"gas(euro/MWh)": 13.4, "kerosine(euro/MWh)": 50.8, "wind(%)": 60}, + "powerplants": [ + { + "name": "gasfiredbig1", + "type": "gasfired", + "efficiency": 0.53, + "pmin": 100, + "pmax": 460, + }, + { + "name": "tj1", + "type": "turbojet", + "efficiency": 0.3, + "pmin": 0, + "pmax": 16, + }, + { + "name": "windpark1", + "type": "windturbine", + "efficiency": 1, + "pmin": 0, + "pmax": 150, + }, + ], + } + + response = self.client.post( + "/productionplan", data=json.dumps(payload), content_type="application/json" + ) + self.assertEqual(response.status_code, 200) + self.assertIsInstance(response.json, list) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_services.py b/tests/test_services.py new file mode 100644 index 000000000..7c5a3ca9d --- /dev/null +++ b/tests/test_services.py @@ -0,0 +1,138 @@ +import unittest +from app.services import calculate_power_distribution + + +class TestCalculatePowerDistribution(unittest.TestCase): + """Test case for the calculate_power_distribution function. + + Args: + unittest (TestCase): The base test case class. + """ + + def test_valid_distribution(self): + """Test a valid power distribution scenario.""" + + load = 480 + fuels = {"gas(euro/MWh)": 13.4, "kerosine(euro/MWh)": 50.8, "wind(%)": 60} + powerplants = [ + { + "name": "gasfiredbig1", + "type": "gasfired", + "efficiency": 0.53, + "pmin": 100, + "pmax": 460, + }, + { + "name": "gasfiredbig2", + "type": "gasfired", + "efficiency": 0.53, + "pmin": 100, + "pmax": 460, + }, + { + "name": "gasfiredsomewhatsmall", + "type": "gasfired", + "efficiency": 0.37, + "pmin": 40, + "pmax": 210, + }, + { + "name": "tj1", + "type": "turbojet", + "efficiency": 0.3, + "pmin": 0, + "pmax": 16, + }, + { + "name": "windpark1", + "type": "windturbine", + "efficiency": 1, + "pmin": 0, + "pmax": 150, + }, + { + "name": "windpark2", + "type": "windturbine", + "efficiency": 1, + "pmin": 0, + "pmax": 36, + }, + ] + expected_output = [ + {"name": "windpark1", "p": 90.0}, + {"name": "windpark2", "p": 21.6}, + {"name": "gasfiredbig1", "p": 368.4}, + {"name": "gasfiredbig2", "p": 0.0}, + {"name": "gasfiredsomewhatsmall", "p": 0.0}, + {"name": "tj1", "p": 0.0}, + ] + result = calculate_power_distribution(load, fuels, powerplants) + self.assertEqual(result, expected_output) + + def test_invalid_powerplant_type(self): + """Test an invalid powerplant type.""" + + load = 480 + fuels = {"gas(euro/MWh)": 13.4, "kerosine(euro/MWh)": 50.8, "wind(%)": 60} + powerplants = [ + { + "name": "some_plant", + "type": "any_type", + "efficiency": 0.53, + "pmin": 100, + "pmax": 460, + } + ] + with self.assertRaises(ValueError): + calculate_power_distribution(load, fuels, powerplants) + + def test_pmin_exceeds_pmax(self): + """Test a powerplant with pmin exceeding pmax.""" + + load = 480 + fuels = {"gas(euro/MWh)": 13.4, "kerosine(euro/MWh)": 50.8, "wind(%)": 60} + powerplants = [ + { + "name": "invalid_plant", + "type": "gasfired", + "efficiency": 0.53, + "pmin": 500, + "pmax": 460, + } + ] + with self.assertRaises(ValueError): + calculate_power_distribution(load, fuels, powerplants) + + def test_unable_to_satisfy_load(self): + """Test a scenario where it is impossible to satisfy the load.""" + load = 1000 # Set a load that cannot be satisfied by the given powerplants + fuels = {"gas(euro/MWh)": 13.4, "kerosine(euro/MWh)": 50.8, "wind(%)": 60} + powerplants = [ + { + "name": "gasfiredbig1", + "type": "gasfired", + "efficiency": 0.53, + "pmin": 100, + "pmax": 460, + }, + { + "name": "tj1", + "type": "turbojet", + "efficiency": 0.3, + "pmin": 0, + "pmax": 16, + }, + { + "name": "windpark1", + "type": "windturbine", + "efficiency": 1, + "pmin": 0, + "pmax": 150, + }, + ] + with self.assertRaises(ValueError): + calculate_power_distribution(load, fuels, powerplants) + + +if __name__ == "__main__": + unittest.main()