Looking for infeasibilities in Gams

A good CGE modelers always checks the calibration of a CGE model using a simple trick: If you set the iteration limit of the model to zero and you try to solve the model, GAMS should find a solution if you correctly initialized and wrote down your model equations with the use of the benchmark data. Often, the calibration is not done properly and this can be seen (using the iteration set to zero) by looking at the infeasibilities of the equations and the variables.

If you use MPSGE to write down your model, the infeasibilities are easily interpreted. If the activity variables show infeasibilities the corresponding zero-profit condition is not met, if the price variables show infeasibilities the corresponding market clearing condition is not met. Either the equation, the initialization or the data is wrong.

Looking for infeasibilities in the listing can be daunting. A simple model can have hundreds of variables and finding the infeasibilities can become time consuming. Therefore, I wrote a small Python script that uses some regular expression magic to find the infeasibilities and show them. Below is an example. It shows all variables with an infeasibility greater than 1E-5. The script runs very fast.

Feel free to use and/or improve the code (I just started learning Python). The script looks like this (assuming that the listing file is called “BenchF.lst”):

Why I don’t like Matlab

Working with a group of researchers can be very interesting but also very frustrating if you have to use their software tools. In my case, I have to use Matlab. If you are used to R, Python, and Gams this is more than a nightmare. This post is after a frustrating day with Matlab.

Most of my researcher colleagues use in their Matlab code matrices, so no row names or column headings. What you see are just numbers. If you want to know what the number represents, you need to look at vectors that contain the sets/row names/headers. Some of my colleagues have mercy with me and use (like I do) tables. However, that is not the end of the frustration. If you want to define a table, you write something like

Now, if you believe that adding column names would need something like

you are wrong. You should use ‘VariableNames’.

