What Went Wrong?

Talharehman Mtrkt
2 min readJan 12, 2024

--

In the previous blog, I explained modeling and implementing a simple optimization problem — Day Ahead Energy Management of a residential building.

before moving forward, I want to showcase a “Simple Coding Mistake” that I was making. The code and results of the previous blog are already with the corrected version. Here I just want to highlight the mistake that may have gone unnoticed sometimes when you are dealing with larger problems and a Huge amount of Code.

Take a look at the below code snippet.

   
int chgeffin = 0.95; //battery effciency
int dischgeffin = 0.95; //battery effciency

These two lines have some mismatch of datatype, as you can see I am assigning an integer a floating point value. It might not throw an error but you will not get the correct output of the optimization problem, and in some cases “nan” objective value.

Here is the figure showing the output of both the corrected and the error case of declaring the charging efficiency using “int”.

Correct (left) and Incorrect (right)

When we assign an integer a floating point value, the compiler will truncate the decimal part and the variable will get the integer value of “0” in our case. And depending on the place where it is used you would get the incorrect or no valid solution.

One more thing I want to highlight here is about Github copilot. I personally recommend using it as it speeds up your coding process very much. However, you need to be careful in some cases like the one in this blog. Below is the figure of the suggestion made by the GitHub copilot.

Suggestion from GitHub copilot

Now here, when I made the first mistake of assigning an integer a floating point, it suggested the same for the second variable. Of course, the issue is not with the GitHub copilot as it follows what we have written already in our code base. But my point is to highlight the importance of being focused and attentive to such details when you are writing Code.

I hope this might be helpful, let me know in the comments below if you have any feedback.

Heads up for more blogs in the series!

--

--