cs1951g Optimization Methods in Finance Riondato/Upfal
Due: Friday, Feb. 19, 2016 at 2 p.m.
• TA office hours are Tuesdays and Thursdays 7–9pm in CIT 219.
• If you absolutely can’t go to office hours, you can contact the staff at cs1951gtas@cs.brown.edu
.
• If you work together on the homework, include your partners’ names at the top of your handin. However, your solutions must be written independently. The full collaboration policy can be found in the syllabus.
In this small project, you will create your own LP solver by implementing a parser for a subset of the AMPL language, the Simplex algorithm, and some additional features.
AMPL language syntax.
The AMPL language is a powerful and featurerich modeling language for optimization problems. In this project, you will implement a parser for a minimal subset of the language, sufficient to specify simple linear programs. A LP is specified in a plain text file (usually with the .mod
extension), containing a sequence of one or more variable declarations (one per line), followed by one declaration of an objective (on a separate line), followed by one or more specifications of constraints (one per line). The syntax that your parser must support is the following.
Variables: A variable is declared in the following forms: var VARIABLE NAME >= 0; if its domain is
R
+
; var VARIABLE NAME; if its domain is
R
; where VARIABLE NAME is any sequence of letters and numbers. Each variable must have a unique name. The following are two examples of variable declarations: var X1; var X2 >= 0;
cs1951g
Objective: The objective of the LP is declared as follows:
<objective> OBJECTIVE NAME: <linear expression>; where <objective> is either minimize or maximize , OBJECTIVE NAME is any sequence of letters and numbers, and <linear expression> is any sequence of terms in the form
<sign> <number> VARIABLE NAME where <sign> is either “+” or “-”, <number> is as before and represent a coefficient by which the variable named <VARIABLE NAME should be multiplied. Variables must have been declared before they can be used in the objective. The following is an example of objective declaration: minimize total cost: + 2 X1 + 2 X2;
Constraints: A constraint is defined in the following form: subject to CONSTRAINT NAME: <linear expression> <ineq> <number>; where CONSTRAINT NAME is any sequence of letters and numbers, <ineq> is one of >= , <= , or = , <number> is any real (in base 10 and in “normal”
(i.e., not scientific) notation), and <linear expression> is a linear expression as described in the specification of object declaration above.
The sequence of terms is terminated with a semicolon “;”. Variables must have been declared before they can be used in a constraint. Each constraint must have a unique name. The following are some examples of constraint definitions: subject to C1: + 3 X1 - 2 X2 >= 0; subject to C2: + 3 X1 - 2 X2 = -2.3;
The following is an example of a full LP: var X1 >= 0; var X2; maximize total cost: + 0.5 X1 - 2 X2; subject to C1: + 3 X1 - 2 X2 >= 2.3; subject to C2: - 4 X1 + 7.3 X2 >= 6;
Solver.
You will implement the Two-Phases Simplex algorithm with Bland’s antycling rule. You are given complete freedom about the details of the implementation, with the exception that you can not use third party libraries
2
cs1951g or packages that implement the Simplex algorithm or parts of it (e.g., finding a basic feasible solution).
Interface specifications.
We will run your code as
./runMyLPSolver <command> MODEL runMyLPSolver must be the name of the script or executable that runs your
LP solver, MODEL is a file with an LP program specified in the subset of the
AMPL language described above, and <command> is one of the following:
• bfs : print a basic feasible solution for the LP in the MODEL file. The output must follow the following format:
VARIABLE1 NAME: <number>
VARIABLE2 NAME: <number>
...
OBJECTIVE NAME: <number> where the number after each variable name is the value assigned to its value, and the number after the objective name is the value of the objective function at the given assignment. If the problem is infeasible, the output must be “ Infeasible problem. Exiting.
”.
• dual : print the dual problem of the one specified in the MODEL file.
The output must be in a format that can be then used as input to runMyLPSolver , i.e., the dual should be specified according to the subset of the AMPL language described above. You can use arbitrary names for the objective, the constraints, and the variables.
• solve : solve the LP specified in the MODEL file using the Two-Phases
Simplex algorithm. The output must follow the same format as for the bfs command, with the additional requirement that if the problem is unbounded, the output must be “ Unbounded problem. Exiting.
”.
• standard : print the LP in the MODEL file in standard form. The output must be in the same format as for the dual command.
You must not assume that <command> is one of the above. If any other string is given as <command> , runMyLPSolver must fail gracefully with output:
“ Unknown command. Exiting.
”.
3
cs1951g
You must not assume that the MODEL file is not malformed (i.e., that it correctly follows the language specification given above). If the MODEL file is malformed, runMyLPSolver must fail gracefully with output: “ Malformed input file. Exiting.
”.
Execution requirements.
Your code must run out-of-the-box on any departmental machine, e.g., any of the Sun Lab machines. You can not require the installation of any additional software. If a Makefile file is present in your handin directory (and only in this case), we will run “ make ” before running our tests.
We’ve provided a bit of Python support code for parsing and outputting
AMPL code. You can download it here.
Test models.
You will also submit at least four test models (and up to nine) and their corresponding output. One test model must be of a feasible problem with a bounded optimal solution. One test model must be of an infeasible problem. One test model must be of an unbounded problem. One test model must be of a malformed model not respecting the required format.
You will put your test models in a directory tests , and sequentially named
X.mod
with X starting from 1. You will put the expected outputs in another directory called testsouts (at the same level of test ) and the outputs must be sequentially named X.out
. We will run your code against all the tests we collect from all the students.
Submission Submit your code and your tests models and output using the cs1951g handin script: cd into the main code directory and run cs1951g handin lp project .
You must submit the source of your code. If compilation is necessary, you should have a Makefile file in your submission, and the compilation must happen when running ” make “ (we will not run any other command).
Evaluation You will be evaluated on the correctness of your code, on its ability to solve a number of tasks (e.g., different LPs in different model files and different commands), and of handling unexpected situations. We will inspect your code, so please use a sufficient number of comments for us to understand the code.
4