How to specify GAMS solver-specific options through Pyomo? - pyomo

I am trying to solve an MINLP problem with the ANTIGONE solver (licensed in GAMS), and I am having difficulties to change the advanced settings of the solver.
First, I call the solver from Pyomo as follows.
solver = pe.SolverFactory('gams')
solver.options['mtype']= "minlp"
solution = solver.solve(model, solver = 'antigone')
This part works as it should, however ANTIGONE cannot close the optimality gap, so I'd like to change some of the more advanced options that are provided here https://www.gams.com/latest/docs/S_ANTIGONE.html.
So what I first tried was to change the solver call to the following line.
solution = solver.solve(model, solver='antigone', add_options=['option number_of_partitions 2;'])
However, it looks like add_options is only for the built-in GAMS options, and not for the solver-specific ones.
As a side note, when using these advanced solver options in GAMS, the standard procedure is to create an options file (i.e. antigone.opt), where we specify the desired options.
number_of_partitions 2
antigone.opt is saved under the same directory, and the .gms file calls this file with the GAMS_MODEL.optfile; line.
Going back to my issue, when modifying this option from Pyomo, I tried
solution = solver.solve(model, solver='antigone', add_options=['GAMS_MODEL.optfile;'])
but the problem is that Pyomo creates a temporary file to solve the problem, and hence I cannot add the file antigone.opt before it starts solving.
Therefore, I can see two options to go: (i) I can find a way to create the antigone.opt file in the temporary file through Pyomo before the solver starts solving (less desirable, but it should work), or (ii) I should directly change the option from Pyomo (preferred).
Any help would be much appreciated and thanks in advance for your time!

Maybe this answer arrives a little late for you however I think this might help other people.
I had the same issue you had. After a long crusade and by putting many Stack Overflow posts together, I finally made it work using Pyomo only.
The line,
solution = solver.solve(model, solver='antigone', add_options = ['GAMS_MODEL.optfile;'])
should be replaced by,
solution = solver.solve(model, solver='antigone', add_options = ['GAMS_MODEL.optfile = 1;','$onecho > antigone.opt', 'number_of_partitions 2', '$offecho'])
So the first option specifies the use of the antigone.opt file and the remaining lines tell GAMS to create and write in the antigone.opt file.

I had a similar problem when using pyomo->gams->knitro. The code below worked for me. My code was in /Users/myDir/, and I created the directory "tmp" there. Then I instruct pyomo to use that and the file knitro.opt is placed there.
opt = SolverFactory('gams')
opts = {}
opts["solver"] = "knitro"
with open("tmp/knitro.opt", "w") as f:
f.write("algorithm 2\n")
addOp = ['GAMS_MODEL.optfile=1;']
opt.solve(instance, io_options=opts, load_solutions=True, tee=True,
add_options=addOp, tmpdir='/Users/myDir/tmp')

Related

Macro within a loop and include

In Stata, I'm trying to use the command include to run several regressions in one do file. The overall goal is to help in forecasting Natural Gas production.
I have a do file for each product and basin that I'm interested in. Within each do file I am running several versions of the regression based on data available at specific times (e.g. the first set of regressions is for product type 4, in basin 2, with information available in June 2020).
The regression command within the do file looks something like this:
include "gas\temp\NG var.doh"
foreach time in dec14 dec15 dec16 dec17 previous current {
arima price_`perm4type' `perm4pvars' if tin(,`yq_`time'') , ar(1) ma()
}
I have the perm4pvars defined in the file NG var.doh like this:
local perm4pvars txfreeze PNGHH_`time' d.CPIE_`time' d.IFNRESPUOR_`time' POILWTI_`time'
When I run my do file the time from my doh file doesn't show up. So I get an error message: "PNGHH_ is an ambiguous abbreviation"
How can I get the time to show up in my regress command?
I did try doing this in my .doh file
foreach time in dec14 dec15 dec16 dec17 previous current {
local perm4pvars txfreeze PNGHH_`time' d.CPIE_`time' d.IFNRESPUOR_`time' POILWTI_`time'
}
I got it to run, only for time=current
The relationship between the included .do file and the main .do file is not symmetric.
The point of include is that the included do file is read into the main do file. But the included do file knows nothing about any files it is included in. So definitions in the included do file may not usefully refer to definitions in the main .do file, or to any others, unless they in turn are included, or so I presume.
That explains why your reference to local macro time in the included file doesn't do what you want. It's not illegal in any do file or Stata program to refer to a local macro that doesn't exist (meaning, is not visible from the file) but as here the consequences may not be what you want.
Imo your issue is more basic than what the above answer suggests, in that you want to use locals from a loop outside of it. Or, in the case where you got it to run for time=current, misunderstanding what the loop does. Might be good to get a good understanding of the functioning of loops first, also since you seem to be using multiple other loops that are not detailed in your question and where I can only assume those are specified correctly.
Assuming the gas\temp\NG var.doh file only holds the line defining the local, i.e. local perm4pvars txfreeze PNGHH_`time' d.CPIE_`time' d.IFNRESPUOR_`time' POILWTI_`time', a way to get it working the way you want (or at least how I think you want it, since you do not detail the specifications you want to achieve with your loop) is to move the include inside the loop, changing your code to:
foreach time in dec14 dec15 dec16 dec17 previous current {
include "gas\temp\NG var.doh"
arima price_`perm4type' `perm4pvars' if tin(,`yq_`time'') , ar(1) ma()
}
This way, the line in the included .do file can use the local time from the loop. In the case there is more in the .doh file you would have to change your code a bit more to get it to work, however I can't help you with that unless you give me more information about the structure of the code and what you want to achieve with it.

