From d3d7f08ad8ca9667f4577330df9be21f00a813ea Mon Sep 17 00:00:00 2001 From: "Rafael S. Perez" Date: Tue, 8 Oct 2024 23:39:40 +0100 Subject: [PATCH 1/6] [001] - Base structure and environment --- .gitignore | 1 + src/app.py | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 .gitignore create mode 100644 src/app.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..bc21dba8e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/validation/ diff --git a/src/app.py b/src/app.py new file mode 100644 index 000000000..39bef55f8 --- /dev/null +++ b/src/app.py @@ -0,0 +1,10 @@ +from flask import Flask, request, jsonify +app = Flask(__name__) + +@app.route('/productionplan', methods=['POST']) +def productionplan(): + payload = jsonify(request.json) + return payload + +if __name__ == '__main__': + app.run(debug=True,port=8888) \ No newline at end of file From cdc66dbd6cdb7325a278f45adc689c0651598a2a Mon Sep 17 00:00:00 2001 From: "Rafael S. Perez" Date: Tue, 8 Oct 2024 23:41:18 +0100 Subject: [PATCH 2/6] [001] - Base structure and environment --- requirements.txt | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..07c6cc5b2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,95 @@ +anyio==4.6.0 +argon2-cffi==23.1.0 +argon2-cffi-bindings==21.2.0 +arrow==1.3.0 +asttokens==2.4.1 +async-lru==2.0.4 +attrs==24.2.0 +babel==2.16.0 +beautifulsoup4==4.12.3 +bleach==6.1.0 +blinker==1.8.2 +certifi==2024.8.30 +cffi==1.17.1 +charset-normalizer==3.3.2 +click==8.1.7 +comm==0.2.2 +debugpy==1.8.6 +decorator==5.1.1 +defusedxml==0.7.1 +executing==2.1.0 +fastjsonschema==2.20.0 +Flask==3.0.3 +fqdn==1.5.1 +h11==0.14.0 +httpcore==1.0.6 +httpx==0.27.2 +idna==3.10 +ipykernel==6.29.5 +ipython==8.28.0 +isoduration==20.11.0 +itsdangerous==2.2.0 +jedi==0.19.1 +Jinja2==3.1.4 +json5==0.9.25 +jsonpointer==3.0.0 +jsonschema==4.23.0 +jsonschema-specifications==2024.10.1 +jupyter-events==0.10.0 +jupyter-lsp==2.2.5 +jupyter_client==8.6.3 +jupyter_core==5.7.2 +jupyter_server==2.14.2 +jupyter_server_terminals==0.5.3 +jupyterlab==4.2.5 +jupyterlab_pygments==0.3.0 +jupyterlab_server==2.27.3 +MarkupSafe==3.0.1 +matplotlib-inline==0.1.7 +mistune==3.0.2 +nbclient==0.10.0 +nbconvert==7.16.4 +nbformat==5.10.4 +nest-asyncio==1.6.0 +notebook==7.2.2 +notebook_shim==0.2.4 +overrides==7.7.0 +packaging==24.1 +pandocfilters==1.5.1 +parso==0.8.4 +pexpect==4.9.0 +platformdirs==4.3.6 +prometheus_client==0.21.0 +prompt_toolkit==3.0.48 +psutil==6.0.0 +ptyprocess==0.7.0 +pure_eval==0.2.3 +pycparser==2.22 +Pygments==2.18.0 +python-dateutil==2.9.0.post0 +python-json-logger==2.0.7 +PyYAML==6.0.2 +pyzmq==26.2.0 +referencing==0.35.1 +requests==2.32.3 +rfc3339-validator==0.1.4 +rfc3986-validator==0.1.1 +rpds-py==0.20.0 +Send2Trash==1.8.3 +setuptools==75.1.0 +six==1.16.0 +sniffio==1.3.1 +soupsieve==2.6 +stack-data==0.6.3 +terminado==0.18.1 +tinycss2==1.3.0 +tornado==6.4.1 +traitlets==5.14.3 +types-python-dateutil==2.9.0.20241003 +uri-template==1.3.0 +urllib3==2.2.3 +wcwidth==0.2.13 +webcolors==24.8.0 +webencodings==0.5.1 +websocket-client==1.8.0 +Werkzeug==3.0.4 From 6390bd428a278d71534d6b65745acf05c412f593 Mon Sep 17 00:00:00 2001 From: "Rafael S. Perez" Date: Wed, 9 Oct 2024 00:49:42 +0100 Subject: [PATCH 3/6] [002] - Calculating functions --- src/app.py | 11 ++++-- src/methods.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 src/methods.py diff --git a/src/app.py b/src/app.py index 39bef55f8..1c0d6734e 100644 --- a/src/app.py +++ b/src/app.py @@ -1,10 +1,17 @@ from flask import Flask, request, jsonify +from methods import calculate_production_plan app = Flask(__name__) @app.route('/productionplan', methods=['POST']) def productionplan(): - payload = jsonify(request.json) - return payload + try: + payload = request.get_json() + if not payload: + raise ValueError("Invalid or missing payload") + return calculate_production_plan(payload), 200 + + except Exception as e: + return {"error": "Internal Server Error"}, 500 if __name__ == '__main__': app.run(debug=True,port=8888) \ No newline at end of file diff --git a/src/methods.py b/src/methods.py new file mode 100644 index 000000000..a21abb857 --- /dev/null +++ b/src/methods.py @@ -0,0 +1,96 @@ +def calculate_production_plan(payload): + """ + This function will implement the algorithm for the calculation of the production + plan based on the payload provided and sort the payload accordingly. + + Parameters: + payload (dict): A dictionary containing the load, fuels, and powerplants. + + Returns: + dict: A dictionary containing the sorted production plan. + + Raises: + ValueError: If the payload is missing required keys or if the load is not a number. + """ + # Extract values from payload + load = payload['load'] + fuels = payload['fuels'] + powerplants = payload['powerplants'] + + # Extract the relevant fuel costs + gas_price = fuels['gas(euro/MWh)'] + kerosine_price = fuels['kerosine(euro/MWh)'] + co2_price = fuels['co2(euro/ton)'] + wind_percentage = fuels['wind(%)'] + + # Sort powerplants based on cost-efficiency (cheapest first) + powerplants = sorted( + powerplants, + key=lambda p: get_cost_efficiency(p, gas_price, kerosine_price, co2_price, wind_percentage) + ) + + # Initialize result list + production_plan = [] + remaining_load = load + + for plant in powerplants: + if remaining_load <= 0: + # If the load is already met, the plant remains off + production_plan.append({"name": plant['name'], "p": 0}) + continue + + p_min = plant['pmin'] + p_max = plant['pmax'] + + if plant['type'] == 'windturbine': + # Wind turbine generates power based on wind percentage + p_max = p_max * (wind_percentage / 100) + production = min(remaining_load, p_max) + production_plan.append({"name": plant['name'], "p": production}) + remaining_load -= production + else: + # Gas-fired or turbojet plants must produce at least pmin when switched on + production = min(remaining_load, p_max) + if production >= p_min: + production_plan.append({"name": plant['name'], "p": production}) + remaining_load -= production + else: + # If production cannot meet pmin, the plant remains off + production_plan.append({"name": plant['name'], "p": 0}) + + # If we still have a load that could not be met + if remaining_load > 0: + raise ValueError("It is not possible to meet the load with the available power plants.") + + return production_plan + + +def get_cost_efficiency(plant, gas_price, kerosine_price, co2_price, wind_percentage): + """ + This function will provide the costs of a power plant based on its type and fuel costs. + + Parameters: + plant (dict): A dictionary containing information about the power plant. + gas_price (float): The price of gas (euro/MWh). + kerosine_price (float): The price of kerosine (euro/MWh). + co2_price (float): The price of CO2 (euro/ton). + wind_percentage (float): The percentage of wind power produced by the plant. + + Returns: + float: The cost-efficiency of the power plant. + + Raises: + ValueError: If the power plant type is unknown. + """ + if plant['type'] == 'gasfired': + # Gas-fired plants have a fuel cost and a CO2 cost (0.3 tons of CO2 emitted per MWh) + co2_emissions_per_mwh = 0.3 + return (gas_price + co2_emissions_per_mwh * co2_price) / plant['efficiency'] + elif plant['type'] == 'turbojet': + # Turbojets use kerosine as fuel, no CO2 costs assumed here + return kerosine_price / plant['efficiency'] + elif plant['type'] == 'windturbine': + # Wind turbines have no fuel cost, so their cost-efficiency is 0 + return 0 + else: + raise ValueError(f"Unknown power plant type: { plant['type'] }") From 408f423728718e9e0614544be63432e1d3d1cf01 Mon Sep 17 00:00:00 2001 From: "Rafael S. Perez" Date: Wed, 9 Oct 2024 01:04:15 +0100 Subject: [PATCH 4/6] [003] - Setting up logger --- src/app.py | 16 +++++++++++----- src/logger.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/logger.py diff --git a/src/app.py b/src/app.py index 1c0d6734e..f34c78478 100644 --- a/src/app.py +++ b/src/app.py @@ -1,17 +1,23 @@ -from flask import Flask, request, jsonify +from flask import Flask, request from methods import calculate_production_plan +from logger import setup_logger app = Flask(__name__) - +logger = setup_logger() @app.route('/productionplan', methods=['POST']) def productionplan(): try: payload = request.get_json() if not payload: - raise ValueError("Invalid or missing payload") + payload_issue_message = "Invalid or missing payload" + logger.warning(payload_issue_message) + raise ValueError(payload_issue_message) + logger.info(f"Payload received: {payload}") return calculate_production_plan(payload), 200 except Exception as e: - return {"error": "Internal Server Error"}, 500 + exception_message = f"Error while processing payload : {e}" + logger.error(exception_message) + return {"error": exception_message}, 500 if __name__ == '__main__': - app.run(debug=True,port=8888) \ No newline at end of file + app.run(debug=True,port=8889) \ No newline at end of file diff --git a/src/logger.py b/src/logger.py new file mode 100644 index 000000000..681decb52 --- /dev/null +++ b/src/logger.py @@ -0,0 +1,31 @@ +import logging + +def setup_logger(): + """ + Sets up the logger with predefined formats for info, error, and warning messages. + """ + # Create custom logger + logger = logging.getLogger("production_plan_logger") + logger.setLevel(logging.DEBUG) # Allow all levels of logging + + # Create console handler and set level to debug + ch = logging.StreamHandler() + ch.setLevel(logging.DEBUG) + + # Create a formatter that includes the log level and the message + formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') + + # Add formatter to the console handler + ch.setFormatter(formatter) + + # Add console handler to logger + logger.addHandler(ch) + + # Example: Predefined log messages + logger.info("Logger initialized successfully.") + logger.warning("This is a predefined warning message.") + logger.error("This is a predefined error message.") + + return logger + + From cce2fa6385eb5efaa822ff0539f8f6f7555f4b92 Mon Sep 17 00:00:00 2001 From: "Rafael S. Perez" Date: Wed, 9 Oct 2024 01:24:13 +0100 Subject: [PATCH 5/6] [004] - Docs, configs and instructions --- .gitignore | 1 - README.md | 186 +++++++++++++++++++++++-------------------------- pyproject.toml | 20 ++++++ 3 files changed, 109 insertions(+), 98 deletions(-) delete mode 100644 .gitignore create mode 100644 pyproject.toml diff --git a/.gitignore b/.gitignore deleted file mode 100644 index bc21dba8e..000000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/validation/ diff --git a/README.md b/README.md index 44c93d608..0740a439d 100644 --- a/README.md +++ b/README.md @@ -1,99 +1,91 @@ # 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) - +
+ +### Deploy and run using Poetry: +1. **Get project** +``` +git clone https://github.com/shizde/powerplant-coding-challenge.git +cd powerplant-coding-challenge +``` + +2. **Install Poetry:** +If you don’t have Poetry installed, you can install it with: + +``` +curl -sSL https://install.python-poetry.org | python3 - +``` + +3. **Install Dependencies:** Run the following command in the root of your project (where the file pyproject.toml is located): + +``` +poetry install +``` + +4. **Run the Project:** you can run your API with the following command: + +``` +poetry run python src/app.py +``` + +5. **Testing API Call:** On a terminal, run the following command + +``` +curl --location '127.0.0.1:8888/productionplan' \ +--header 'Content-Type: application/json' \ +--data '{ + "load": 910, + "fuels": + { + "gas(euro/MWh)": 13.4, + "kerosine(euro/MWh)": 50.8, + "co2(euro/ton)": 20, + "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": "gasfiredsomewhatsmaller", + "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 + } + ] +} +' +``` \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 000000000..81a1a5326 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,20 @@ +[tool.poetry] +name = "production_plan_api" +version = "0.1.0" +description = "A REST API to calculate the production plan for power plants." +authors = ["Rafael S. Perez "] +license = "MIT" + +[tool.poetry.dependencies] +python = "^3.8" + + +[tool.poetry.scripts] +production-plan = "src.app:app" # This can be adjusted if you're using FastAPI or Flask + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" + +[tool.poetry.dev-dependencies] +# pytest = "^6.2.4" From 087ebe9fe30da439ae7e3feb038a7e78dee0dc27 Mon Sep 17 00:00:00 2001 From: "Rafael S. Perez" Date: Wed, 9 Oct 2024 01:24:22 +0100 Subject: [PATCH 6/6] [004] - Docs, configs and instructions --- src/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app.py b/src/app.py index f34c78478..2243cd382 100644 --- a/src/app.py +++ b/src/app.py @@ -20,4 +20,4 @@ def productionplan(): return {"error": exception_message}, 500 if __name__ == '__main__': - app.run(debug=True,port=8889) \ No newline at end of file + app.run(debug=True,port=8888) \ No newline at end of file