Quick Search for Useful Linux Commands

On October 21, 2016, in Mac/Linux, by jild13
  1. Count the number of files in the current directory:
    ls -1 | wc -l
    ls -l | grep -v ^l | wc -l
    Reference: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x700.html
  2. Remove all the files with name XXX* in the current directory:
    find . -name “XXX*” -print0 | xargs -0 -I {} rm {}
  3. List the total size of each folder (-s is to list only total size):
    du -sh directory
    du -h directory

 

 

 

TO BE UPDATED

 

Basic Programming in Optimization

On August 24, 2016, in Lehigh ISE 406, Mac/Linux, by jild13

This tutorial include basics of using popular optimization solvers and modeling tools.

 

 

O. Login to Servers

It is easy and convenient to start and use AMPL on the servers (polyps) of Lehigh CORAL. To access lehigh servers, you need to ssh with your personal account (assigned by Lehigh ISE Administrator Aykut Bulut). The command to login to servers at Lehigh ISE on Linux/Unix based operating systems (e.g. Mac OS, Red Hat, Ubuntu, Debian etc.) is:

ssh 

For Windows users, please install PuTTY. Refer to CORAL User Guide and PuTTY website for installation and usage. It’s simple!

There are currently three types of machines with shared storage under Lehigh ISE Department.

“coral” is mainly for documentation usage or administrative usage which does not work with many solvers and software (probably some GNU commands and simple solvers works on it).

“shark” is mainly used for connecting to Lehigh internal servers via off-campus internet service. It is also lack of mainstream solvers and software.

“polyps” contains the main clusters which we should generally used for experiments and programming. This is only accessible via on-campus internet or Lehigh campus VPN. The polyps includes multiple clusters. You will be assigned one when you login. However, you can ssh into one specific cluster which is NOT recommended (except for particular use).

NOTICE: due to their usages and limited software, we do NOT recommend run programs using “coral” and “shark”. These two are used to connect to the Lehigh internal server “polyps”.

A list of the resources you can use on “polyps” is available here. Feel free to refer to Lehigh ISE/COR@L Wiki to know more about tools and systems available at ISE.

To log out the server, just simply type

logout

and enter.

Before the tutorial, please copy all the folder for your tutorial by entering

cp -r /tmp/ISE406T ./

 

 

I. AMPL

AMPL (A Mathematical Modeling Language) is a algebraic language modeling tool to describe and solve large-scale mathematical optimization problems. It is written and maintained by ampl.com. Please visit here for a complete list of different solvers and check the column of documentations for usages. To access AMPL under “polyps”, just type in:

ampl

then type “enter/return”. To exit type in:

exit;

In order to write a AMPL model, we need to understand some of the important syntaxes. A list of available functions and syntaxes in writing an AMPL model is reachable at http://www.ampl.com/BOOK/CHAPTERS/10-params.pdf. Some basics are:

param: declare/define parameters (known)

set: declare/define a set of parameters

var: declare/define variables (unknown to be solved)

sum: sums of multiple formulas

minimize/maximize ***: minimizing/ maximizing an objective

subject to: followed by constraints

The following serves our first example just for practicing  writing a model:

# Example 1

# Declaration of parameters

param m = 5;

param a {i in 1..m, j in 1..m} = i-j;

var x {i in 1..m} >=0;

# Objective Function

minimize obj: x[1] + 2*x[2];

# Constraints

subject to constraint1 {i in 1..m}:

sum {j in 1..m} a[i,j]*x[j] <= 0;

subject to constraint2 {j in 1..m}:

x[j] <= 10;

Please save the file as “***.mod” (with suffix mod) for modeling with AMPL. To simply introduce the usages of AMPL in the documentation, I list the following basic usage for your reference.

Model/Load an AMPL model file:

model ***.mod;

Form an AMPL model file:

write **.mod;

Reset the whole model (Before loading a new model):

reset;

Sometimes we can immediately get a notice from AMPL showing that

