GAMSPy model building Topic is solved

Problems with modeling
Post Reply
Maclean16
User
User
Posts: 5
Joined: 4 months ago

GAMSPy model building

Post by Maclean16 »

Hello dear support team!

I'm trying to build a model using GAMSpy environment.

Code: Select all

e1=Equation(container=cep, name="e1", type="regular", domain=[tech_demand], description=" variable cost for demand technology",)
e1=Equation(container=cep, name="e1", type="regular", domain=[impact, tech_demand])
        e1["var", impact, tech_demand]=COST[impact, tech_demand]==sign_generation* Sum([t,k,node], 
                                                                  GEN[tech_demand,carrier_demand , t,k,node ]
                                                                  *ts_weights[k]*ts_deltas[t,k]*costs[tech_demand, node, year, "var", impact])*scale[':GEN']/scale[':COST']
Without defining the "e1" equation, the code doesn't display an error. But the following error appears in debugging mode when I define the equation:
Traceback (most recent call last):
File "c:\Users\Lere\Codes\InvestmentPY\main.py", line 41, in <module>
cep_result = run_opt(ts_input_data, cep_data, optimizer, descriptor="simple storage", transmission=True, storage_type="simple", conversion=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\Lere\Codes\InvestmentPY\opt_problems\run_opt.py", line 34, in run_opt
run_opt_main(ts_data, opt_data, config, optimizer)
File "c:\Users\Lere\Codes\InvestmentPY\opt_problems\run_opt.py", line 158, in run_opt_main
e1[tech_demand]=COST[impact, tech_demand]==sign_generation* Sum([t,k,node],
~~^^^^^^^^^^^^^
File "C:\Users\Lere\anaconda3\Lib\site-packages\gamspy\_symbols\equation.py", line 215, in __setitem__
self.container._run(is_implicit=True)
File "C:\Users\Lere\anaconda3\Lib\site-packages\gamspy\_container.py", line 327, in _run
self._job = GamsJob(
^^^^^^^^
File "C:\Users\Lere\anaconda3\Lib\site-packages\gams\control\execution.py", line 876, in __init__
file.write(source)
File "C:\Users\Lere\anaconda3\Lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode character '\u2212' in position 276: character maps to <undefined>
What could be the main cause ? Thank you!
Last edited by Maclean16 4 months ago, edited 1 time in total.
msoyturk
User
User
Posts: 3
Joined: 4 months ago

Re: GAMSPy model building

Post by msoyturk »

Looks like an encoding issue. Is there a chance that you can share your code with us so that we can reproduce the issue? It would also be good if you can tell us the operating system you use so that we understand the default encoding type of your system or you can just get the encoding type by running:

import locale
print(locale.getpreferredencoding())
Maclean16
User
User
Posts: 5
Joined: 4 months ago

Re: GAMSPy model building

Post by Maclean16 »

Here is the results of your suggestion: cp1252.
I'm working on windows.
Here is the link to the code: https://github.com/Lere16/Example.git
Last edited by Maclean16 4 months ago, edited 1 time in total.
Online
User avatar
bussieck
Moderator
Moderator
Posts: 1043
Joined: 7 years ago

Re: GAMSPy model building

Post by bussieck »

Thanks for sharing the code. The trouble is that the description of set "impact" had some funny characters which don't show when you look at the file in github ("impact categories like EUR or USD, CO 2 − eq") but do when you check this out on Windows and look at it in e.g. notepad++ (with ANSI encoding): "impact categories like EUR or USD, CO 2 − eq". This throws Python off because it can't decode this. After I removed this "funny" character sequence. I did not get this error anymore. But I got logical errors with eq1. You use some sets on the right, namely carrier_demand and year that are neither control by the equation index, nor by the Sum. Moreover, the first index in variable COST (from set account) was missing. Also, you indexed the equation with set tech_demand and use this e.g. in index position 3 in variable COST. COST's domain is [account, impact , tech], so you either change the domain of COST or, probably better, make tech_demand a subset of tech. I "corrected" this and use carrier_demand and year as equation index. Hence I also had to make carrier_demand a subset of carrier. Here are the changes that make this run:

Code: Select all

tech_demand=Set(cep, name= "tech_demand", domain=[tech], records =sets["tech"]["demand"], description = "demand technology")
carrier_demand=Set(cep, name= "carrier_demand", domain=[carrier], records =techs['demand'].input['carrier'], description = "demand technology")
e1=Equation(container=cep, name="e1", type="regular", domain=["var",impact, tech_demand,carrier_demand,year])
e1['var', impact, tech_demand,carrier_demand,year]=COST['var',impact, tech_demand]==sign_generation*Sum([t,k,node], 
                                                                  GEN[tech_demand,carrier_demand , t,k,node ]
                                                                  *ts_weights[k]*ts_deltas[t,k]*costs[tech_demand, node, year,'var', impact])*scale[':GEN']/scale[':COST']
-Michael

PS gamspy@gams.com would love to hear about your experience with GAMSPy.
PPS If you really meant to have a cp1252 encoding source file, you should add "# -*- coding: cp1252 -*-" as the first line to your source.
Maclean16
User
User
Posts: 5
Joined: 4 months ago

Re: GAMSPy model building

Post by Maclean16 »

Thank you so much :) ! It works perfectly.
Maclean16
User
User
Posts: 5
Joined: 4 months ago

Re: GAMSPy model building

Post by Maclean16 »

Dear support team,

For this small system example (demand, dispatchable generation, non dispatchable generation), I'd like to know the capacities of the different sources to satisfy the demand. In any case, the optimization should start with the existing "ex" capacities ( equation "e25" ). In the gdx_out file, I noticed that the "ex" values of the "CAP" variable are not fixed. I force the values with the instruction

Code: Select all

CAP.fx[tech_ex, "ex", node]==EX[tech_ex,node]/scale[':CAP']
, but the result is always the same and the objective function has the value 0, no capacity is added.

I'd like to know if this way of setting a variable in GAMSPy is correct, or if the balancing equation ("e27") is wrong. Just a reminder that demand is included in the GEN variable with negative values.

code link: https://github.com/Lere16/Example.git
You can find the optimization model in "opt_problems\run_opt.py"
Maclean16
User
User
Posts: 5
Joined: 4 months ago

Re: GAMSPy model building

Post by Maclean16 »

I finally found the error. COST should not be an optimization variable. In addition, the type of GEN should be "Positive". For my first experience with GAMSPy, I find that this environment is very fast in model building (little time required), which saves me quite a bit of time in execution.
Happy new year :ugeek: !
Post Reply