Correcting errors in Mosel models



The parser of Mosel is able to detect a large number of errors that may occur when writing a model. In this chapter we shall try to analyze and correct some of these. As a next step, we also show how to obtain information for dealing with run time errors.

Other types of errors that are in general more difficult to detect are mistakes in the data or logical errors in the formulation of Mosel models—you may use the Mosel Debugger (see Section The Mosel Debugger) to trace these.

Correcting syntax errors in Mosel

If we compile the model poerror1.mos

model `Plenty of errors'
 declarations
  small, large: mpvar
 end-declarations

 Profit=   5*small + 20*large
 Boxwood:=   small + 3*large <= 200
 Lathe:=   3*small + 2*large <= 160

 maximize(Profit)

 writeln("Best profit is ", getobjval
end-model

we get the following output:

Mosel: E-100 at (1,7) of `poerror.mos': Syntax error before ``'.
Parsing failed.

The second line of the output informs us that the compilation has not been executed correctly. The first line tells us exactly the type of the error that has been detected, namely a syntax error with the code E-100 (where E stands for error) and its location: line 1 character 7. The problem is caused by the apostrophe ` (or something preceding it). Indeed, Mosel expects either single or double quotes around the name of the model if the name contains blanks. We therefore replace it by ' and compile the corrected model, resulting in the following display:

Mosel: E-100 at (6,8) of `poerror.mos': Syntax error before `='.
Mosel: W-121 at (6,29) of `poerror.mos': Statement with no effect.
Mosel: E-100 at (10,16) of `poerror.mos': `Profit' is not defined.
Mosel: E-123 at (10,17) of `poerror.mos': `maximize' is not defined.
Mosel: E-100 at (12,37) of `poerror.mos': Syntax error.
Parsing failed.

There is a problem with the sign = in the 6th line:

Profit=  5*small + 20*large

In the model body the equality sign = may only be used in the definition of constraints or in logical expressions. Constraints are linear relations between variables, but profit has not been defined as a variable, so the parser detects an error. What we really want, is to assign the linear expression 5*small + 20*large to Profit. For such an assignment we have to use the sign :=. Using just = is a very common error.

As a consequence of this error, the linear expression after the equality sign does not have any relevance to the problem that is stated. The parser informs us about this fact in the second line: it has found a statement with no effect. This is not an error that would cause the failure of the compilation taken on its own, but simply a warning (marked by the W in the error code W-121) that there may be something to look into. Since Profit has not been defined, it cannot be used in the call to the optimization, hence the third error message.

As we have seen, the second and the third error messages are consequences of the first mistake we have made. Before looking at the last message that has been displayed we recompile the model with the corrected line

Profit:=  5*small + 20*large

to get rid of all side effects of this error. Unfortunately, we still get a few error messages:

Mosel: E-123 at (10,17) of `poerror.mos': `maximize' is not defined.
Mosel: E-100 at (12,37) of `poerror.mos': Syntax error.

There is still a problem in line 10; this time it shows up at the very end of the line. Although everything appears to be correct, the parser does not seem to know what to do with maximize. The solution to this enigma is that we have forgotten to load the module mmxprs that provides the optimization function maximize. To tell Mosel that this module is used we need to add the line

uses "mmxprs"

immediately after the start of the model, before the declarations block. Forgetting to specify mmxprs is another common error. We now have a closer look at line 12 (which has now become line 13 due to the addition of the uses statement). All subroutines called in this line (writeln and getobjval) are provided by Mosel, so there must be yet another problem: we have forgotten to close the parentheses. After adding the closing parenthesis after getobjval the model finally compiles without displaying any errors. If we run it we obtain the desired output:

Best profit is 1333.33
Returned value: 0

Correcting run time errors in Mosel

Besides the detection of syntax errors, Mosel may also give some help in finding run time errors. It should only be pointed out here that it is possible to add the flag -g to the compile command to obtain some information about where the error occurred in the program, resulting in a command sequence such as

mosel -c "cl -g mymodel.mos; run"

or short

mosel -c "exec -g mymodel.mos"

Xpress-IVE compiles by default with this debug flag.

Also useful is turning on verbose reporting, for instance

setparam("XPRS_VERBOSE",true)
setparam("XPRS_LOADNAMES",true)


If you have any comments or suggestions about these pages, please send mail to docs@dashoptimization.com.