Pyrite | Level 9

## Define constraints and optimization function in matrix form

I'm getting stuck in defining constraints and the optimization function in matrix form for proc optmodel.

Suppose these scenarios:

1) I have two vectors A and B, and my constraint is A<=B (meaning: element-wise). How can I write it without a for loop to cycle the on the elements?

2) I have two matrices A and B, and my constraint is A[:, k]' * B[: ,k] <= 0 for each k in 1..n (meaning: for each k in 1..n, take the k-th column of matrix A, transpose it, take the k-th column of matrix B, and do the scalar product between the two vectors; this number must be <=0). How can I write it with only one for loop to cycle over k?

These conditions are very easy to write e.g. in Matlab, but I find very difficult to write them in the proc optmodel.

How would you do that?

Any help appreciated,

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: Define constraints and optimization function in matrix form

PROC OPTMODEL uses algebraic expressions to declare such constraints:

var A {1..n};
var B {1..n};
con MyCon {i in 1..n}:
A[i] <= B[i];
var A {1..m, 1..n};
var B {1..m, 1..n};
con MyCon {k in 1..n}:
sum {i in 1..m} A[i,k] * B[i,k] <= 0;

You might also be interested in this post, which shows how to use PROC FCMP with PROC OPTMODEL to access some matrix routines.

2 REPLIES 2
SAS Super FREQ

## Re: Define constraints and optimization function in matrix form

PROC OPTMODEL uses algebraic expressions to declare such constraints:

var A {1..n};
var B {1..n};
con MyCon {i in 1..n}:
A[i] <= B[i];
var A {1..m, 1..n};
var B {1..m, 1..n};
con MyCon {k in 1..n}:
sum {i in 1..m} A[i,k] * B[i,k] <= 0;

You might also be interested in this post, which shows how to use PROC FCMP with PROC OPTMODEL to access some matrix routines.

Pyrite | Level 9

## Re: Define constraints and optimization function in matrix form

Thanks a lot, it really helped!
Discussion stats
• 2 replies
• 655 views
• 0 likes
• 2 in conversation