Solution determined by presolve;
objective obj = 0.

for Example 1.

Select solver of your choice:

option solver ***;

where “***” stands for the solver name, and some of the available solvers that you may use in your Lehigh ISE 406 class can be chosen by the following names:

CPLEX: cplexamp

MOSEK: mosek

Gurobi: gurobi_ampl

We will not illustrate all the methods in each solver. Just take CPLEX with AMPL as an example for the following case (comes in the end of this section). Again, for a complete list of solvers and corresponding methods, please refer to here. Note that not all the solvers listed in available in Lehigh ISE “polyps” server since we may only have an older version of AMPL installed.

 

Load an existing data file (please load a model before loading data):

data **.dat;

Please do NOT forget the semicolon “;” for commands under AMPL.

To illustrate how to load existing data set diet.dat and model diet.mod. Please download them then just simply load the model and data, and form the model  by

model diet.mod;
data diet.dat;
write diet.mod;

More examples with models and data are available at the AMPL website with a detailed illustrations of the problems at here.

You can also manually input data at AMPL prompt by entering data mode with

and data, and form the model  by

data;

and set any parameter value, say for m, by using

param m := ***;

Starting with anything else will exit the data mode (say “model;”).

We can review the existing parameters, sets, variables, constraints and the objective by

show;

and display any values of these items by

display ***;

After setting all the options just type

solve;

which would return with the solving status. For the diet example, it shows

CPLEX 12.6.1.0: optimal solution; objective 88.2
1 dual simplex iterations (0 in phase I)

To change the method in CPLEX from default method to, say barrier method (IPM), just type

option solver cplexamp;
option cplex_options baropt;

then solve it and it shows on my laptop that

CPLEX 12.6.1.0: baropt
CPLEX 12.6.1.0: optimal solution; objective 88.2
6 barrier iterations
0 push, 1 exchange dual crossover iterations

 

 

II. CPLEX

MPS (Mathematical Programming System) file is a format originally proposed by IBM, which later can be accepted and solved by most commercial or some open-source LP solvers, including CPLEX, GuRoBi and MOSEK. Other format files for LP problems include bas and lp files. Please check the user’s manual for reading them for different solvers.

We are taking a classical example of mps file and try to use it for demonstrating usage of these mentioned popular solvers.

CPLEX is a commercial solver developed by IBM. To access CPLEX under “polyps”, just type in:

cplex

then type “enter/return”. To exit type in:

quit

and for any question just type:

help

It’s really easy to operate with CPLEX with the “help” command and the user’s manual. Note that dual to the possibly limited updates of CPLEX on polyps, some “newly added” functions may not work. Let’s take the pilot.mps as an example. To read the MPS file, either

read pilot.mps

or

read pilot mps

works in CPLEX. Then we can solve it by using primal method/simplex with

primopt

or just simply truncated form “prim”.

Please practice yourself with different methods available in CPLEX and compare the results.

 

 

III. Gurobi

Gurobi is also a commercial optimization solver by Gurobi Optimization. Gurobi is named for its founders: Zonghao Gu, Edward Rothberg and Robert Bixby. Bixby was also the founder of CPLEX, while Rothberg and Gu led the CPLEX development team for nearly a decade. To access Gurobi on the cluster, just enter:

gurobi.sh

and read the mps file by

m = read('pilot.mps')

To set different methods or parameters, please use

setParam(method/parameter, value)

and solve the problem by using

m.optimize()

For a complete list of usages, please refer to the official guide or other resources online.

 

 

IV. MOSEK

MOSEK is a commercial mathematical optimization solver by MOSEK ApS. To use MOSEK to solve the mps format problem, just type

mosek pilot.mps

with options for different methods. Please refer to the User’s Guide and other references for different options. For example, the following command exploit and solve the problem by using primal simplex.

mosek pilot.mps -d MSK_IPAR_OPTIMIZER MSK_OPTIMIZER_PRIMAL_SIMPLEX

