Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  get coefficient of constraint

    Posted 12/14/11 05:02 AM

    Originally posted by: JFYY_yaohua_tang


    hello, everyone.

    I have thousands of constraints, and I need to get their coefficients for each variable in each constraints, following is a example.

    variables are x1 x2 x3 x4

    constraint are 3*x1 + x3 + 5*x4 > 0

    and what I need is a vector 3,0,1,5.

    Is there any easy way to finish such a job?

    thanks.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: get coefficient of constraint

    Posted 12/14/11 05:15 AM

    Originally posted by: SystemAdmin


    What API do you use (C, C++, Java, ...)?
    Do you need the value 0 in the vector in your example or is it OK to return a vector that has only the non-zero elements?
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: get coefficient of constraint

    Posted 12/14/11 06:22 AM

    Originally posted by: JFYY_yaohua_tang


    I need the 0, because I need to know the direction of the half plane indicated by the constraint.

    I am using C++.

    thanks.
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: get coefficient of constraint

    Posted 12/14/11 11:03 AM

    Originally posted by: SystemAdmin


    Using an IloExpr::LinearIterator you can iterate over the non-zero terms of an IloRange:
    
    IloExpr expr = range.getExpr(); 
    
    for (IloExpr::LinearIterator l = expr.getLinearIterator(); l.ok(); ++l) 
    { std::cout << l.getCoef() << 
    " * " << l.getVar() << std::endl; 
    }
    

    Since you also need the zero entries an probably also a particular order of the variables you need to do more. Assuming you have all your variables in an IloNumArray x you could store the non-zero coefficients in a map and then do something like this (untested code)
    
    
    // Compare instances of IloNumVar by their extractable id. struct LessIloNumVar 
    { bool operator()(IloNumVar const& v1, IloNumVar const& v2) 
    
    const 
    { 
    
    return v1.getId() < v2.getId(); 
    } 
    };   
    // Iterate over expression and store non-zero coefficients in a map. typedef std::map<IloNumVar,IloNum,LessIloNumVar> VarMap; VarMap varmap; IloExpr expr = range.getExpr(); 
    
    for (IloExpr::LinearIterator l = expr.getLinearIterator(); l.ok(); ++l) 
    { 
    // If the variable is already in the map then we just update the 
    // coefficient. Otherwise we insert the variable into the map. VarMap::iterator it = varmap.find(l.getVar()); 
    
    if ( it == varmap.end() ) varmap.insert(VarMap::value_type(l.getVar(), l.getCoef())); 
    
    else it->second += l.getCoef(); 
    }   
    // Iterate over all variables and fetch their coefficients from the map. 
    // If a variable is not in the map then its coefficient is 0. IloNumArray vec(x.getEnv()); 
    
    for (IloInt i = 0; i < x.getSize(); ++i) 
    { VarMap::const_iterator it = varmap.find(x[i]); vec.add(it == varmap.end() ? 0 : it->second); 
    }   
    // vec is now the vector you are looking for.
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: get coefficient of constraint

    Posted 12/14/11 11:20 PM

    Originally posted by: JFYY_yaohua_tang


    thanks so much , I have test your program and it works quit well.
    #CPLEXOptimizers
    #DecisionOptimization