Smart control of Heat pump

I am controlling my heatpump at the moment by increasing/decreasing the heat curve.
The heatpump is a panasonic.

In the screenshot attached, you can see how much energy the heatpump consumes, what the current electricity price is (changes every hour) and you can see the temperature development in the heating circle over time.

Sidemark: The electricity price is there only as an example. I have another function to calculate the COP. In the final stage, I want to replace electricity price by the “electricity costs”, which is the electricity price divided by the relevant COP.

The aim is to minimize the electrivity costs.

From a mathematical point of view: How do I calculate an optimal heating curve for every hour?
What algorithms are there to tackle this? Any hint here may help me.

What I can imagine:

  • Calculate cooling rate based on outside ambient temperature.

Then:

  1. Brute force: Calculate energy cost for every day based on target temperatures. Set target temperatures for every hour to 30 to 55 °C. The costs have to be calculated on COP and electricity costs. The cooling rate then defines when the heat pump has to work again.

  2. Set a standard heat curve. Increase the curve +5°C for the 6 cheapest hours and decrease for the most expensive hours. Thats my current solution.
    Disadvantage: If the price does not vary too much, you waste efficiency by heating too high. Also if all expensive hours are concurrent, the heat pump is of for quite some time. Also it happens that for several concurrent cheap hours, the water is heated and cools down at a low level just before the price is up again, so when the price is very high, it need to heat again. Happened in the graph at 8:00.

  3. Look for a maximum in price. Look for cheaper hours before that price maximum, increase the temperature by X°C to overcome the price maximum.