The “pilot.bas” file is generated for storage of solutions and iterations.

 

 

V. MATLAB

MATLAB, developed by MathWorks, is a widely used programming software across all engineering fields. However, this is a commercial software and is very expensive to purchase, and currently there is no academic license free to use (cheaper but not free, MOSEK, CPLEX and Gurobi all have free academic license available). Fortunately, we have it available on machines owned by Lehigh (including polyps: just type matlab to enter), and I recently learned that it should be free for all Lehigh students to use for academic purpose now. Please log into your Lehigh account and download it from here if you would like to. Alternatively, GNU Octave is also available as an open-source version of MATLAB.

All the mentioned solvers, namely CPLEX, GuRoBi and MOSEK, have interfaces for MATLAB. It is recommended that you can learn to install the three solvers with MATLAB interfaces although this is not mandatory. If you are interested, just refer to the manuals I cited above.

In this scope, we will learn how to use some of the popular MATLAB optimization tool boxes available. The ones that are popular for research purpose are CVX Toolbox, SeDuMi and YALMIP. Since the installation guide and user’s guide is detailed enough, and it’s not really our focus in ISE 406 class, we won’t illustrate any more on CVX, which also provides solvers of MOSEK, Gurobi, etc.

 

SeDuMi is an optimization solver mainly for solving SDP (semidefinite programming) problems, currently maintained by Lehigh University. Our focus here is not on the definition of SDP problems and you will be able to learn it in your future classes, please refer to the Wiki page for more knowledge into this topic. Download and unzip the package at SeDuMi Github. Even though their installation guide says that one needs to run install_sedumi to install it. In fact, one may just add all the directories to your MATLAB path and it would be sufficient to use it. There is an official user guide available, and please take a look for your reference. Now let us see how to use SeDuMi to solve a SDP (semidefinite programming) problem.

Some commonly used simple syntaxes are:

K.f: number of free components

K.l: number of non-negative components

K.q: dimensions for quadratic or second-order cone constraints

K.s: dimensions for positive semi-definite (PSD) constraints

For instance, we take the example prob_sedumi, which is by getting the largest eigenvalue of a single matrix A. The formulation is somehow simple and can be written as the following SDP problem.

min x
s.t. xI-A≽0

By running the code as following (with specific A defined in the code)

A = [1 -2 3; -2 0 -4; 3 -4 5];

K.s = size(A,1); 

I = eye(K.s);  

bt = -1;   

ct = -[vec(A)];  

At = -[vec(I)];

[x,y,info]=sedumi(At,bt,ct,K); 

A solution of y = 8.8612 should be returned and if you check the eignvalues of A, the largest one exactly matches this value.

For more information about how to solve different problems using SeDuMi, please refer to some existing tutorials online (say here).

 

YALMIP is a popular optimization solver which is originally developed for solving LPs and SDPs but later was promoted to a more complete package. For installation, simply download the package and unzip it. Then add folders under the YALMIP directory to your MATLAB path. A lazy piece of code provided by the YALMIP website is to run the following the MATLAB command window.

cd YALMIPfolderShouldbeHere
urlwrite('https://github.com/yalmip/yalmip/archive/master.zip','yalmip.zip');
unzip('yalmip.zip','yalmip')
addpath(genpath([pwd filesep 'yalmip']));
savepath

Then type the following to verify the success of installation:

yalmiptest

and if there are no errors, then you are done with installation.

If you are interested, please refer to YALMIP website for more information on available solvers and how to solver other type of methods. The three mentioned solvers above are also available in YALMIP. There is also a list of commonly used commands in YALMIP at here. Some of the basic commands include:

sdpvar: set SDP variables, e.g. x = sdpvar(d,1) is a d dimensional variable;

sdpsettings: set solver for optimization, e.g. options =  sdpsettings(‘solver’,’sedumi’);

optimize: solve the problem with desired optimization solver; usage: optimize(constraints, objective, options).

The syntax for writing the constraints and objective is simple. For constraints, if we use “cons” to represent, that should simply be initialized as

