diff --git a/App/app.py b/App/app.py new file mode 100644 index 000000000..ddf481a2e --- /dev/null +++ b/App/app.py @@ -0,0 +1,158 @@ +import json +from flask import Flask, request, jsonify +import logging + +app = Flask(__name__) +#Set up logging +logging.basicConfig(level=logging.INFO) + +@app.route('/production_plan',methods=['POST']) + +def production_plan(): + try: + # Import Data inputs + data = request.json + complete_load = data.get('load') + fuels = data.get('fuels') + powerplants_list = data.get('powerplants') + + if complete_load is None or fuels is None or powerplants_list is None: + return "Error : Load, fuels or powerplants is missing" + + logging.info(f"Calculating production plan for load = {complete_load} ") + + #Call the function to calculate the production plan + res = execute_production_plan(complete_load, fuels, powerplants_list) + + #Save the results in a JSON File + with open('production_plan_results.json', 'w') as json_file: + json.dump(res, json_file,indent=4) + + return jsonify(res),200 + except Exception as error: + logging.error(f"An error as occured : {str(error)}") + return jsonify({"error": str(error)}),500 + + + +def execute_production_plan(complete_load, fuels, powerplants_list): + # Go throught the powerplant list + for powerplant in powerplants_list: + print(powerplant) + if powerplant['type'] == 'gasfired': + fuel_cost = fuels['gas(euro/MWh)'] / powerplant['efficiency'] + cost_co2 = 0.3 * fuels['co2(euro/ton)'] + cost = fuel_cost + cost_co2 + elif powerplant['type'] == 'turbojet': + cost = fuels['kerosine(euro/MWh)'] / powerplant['efficiency'] + elif powerplant['type'] == 'windturbine': + cost = 0 + #We add the cost + powerplant['cost'] = cost + + # Now lets see the merit-order sorting all the powerplants by cost + powerplant_sorted = sorted(powerplants_list,key=lambda plant:plant['cost']) + + prod_plan = [] + current_load = complete_load + + #Wind allocation + + for powerplant in powerplant_sorted: + print(current_load) + if current_load <= 0: + break + max_ouput = powerplant['pmax'] + if powerplant['type'] == 'windturbine': + wind_output = min(max_ouput * (fuels.get('wind(%)',0) / 100),current_load) + produced_power = round(wind_output,1) #round to 0.1MW + prod_plan.append({ + "name": powerplant['name'], + "type": powerplant['type'], + "efficiency": powerplant['efficiency'], + "pmax": powerplant['pmax'], + "pmin": powerplant['pmin'], + "cost": powerplant['cost'], + "p": produced_power + }) + current_load = current_load - produced_power + else: + power_generate = min(current_load,max_ouput) # To be sure to respect the constraint of the powerplant + + #Make sure we use at least pmin and adjust for multiple of 0.1MW + power_generate = max(powerplant['pmin'],round(power_generate/0.1)*0.1) + power_generate = round(power_generate,1) + + #If the power generate is between pmin and pmax + if power_generate >= powerplant['pmin'] and power_generate <= powerplant['pmax']: + prod_plan.append({ + "name": powerplant['name'], + "type": powerplant['type'], + "efficiency": powerplant['efficiency'], + "pmax": powerplant['pmax'], + "pmin": powerplant['pmin'], + "cost": powerplant['cost'], + "p": round(power_generate,1) + }) + current_load = current_load - power_generate + else: + prod_plan.append({ + "name": powerplant['name'], + "type": powerplant['type'], + "efficiency": powerplant['efficiency'], + "pmax": powerplant['pmax'], + "pmin": powerplant['pmin'], + "cost": powerplant['cost'], + "p": 0.0 + }) + if current_load < 0: + for element in reversed(prod_plan): + if current_load >=0: + break + + #How much to reduce ? + current_power_prod = element['p'] + reducible_power = current_power_prod - element['pmin'] + if reducible_power > 0 : + #How much can be reduced ? + to_reduce = min(reducible_power,-current_load) + element['p'] = element['p'] - to_reduce + current_load = current_load + to_reduce + + #Reactivate remaining powerplants if current_load > pmin + + for powerplant in powerplant_sorted: + if current_load >= powerplant['pmin']: + #Check if the powerplant can generate additionnal power + for element in prod_plan: + if element['name'] == powerplant['name'] and element['p'] < powerplant['pmax']: + add_power = min(powerplant['pmax'] - element['p'],current_load) + element['p'] = element['p'] + add_power + current_load = current_load - add_power + break + + for powerplant in powerplants_list: + if not any(p['name'] == powerplant['name'] for p in prod_plan): + prod_plan.append({ + "name": powerplant['name'], + "type": powerplant['type'], + "efficiency": powerplant['efficiency'], + "pmax": powerplant['pmax'], + "pmin": powerplant['pmin'], + "cost": powerplant['cost'], + "p": 0.0 + }) + return {"Production plan": prod_plan} + + +with open('payload3.json','r') as f: + payload = json.load(f) +complete_load = payload.get('load') +fuels = payload.get('fuels') +power_plant_list = payload.get('powerplants') +results = execute_production_plan(complete_load,fuels,power_plant_list) +print(results) + + +if __name__ == '__main__': + app.run(port=8888,debug=True) \ No newline at end of file diff --git a/App/payload1.json b/App/payload1.json new file mode 100644 index 000000000..b377475fb --- /dev/null +++ b/App/payload1.json @@ -0,0 +1,54 @@ +{ + "load": 480, + "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 + } + ] +} diff --git a/App/payload2.json b/App/payload2.json new file mode 100644 index 000000000..f3c7525db --- /dev/null +++ b/App/payload2.json @@ -0,0 +1,54 @@ +{ + "load": 480, + "fuels": + { + "gas(euro/MWh)": 13.4, + "kerosine(euro/MWh)": 50.8, + "co2(euro/ton)": 20, + "wind(%)": 0 + }, + "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 + } + ] +} diff --git a/App/payload3.json b/App/payload3.json new file mode 100644 index 000000000..bd28884ce --- /dev/null +++ b/App/payload3.json @@ -0,0 +1,54 @@ +{ + "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 + } + ] +} diff --git a/App/production_plan_results.json b/App/production_plan_results.json new file mode 100644 index 000000000..2f42b8285 --- /dev/null +++ b/App/production_plan_results.json @@ -0,0 +1,58 @@ +{ + "Production plan": [ + { + "name": "windpark1", + "type": "windturbine", + "efficiency": 1, + "pmax": 150, + "pmin": 0, + "cost": 0, + "p": 90.0 + }, + { + "name": "windpark2", + "type": "windturbine", + "efficiency": 1, + "pmax": 36, + "pmin": 0, + "cost": 0, + "p": 21.6 + }, + { + "name": "gasfiredbig1", + "type": "gasfired", + "efficiency": 0.53, + "pmax": 460, + "pmin": 100, + "cost": 31.28301886792453, + "p": 460.0 + }, + { + "name": "gasfiredbig2", + "type": "gasfired", + "efficiency": 0.53, + "pmax": 460, + "pmin": 100, + "cost": 31.28301886792453, + "p": 338.4 + }, + { + "name": "gasfiredsomewhatsmaller", + "type": "gasfired", + "efficiency": 0.37, + "pmax": 210, + "pmin": 40, + "cost": 42.21621621621622, + "p": 0.0 + }, + { + "name": "tj1", + "type": "turbojet", + "efficiency": 0.3, + "pmax": 16, + "pmin": 0, + "cost": 169.33333333333334, + "p": 0.0 + } + ] +} \ No newline at end of file diff --git a/App/requirements.txt b/App/requirements.txt new file mode 100644 index 000000000..a993b8dff --- /dev/null +++ b/App/requirements.txt @@ -0,0 +1 @@ +Flask==3.0.3 \ No newline at end of file diff --git a/App/response3.json b/App/response3.json new file mode 100644 index 000000000..1dd9ed852 --- /dev/null +++ b/App/response3.json @@ -0,0 +1,26 @@ +[ + { + "name": "windpark1", + "p": 90.0 + }, + { + "name": "windpark2", + "p": 21.6 + }, + { + "name": "gasfiredbig1", + "p": 460.0 + }, + { + "name": "gasfiredbig2", + "p": 338.4 + }, + { + "name": "gasfiredsomewhatsmaller", + "p": 0.0 + }, + { + "name": "tj1", + "p": 0.0 + } +] \ No newline at end of file diff --git a/README.md b/README.md index 44c93d608..54a714a0e 100644 --- a/README.md +++ b/README.md @@ -1,99 +1,31 @@ -# 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) - +# Production Plan Calculator + +This Flask application calculates the production plan for power plants based on provided load and fuel costs. + +## Prerequisites + +- Python 3.x installed +- pip (Python package installer) + +## Installation +Install Dependencies using pip from the directory requirements.txt is located: + +pip install -r requirements.txt + +## Running the Application +Start the Flask Application: Run the following command in your terminal from the directory where app.py is located : + +python app.py + +This will start the Flask server, which listens on http://127.0.0.1:8888. + +## Sending a POST Request +To calculate the production plan, you need to send a POST request with a JSON payload. +You can use the curl command to send a POST request with the JSON payload + +For exemple, if you have a json file named payload3.json and containing all your inputs : + +curl -X POST http://127.0.0.1:8888/production_plan -H "Content-Type: application/json" -d @payload3.json + +## Viewing Results +The results are saved in a "production_plan_results.json" file.