Monday, October 23, 2017

Setting Up PGI Compilers Community Edition on Windows

1. Download and install from the PGI Compilers website: PGI Compilers Community Edition

2. Follow the set up instructions here: Installation Guide

3. Add some paths to the environment:

Under "path" variable: C:\Program Files\PGI\win64\17.4\bin

And then create a new varible called "PGROUPD_LICENSE_FILE" and add the path where license.dat is stored. In my case, it's: C:\Program Files\PGI\license.dat (must be the full path)

And we're done! :)

Wednesday, September 27, 2017

Setting Up CUDA on your local machine post-September 2017

1) Download and install Visual Studio 2017 (Community edition):

https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=Community&rel=15

(Another option (I did not try) : http://landinghub.visualstudio.com/visual-cpp-build-tools)

Then, in the installer, click "modify" and check "Desktop development with C++". Install.

2) Download CUDA toolkit 9.0: https://developer.nvidia.com/cuda-toolkit.
Install.

3) Add "cl.exe" to path. This can be found in:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX64\x64\cl.exe
4) On cmd, do:

>> nvcc test.cu -o test
>> test

We're done!

Monday, February 20, 2017

Best Tower of Hanoi Explanation

SOURCE: https://www.cs.cmu.edu/~cburch/survey/recurse/hanoiimpl.html


Up: Recursion. Prev: Recursion. Next: Program trace.

Writing a Towers of Hanoi program

Using recursion often involves a key insight that makes everything simpler. Often the insight is determining what data exactly we are recursing on - we ask, what is the essential feature of the problem that should change as we call ourselves? In the case of isAJew, the feature is the person in question: At the top level, we are asking about a person; a level deeper, we ask about the person's mother; in the next level, the grandmother; and so on.
In our Towers of Hanoi solution, we recurse on the largest disk to be moved. That is, we will write a recursive function that takes as a parameter the disk that is the largest disk in the tower we want to move. Our function will also take three parameters indicating from which peg the tower should be moved (source), to which peg it should go (dest), and the other peg, which we can use temporarily to make this happen (spare).
At the top level, we will want to move the entire tower, so we want to move disks 5 and smaller from peg A to peg B. We can break this into three basic steps.
  1. Move disks 4 and smaller from peg A (source) to peg C (spare), using peg B (dest) as a spare. How do we do this? By recursively using the same procedure. After finishing this, we'll have all the disks smaller than disk 4 on peg C. (Bear with me if this doesn't make sense for the moment - we'll do an example soon.)
  2. Now, with all the smaller disks on the spare peg, we can move disk 5 from peg A (source) to peg B (dest).
  3. Finally, we want disks 4 and smaller moved from peg C (spare) to peg B (dest). We do this recursively using the same procedure again. After we finish, we'll have disks 5 and smaller all on dest.

In pseudocode, this looks like the following. At the top level, we'll call MoveTower with disk=5, source=A, dest=B, and spare=C.
FUNCTION MoveTower(disk, source, dest, spare):
IF disk == 0, THEN:
    move disk from source to dest
ELSE:
    MoveTower(disk - 1, source, spare, dest)   // Step 1 above
    move disk from source to dest              // Step 2 above
    MoveTower(disk - 1, spare, dest, source)   // Step 3 above
END IF

Note that the pseudocode adds a base case: When disk is 0, the smallest disk. In this case we don't need to worry about smaller disks, so we can just move the disk directly. In the other cases, we follow the three-step recursive procedure we already described for disk 5.
If this doesn't make sense to you, maybe an example run will help. Even if it does make sense, run through the example to make sure you understand. Understanding the concept of recursion is important to understanding the rest of the course.
Next: Program trace.

Sunday, February 12, 2017

Mini Programming Tutorial for Python with Cplex

This code is from lpex1.py that you can find in ...\IBM\ILOG\CPLEX_Studio127\cplex\examples\src\python, minimized for demostrating purpose. 

Tutorial by IBM is here: https://www.ibm.com/support/knowledgecenter/en/SSSA5P_12.6.0/ilog.odms.cplex.help/CPLEX/GettingStarted/topics/tutorials/Python/eg_lpex1.html