Having a beamer_presentation and a pdf_book in the same Bookdown project

In my Bookdown project, I have both a bookdown::pdf_book and a beamer_presentation specificed in the _output.yml. (Why? Here is an example, and here is the explanation.)
The problem is that pdf_book seems to respect the output_dir specified in _bookdown.yml, but not the beamer_presentation. Thus, when hitting the Build Book button, the presentation won't get into the docs directory, it'll appear in the base directory.
Is there any way to make beamer_presentation respect the output_dir specification?
(Also, it'll mean that something has to be done with the filenames, as by default the the names would be the same.)
EDIT: I realized that using bookdown::beamer_presentation2 instead of beamer_presentation will solve the problem, as it'll respect the output_dir. But I'd call it a partial answer, as my fears in the last sentence realized: this will simply overwrite the pdf_book (as they'll indeed have the same name), so it is still not really working...
Yes, you need to use bookdown::beamer_presentation2, which will respect the output_dir setting in _bookdown.yml.
Regarding to your second problem (two formats having the same output filename), there isn't a nice solution at the moment if you only want to click the Knit button in RStudio---you have to call rmarkdown::render() and specify the output filename in the call, e.g.,
rmarkdown::render('file.Rmd', 'bookdown::pdf_book', output_file = 'book.pdf')
rmarkdown::render('file.Rmd', 'bookdown::beamer_presentation2', output_file = 'beamer.pdf')

Pyomo gams writer

I have the following problem. I want to solve a number of MINLP's in the following manner:
Create .gms file using pyomo
Solve model on a server with the gams license and solver's like BARON and SCIP
Use the solution of the current MINLP for the next MINLP
Go to 1.
Now I have some questions:
What is the best way to read the solution of the previous MINLP (right now I'm reading the .dat file)
How can I change the default gams/mipstart option to a value of 3? (I tried: io_options['add_options']=['option gams/mipstart = 3;'] but didn't work)
I don't know anything about the Pyomo/GAMS link.
But the "gams/mipstart = 3" line should go into a GAMS/SCIP options file (probably scip.opt, see also https://www.gams.com/25.1/docs/UG_SolverUsage.html#BASIC_USAGE_SOLVER_OPTION_FILE) and you would need to tell Pyomo to tell GAMS to use this option file.

How can I give a custom file names to a code file in ideone.com?

I am using ideone as online c++ compiler. When I save a code, ideone gives a random name such as
lnzr40
which might be confusing later on (when I want to open a specific code). I want to change that name or give a custom name at the time of writing/saving the code. How can I do that?
Edit: Labeling or creating custom links may be slightly useful but they are not what I need exactly! I need to modify the file name.
Thanks!
I am no ideone power-user, but there does not seem to be any simple way to change the name of a source code file.
However, there is a note field immediately to the right of the file name in the list of files. You could use this field to enter the appropriate file name:
This seems to be a case where a link-only answer is appropriate ;)
http://tinyurl.com/Yes-it-is-possible

How to override the default text in MATLAB

In MATLAB, when you click File -> New -> Function M-File, you get a file with the following contents:
function [ output_args ] = Untitled( input_args )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
end
Is it possible to override this behaviour, and specify your own text?
(The motivation is that I'm trying to persuade my colleagues to document their m-files more thoroughly, and having default text for them to fill in might encourage them.)
I didn't even know File->New->Function did that.
The way I solved the issue was to write a function that you call via
>>newFunction myNewFunctionName
It then
pops up an inputdlg window, which asks the user for the synopsis and the H1 line and allows to already write help to explain input and output arguments. There, the user also selects whether myNewFunctionName is a function or a class in order to choose the right header and 'function call'
checks whether a function of the same name exists already
asks for a folder to save the function, and
opens the function in the editor
The header is set up so that it's easy to fill in info about input and output. It also automatically lists the username of the person who created the file as well as the date and the Matlab version.
EDIT
For new classes, the template function automatically makes sure that they subclass my general superclass that implements methods such as 'help' (which calls doc(class(obj)) )
Now if the template functionwould also write the algorithm part of the function, it would be really convenient. :)
EDIT2
Here's a link to the function on the file exchange.
I would suggest making your own default m-file template, called default.m for example, and placing it in a folder on the MATLAB path where your colleagues can access it. You should then set the file to be read-only. Your colleagues can then execute any one of the following commands in the MATLAB Command Window when they want to create a new function m-file:
open default.m
open('default.m')
edit default.m
edit('default.m')
The functions OPEN and EDIT will open a file in the MATLAB Editor. Since the file default.m is read-only, if anyone tries to save over it they will get a dialog box warning them as such and asking them to save to a new file (or overwrite it). That should keep them from accidentally modifying the template.
I searched through all text files starting from matlabroot folder, but could not find that template. Seems it's hard-coded, which is weird.
I like Jonas approach. As my two cents, you can download a function (not mine) doing similar things with some customization from here.
After more pondering, I've come up with a solution that I'm happy with, combining Jonas' and gnovice's answers. It's a function that creates a new m-file (with template documentation), and opens it in the editor. It is available from the Matlab Central File Exchange.