cons = [Ax <= b];

with know A, b. We may also add additional constraints by

cons = cons + [Bx <= d];
cons = cons + [x >= 0];

The objective can be simply written in the form similar to:

obj = c'*x;

Then we can solve the problem by

optimize(cons, obj, options);

The syntaxes for YALMIP is pretty simple and it works with most mathematical operators in MATLAB. For instance, we take the example prob_yalmip which is to solve the following problem as an example: find a non-negative pair of (x1, x2) which solves the following problem:

min 2x(1) + x(2)
s.t. 2x(1) + 2x(2) - 1 ≥ 0.1 |x|
     2x(1) + 2x(2) - 2 ≥ 0.1 |x|

where the norm is the Euclidean norm.

By solving the problem with SeDuMi through YALMIP, the code is appeared as

ops =sdpsettings(‘solver’,’sedumi’);

A = [2 2; 2 1];

e = [1;1];

b = [1;2];

c = [2;1];

x = sdpvar(2,1);

prob = [A*x-b>=0.1*sqrt(x(1)^2+x(2)^2)*e];

prob = prob + [x(1)>=0];

prob = prob + [x(2)>=0];

obj = c’*x;

solvesdp(prob,obj,ops);

x = double(x);

obj = double(obj);

The solution is stored in x and the corresponding objective value is in obj.

 

There are also a lot of optimization functions originally available in MATLAB such as linprog for linear programming and quadprog for quadratic programming. If you are interested, you can check the MATLAB Optimization Toolbox for a complete list of all those functions with instructions and usages.

 

ATTENTION! Please finish your assignments for ISE 406 in accordance with the requirements. If the problem requires you to write an AMPL file and solve with CPLEX, MOSEK, Gurobi, please do NOT use other interfaces of these solvers (YALMIP, SEDUMI, etc.). Always be in consistent with the instructor’s requirements. Otherwise, you will get no points 🙂

 

Updated: Sep 29, 2016.

 

A Bite of New Jersey (NJ美食)

On July 28, 2016, in Food & Drinks, by jild13

Princeton & Trenton, NJ:

莲园(Lotus Garden)

10 Schalks Crossing Rd., Plainsboro, 609-799-8888

 

大钱美食林 Shanghai Park  609-924-8001   Star: 2

301 N Harrison St, Princeton, NJ 08540

推荐:上海菜,小笼包,水煮牛, 狮子头,鱼, 周末早茶豆浆油条

 

龙园 Dragon Palace    732-549-7675

1635 Oak Tree Rd, Edison, NJ 08820

葱油饼,稀饭

 

Yaya Noodles   609-921-8551       Star:3.5

1325 Route 206, Skillman, NJ 08558

红烧牛肉面,雪菜肉丝面,香辣牛筋,玫瑰酥(需提前预定)

 

东之王自助(Super Star East Chinese Buffet)   Star: 2.5

地址:311 Nassau Park Blvd, Princeton, NJ, 609-987-9168

 

川妹子 Szechuan House     Star: 4

2022 Nottingham Way Hamilton, NJ 08619

推荐:重庆辣子鸡,夫妻肺片,干煸肥肠,魔芋烧鸭,清炒丝瓜

 

好世界自助餐厅(World Buffet)

地址:368 Rt.33 Mercerville, NJ 08619

评价:菜的种类丰富, 个人感觉相比东之味, 好世界的口味更偏台湾系一点

 

Hot Wok Cafe      609-716-8983   Star: 4

217 Clarksville Road, West Windsor Township, NJ 08550

孜然羊肉、小椒牛肉、水煮牛、水煮鱼与凉面,

 

Edison, NJ:

九寨沟   Star: 5

1167 Raritan Ave. Highland Park, NJ 08904

732-572-9510

推荐:干锅茶树菇,毛血旺,水煮鱼,辣子鸡,四川火锅

 

H-mart, 小肥羊,巴黎贝甜    Star: 4