Other stuff that is frustrating:

  • Reading from an excel sheet using xlsread: if you have some empty cells at the beginning of the last row, Matlab just leaves them out. In order to have this line read properly, you must use the following three lines of code (look it up in the documentation and there is no clear indication that you only can read numbers directly without NaN directly:
  • Export a table to excel: the column headings from the table are used, but the row names not. The row names have to be exported separately.

The good thing: you really learn to love R, Python, and Gams more than anything else. For Matlab users: never look over the shoulder of someone using other software 🙂

Showing a beamer presentation with preview of next slide

If you use LaTeX with the package Beamer instead of Powerpoint to make presentations, you will probably miss the possibility to present a slide and at the same time have a quick glance on the next slide (Powerpoint has this great feature).

Searching the internet, I found the following solution: pympress:

“It is designed to be a dual-screen reader used for presentations and public talks, with  two displays: the Content window for a projector, and the Presenter window for your laptop. It is portable and has been tested on various Mac, Windows, and Linux systems.”

You can download it even on your Windows computer and it works like a dream: Just start it and open a Beamer-pdf-file, set your screen-mode to extend, move the second window of pympress to the main screen and keep the “presenter window” on your desktop. The presenter window looks like this:

image

Aspell does not work anymore with the new Emacs 26.1 for Windows: hunspell to the rescue

I downloaded the newest version of Emacs (26.1) from Vincent Goublet’s homepage., and run into the problem that my spell-checking of documents doesn’t work anymore. I use ispell and Aspell but the new version of Emacs needs Aspell 6.0 which is not available for Windows. After some googling, I found a good replacement: hunspell. Now I can check my spelling again:

image

I added the following code in my .emacs file (I found it somewhere on the internet).


(setq ispell-program-name "C:/Program Files (x86)/hunspell/bin/hunspell")
;; "en_US" is key to lookup in `ispell-local-dictionary-alist`, please note it will be passed   to hunspell CLI as "-d" parameter
(setq ispell-local-dictionary "en_US") 
(setq ispell-local-dictionary-alist
     '(("en_US" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil ("-d" "en_US") nil utf-8)))

Drawing nice side-by-side trees in LaTeX

In my work I often have to describe the model I use. For this it is almost inevitable to draw some hierarchical production functions. The following shows you how to draw two production functions side by side using Tik/. TikZ is a German acronym and stands for “TikZ ist kein Zeichenprogramm” (TikZ is not a drawing program) and produces high-quality drawings in LaTeX.

This is how the two production function look in the paper:

2017-12-06 12_04_12-Report top-down modeling.pdf - SumatraPDF

I use the Latex package subcaption (as the package subfigure is deprecated) to get the two production functions side by side. You further need to load Tikz and Tikz-qtree.

Some remarks:

  • The following line starts the left panel of the figure. I set the width to 0.45 of the text width, so both pictures can fit easily. Furthermore, I use the option “t”, so that both figures align at the bottom.
  • The following code starts the TikZ picture with some style settings. The size of the picture is slightly reduced
  • The direction of growth for the production function is down. I change this direction to “up” to add the top of the picture.
  • Each nest starts with a top node by adding a dot right after the square parenthesis, like: [.{your text}. Every child is added by some text between two curly brackets like

Tables with footnotes in LaTeX

Once in a while, I need a table in LaTeX with some footnotes below the table. Doing an internet search I found a nice solution using the package threeparttable. Here is a small example with the nuclear power plants in Switzerland:

image

The LaTeX code for this table is self-explaining (note the special structure: table – threeparttable – tabular):

Efficient desktop environment under Windows 10 using virtual desktops and Vdesk

You probably know the feeling: You are working in your editor and want to jump to Emacs or another program opened on your computer. You Alt-Tab your way through Chrome, Word, Tetris, and lots of other programs and then you move to fast and just click past Excel. Here is a typical desktop when I am working.

2017-05-07 16_19_35-

You could use your mouse and just click on the window showing when you press Alt-Tab or click on the small icon in the task bar. But I don’t like using the mouse and love to have everything below my finger tips.

Windows 10 has a nice feature to have virtual desktops. You can have multiple desktops with specific programs opened. For example, one virtual desktop with outlook, skype and other programs relating to communication, one with your code editor for programming and writing as well as Excel, etc. Here is an introduction to working with virtual desktops.

One problem is that you can’t save these virtual desktops, so you have to install them again when you restart your computer. Vdesk is a free, nice utility to do this for you. Just install it and write a small script (you can find all the technicalities here. Just save the code in a batch file and add it to your autostart folder. This folder can be found by running the command “shell:autostart”:

2017-05-07 16_42_12-Run

Here is a small example of a script (“vdeskstart.bat”) that produces two virtual desks:

vdesk create:2
desk on:2 run:”C:\emacs\bin\runemacs.exe”
timeout /T 25
vdesk on:1 run:”C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE”

(the timeout is necessary as Emacs needs about 25 seconds to start).

This creates at start-up two virtual desktops and opens Emacs and Chrome in the first and Outlook in the second virtual desktop. You can now work in a virtual desk with your favorite coding tools, switch easily if you want to do some emailing. The coding virtual desktop now only has the programs in the background you really need.

Jumping to the other virtual desktop is easy. Just hit Ctrl-Windows and arrow left or right and you cycle through the desktops. Or you hit on the task view icon in the task bar and click, and you will see your virtual desktops you can choose from:

2017-05-07 16_30_00-Posteingang - renger@vannieuwkoop.ch - Outlook

More on problems with HiRes screens

In one of my previous posts, I showed how to adjust the properties of the executable of a program under Windows 10 that has a fuzzy look: Right-click on the executable and choose “”the “ Compatibility” tab where you have to activate the “Disable display scaling on high DPI settings”. This trick works fine for non-windows programs. However, Outlook or Excel still look terrible.

If you go to http://windows10_dpi_blurry_fix.xpexplorer.com/ you can download a free, small utility, where you can set your overall DPI-Settings back those from Windows 8 (which doesn’t have this problem).

2017-02-01 13_50_48-XP Explorer - Windows 10 DPI Fix

Just click on “Use Windows 8.1 DPI scaling (our fix)” and you are set.

Generating headings for project files

I use the free software AutoHotkey (AHK) for automation of many things. One of those things is generating headings for my project file. I developed a simple AHK-script that asks me for the project name, the topic and then inserts the heading with additional information like my name, my E-mail address and a keyword for subversion.

The script is quite simple and I have adopted it for headings for several project files (tex, R and gams). The only difference being the characters used to comment out a line.

Here is the script

The line with ::tplt:: shows the characters that when typed start the template script (in this case for a tex-file). The lines with “Inputbox” produce a box where I can enter information

message

In this case, I enter the project name. What follows is the input box for the subject and then some additional information. Note, that the command “send” needs a variable (between %-values), therefore, everything  without variables should be on the same line.

Here the result for a new gams project file:

gams

You can easily adapt this script for your own use.

Blurry look of programs on your high display screen

My new ThinkPad T460s is lighter (1.4kg instead of 1.8kg), thinner and has more memory (20Gb) than my previous one (T430s). It has a much better screen with 1920 x 1080 pixels. This last feature is nice, but some of my programs get a fuzzy look as they are not yet adapted to this kind of display.

For example, Evernote is not ready for scaling on high DPI settings. If you look in the discussions on this issue, for programmers this seems to be a rather daunting task. Evernote explicitly does not promise anything in that direction (and it is an issue since 2013…).

The problem is resolved quite easily (although you probably have to do it again after updating the program):

  • search for the executable on your computer (in this case Evernote.exe)
  • right click on the file name and choose properties
  • in “properties” choose “compatibility” and (if the program is set up for all users), choose “Change settings for all users” and click on “Disable display scaling on high DPI settings”.

settings

Afterwards, your Evernote (or other programs with the same problem) are crystal clear again.