1) Import statements

from __future__ import print_function
import sys
import cplex
from cplex.exceptions import CplexError

2) First, you initialize the problem information.
my_obj = [5.0, 4.0] (i)
my_ub = [cplex.infinity, 2.0] (ii) 
my_colnames = ["x1", "x2"] (iii)
my_rhs = [24, 6.0, 1.0] (iv)
my_rownames = ["c1", "c2", "c3"] (v)
my_sense = "LLL" (vi)
i. Objective constants
ii. Set the bound for each variables.
iii. Declare the variable names.
iv. RHS of constraints.
v. Constraint names
vi. For each contraint, specify the relationship with RHS. L/G/E for Less than, Greater than, etc (see source code for cplex.linear_constraints.add for more info)


2) Set up the problem as a function populatebyrow(prob) where prob will take an object of cplex.Cplex()
def populatebyrow(prob): 
i. Set type of problem.  For minimization, use prob.objective.sense.minimize. 
    prob.objective.set_sense(prob.objective.sense.maximize)
ii. Lower bound is 0.0 by default. Should be a line to set lower bounds if needed. For now, add the variables to the problem. 
    prob.variables.add(obj=my_obj, ub=my_ub, names=my_colnames)
 iii. Prepare lower bounds and upper bounds. In problem information initialization, for unbounded variables, we set cplex.infinity. However, here, for upper bound, since it applies to x2 only, we can also set ub1 to get_upper_bounds(1), retrieving upper bound for x2 only. 
    lbs = prob.variables.get_lower_bounds()
    ub1 = prob.variables.get_upper_bounds()
iv. Get the variable name.  
    names = prob.variables.get_names()
v. This part, set the rows as a pair of list of indices mapping to the values. I don't really get how the indices are determined but this should work for now.  
    rows = [[["x1", "x2"], [6.0, 4.0]],
            [["x1", "x2"], [1.0, 2.0]],
            [["x1", "x2"], [-1.0, 1.0]]
            ]
    prob.linear_constraints.add(lin_expr=rows, senses=my_sense,
                                rhs=my_rhs, names=my_rownames)
    cols = prob.variables.get_cols("x1", "x2")

3) Using the function:

my_prob = cplex.Cplex()
populatebyrow(my_prob)
my_prob.solve()
numrows = my_prob.linear_constraints.get_num()
numcols = my_prob.variables.get_num()
numrows = my_prob.linear_constraints.get_num()
numcols = my_prob.variables.get_num()
print()
# solution.get_status() returns an integer code
print("Solution status = ", my_prob.solution.get_status(), ":", end=' ')
# the following line prints the corresponding string
print(my_prob.solution.status[my_prob.solution.get_status()])
print("Solution value  = ", my_prob.solution.get_objective_value())
slack = my_prob.solution.get_linear_slacks()
pi = my_prob.solution.get_dual_values()
x = my_prob.solution.get_values()
dj = my_prob.solution.get_reduced_costs()
for i in range(numrows):
    print("Row %d:  Slack = %10f  Pi = %10f" % (i, slack[i], pi[i]))
for j in range(numcols):
    print("Column %d:  Value = %10f Reduced cost = %10f" %
              (j, x[j], dj[j]))
my_prob.write("lpex1.lp")

Saturday, February 11, 2017

Setting up CPLEX with Python

1. Get license for IBM ILOG CPLEX 12.7, download and install.
2. I use Python 3.5.3 .
3. Set environment path PYTHONPATH to the directory of the python cplex folder. In my case, it was C:\Program Files\IBM\ILOG\CPLEX_Studio127\cplex\python\3.5\x64_win64.
4. Run cmd as administrator. cd into the cplex folder. Run python setup.py install --home <directory to keep the cplex modules>. In my case, e.g:

From C:\Program Files\IBM\ILOG\CPLEX_Studio127\cplex\python\3.5\x64_win64:

...\> C:\Python35\python setup.py install --home C:\PythonCPLEX


Then run python, and import cplex.

Sauce(s) : The CPLEX Tutorial Setting up CPLEX with Python