1761 Route 27 Edison, NJ 08817

 

太阳大酒家(Sunny Palace)   Star: 5

地址:1069 State Route 18, East Brunswick, NJ 08816

推荐:广式口味, 早茶,周末流水早茶

 

东方串店  Star: 4.5

地址:1834 Route 27, Edison, NJ 08817, 732-339-8899

推荐:炭火自行烧烤, 既好吃, 北方菜也做得非常地道, 羊肉串、羊汤、拔丝地瓜, 羊蝎子火锅.

 

虹城餐廳 (Kings Village)

地址:1639 Route 27, Edison, NJ 08817, 732-339-9858 评价:有许多地道的北方菜, 比如卤煮, 北方来的同学多半会喜欢

 

四川第一楼  Szechuan ACE Chinese Restaurant   732-937-9330

1721 Route 27, Somerset, NJ 08873

 

青苹果 Green Apple Japanese & Chinese Cuisine

432 Renaissance Boulevard East North Brunswick, NJ 08902

酸菜鱼片,排骨,羊肉

 

湘味轩 (Hunan House)   732-346-1688 (Edison)好! Star: 4

49 Lafayette Rd fords, NJ 08863

推荐:剁椒鱼头,活水煮活鱼,干锅田鸡,干煸肥肠

 

知味坊(Dragon Asian Bistro)

地址: 239 Raritan Ave, Highland Park, NJ 08904

 

美东超市(盒饭+亚洲超市)

1339 Centennial Ave, Piscataway, NJ 08854

Tel: (732)645-3018

Fax:(732)645-3019

Mon-Thu 9:00AM-8:00PM

Fri-Sat 9:00AM-8:30PM

 

Bridgewater, NJ:

老湖南 Fortune Cookie Asian Cuisine (Old Hunan)  908-429-8886    Star: 3

41 Old York Road, Bridgewater, NJ 08807

推荐:剁椒鱼头,梅菜扣肉,酸豆角肉末

 

乐膳 Gallop Asian Bistro   908-210-9161  Star: 5

793 Route 202 North, Bridgewater, NJ 08807

推荐:烤鱼,牛蛙,红烧茄子

 

To be updated …

*********************************************************************************************

Another collection of all kinds of restaurants is available at http://www.cs.princeton.edu/~wayne/princeton.restaurants/

*********************************************************************************************

 

Two of the most common used computational libraries are LAPACK and BLAS.  They are super fast in doing linear algebra operations involving matrices and vectors, .e.g. solving linear equation systems, doing SVD factorization, etc.

Originally, both packages were written in FORTRAN, but I want to use them in C++ for the purpose of basic computational research. Next, I will introduce how to install them on Linux-based systems (tested on Mac OS) for C++.

Let us create a library to adapt them to your systems.

First, you have to install BLAS before LAPACK, because LAPACK needs it. Download the packages from the following websites.

Switch to the BLAS folder and execute

make

to compile all fortran files. After that, execute

mv blas_UNIX.a libblas.a

to rename the created library. After creating the library called “libblas.a”, copy that file to your library folder by executing the following command

sudo cp libblas.a /usr/local/lib/

The above directory can be replaced by any library path in your systems.

Now we have installed the BLAS package. Then switch to the LAPACK directory and adjust the file “make.inc” if necessary. After setting all parameters correctly, type the command

make

Now, you created a library e.g. called “lapack_MACOS.a”. Copy that file to your library folder by executing

sudo cp liblapack.a /usr/local/lib/

Congratulation, you’ve installed BLAS and LAPACK on your systems!

Note: when using C++, do not forget to point out your search directory for header files with option “-I”, and add your library path with “-L” for libraries with “-l” if the search paths for the header files and libraries are not included.

Any comment or question is welcome.

 

Condor or HTcondor is an open source project at the University of Wisconsin’s Computer Science Department, which is a high-throughput computing software framework for coarse-grained distributed parallelization of computationally intensive tasks, a.k.a., a cluster management system.

 

