# Decision Optimization

View Only

## how to create an expression indexed

• #### 1.  how to create an expression indexed

Posted Tue January 03, 2023 02:25 PM
i have this expression and i want to model it with cplex and c++ , i don't know if the code is correct or not , can someone help me,  with x[w][w][i][j] is a decision variable

this is my code :
``````		compteur = 0;
IloFloatVarArray2 CW(env, W);
for (w = 0; w < W; w++)
{
CW[w] = IloFloatVarArray(env, W, 0.0, INFINITY);
#ifdef DEBUG
for (w = 0; w < W; w++)
{
sprintf(varname, "CW_%d_%d", w, w);
CW[w][w].setName(varname);
compteur++;
}
}
#endif
#ifdef DEBUG
printf("compteur cw =%d\n", compteur);
#endif

IloExpr CW[w][w](env);

for (i = 0; i < A; i++)
for (j = 0; j < A; j++)
CW[w][w] += d[i][j] * xW[w][i][j][w];
for (i = 0; i < A; i++)
for (j = 0; j < A; j++)
CW[w][w] += Parc[i][j] * xW[w][i][j][w];
for (i = 0; i < A; i++)
for (j = 0; j < A; j++)
CW[w][w] += 1 * xW[w][i][j][w];
CW[w][w].end();
​``````

------------------------------
siwar mni
------------------------------

#DecisionOptimization

• #### 2.  RE: how to create an expression indexed

Posted Wed January 04, 2023 08:21 AM
Hello,
you have to create a 2-D array of expressions, fill it, and then constrain the expressions by some constraints. for example:
``````#include <ilcp/cp.h>

int main() {
typedef IloArray<IloNumExprArray> IloNumExprArray2;
IloEnv env;
IloInt n = 4;
IloNumExprArray2 c(env, n);
IloIntVarArray var(env, n, 0, n-1);
for (IloInt x = 0; x < n; x++) {
c[x] = IloNumExprArray(env, n);
for (IloInt y = 0; y < n; y++) {
c[x][y] = IloNumExpr(env, 0);
c[x][y] += var[x] * var[y];
}
}
IloModel mdl(env);

for (IloInt x = 0; x < n; x++) {
for (IloInt y = 0; y < n; y++) {
}
}
IloCP cp(mdl);
cp.startNewSearch();
while (cp.next()) {
cp.out() << cp.domain(var) << std::endl;
}
cp.end();
env.end();
return 0;
}
​``````

------------------------------
Olivier Lhomme
------------------------------