For a complete manual with tutorials and examples please check the manual.

 

However, there is always the possibility that we have to use a machine without condor installed. I was stuck in this embarrassment when interning at IBM Research Lab – Ireland. The purpose of this tutorial is to find a substitute which is more widely used. The right tool we would like to use is GNU Screen.

 

This tutorial will have some basic and frequent-used commands introduced as below (PC/Mac resp.).

 

Start a new screen session:

screen

Detach from the current screen session (leave session temporarily):

Ctrl/Command + a + d

List all screen sessions:

screen -ls

Return to a screen session:

screen –r +sessionID

Kill the current session:

exit

Start a new screen session:

pkill screen

or

killall screen

More screen commands are available to use, and for a complete manual of the command “screen”, please type

man screen

or check here.

 

Any comment or question is welcome.

 

The GNU Scientific Library (GSL) is a numerical library for C and C++ programmers. It is a free open source library under the GNU General Public License.

 

Note: this tutorial has been tested on Ubuntu, Redhat and Mac OS.

 

Download: 

ftp://ftp.gnu.org/gnu/gsl/

wget ftp://ftp.gnu.org/gnu/gsl/gsl-*.*.tar.gz

*.* should be replaced by the version to be installed. Please check release history available at https://www.gnu.org/software/gsl/. You can also download here and unzip the program directly depending on the OS you are working with.

 

Place the file in your home directory and unpack the file with the following command:

tar -zxvf gsl-*.*.tar.gz

This will create a directory called gsl-*.* in your home directory. Change to this directory.

cd gsl-1.7

The next step is to configure the installation and tell the system where to install the files. Create a directory to install your gsl package, say “/home/yourname/gsl” with the following command

mkdir /home/yourname/gsl

Now configure the installation and tell it to use your new directory. This step may take a few minutes.

./configure --prefix=/home/yourname/gsl

If there are no errors, compile the library. This step will take several minutes.

make

Now it is necessary to check and test the library before actually installing it. This step will take some time.

make check

If there are no errors, go ahead and install the library with:

make install

Now we can write a test program to see if the library works. Create the following program and name it example.c

#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>

int
main (void)
{
    double x = 15.0;
    double y = gsl_sf_bessel_J0 (x);
    printf ("J0(%g) = %.18e/n", x, y);
    return 0;
}

Compile and link the program with the following commands (but use the correct path for your username):

gcc -Wall -I/home/yourname/gsl/include -c example.c
gcc -L/home/yourname/gsl/lib example.o -lgsl -lgslcblas -lm

Now run your program!

./a.out

 

Now that you have the GSL installed, you can remove the gsl-*.* directory that was created in your home directory.

 

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

For Linux/Unix based machines, a complete instruction is available at the installation page of gcc website.

For Mac users, there are more choices except for the one above (installing from source, gcc version 6.1 which can be replaced!):

1. Port Install:

sudo port install gcc6
sudo port select --list gcc
sudo port select --set gcc mp-gcc6

where the first line is for installation, second line for listing existing compilers by Macports and the last one is to select default gcc compiler. You can update Macports by:

sudo port selfupdate

2. Homebrew:

brew tap homebrew/versions
sudo brew install [flags] gcc6

Please use the following to check available flags:

sudo brew options gcc6

and use the following for checking available gcc versions:

sudo brew search gcc

You can update Macports by:

sudo brew update

For installation of Homebrew, please check MacPorts and Homebrew.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Here is command for replacing the default gcc compiler with the newest one.

cd /usr/local/bin
rm cc gcc g++ c++

where the directory should be replaced by the directory where default gcc is placed

Now run your program!

ln -s /usr/local/bin/gcc-6 cc
ln -s /usr/local/bin/gcc-6 cc
ln -s /usr/local/bin/g++-6 g++
ln -s /usr/local/bin/c++-6 c++

where the compilers (with directories) should be replaced by your new compilers.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Any comment or question is welcome.