本文整理汇总了Java中ilog.concert.IloNumVar类的典型用法代码示例。如果您正苦于以下问题:Java IloNumVar类的具体用法?Java IloNumVar怎么用?Java IloNumVar使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IloNumVar类属于ilog.concert包,在下文中一共展示了IloNumVar类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addVariable
import ilog.concert.IloNumVar; //导入依赖的package包/类
@Override
public void addVariable(String varName, double objWeight, double lowerBound, double upperBound, boolean override, boolean isZVar) throws ILPException
{
try {
IloNumVarType type = null;
if(upperBound > 1) {
type = IloNumVarType.Int;
} else {
type = IloNumVarType.Bool;
}
IloNumVar var = cplex.numVar(lowerBound, upperBound, type, varName);
if(override || !variables.containsKey(varName)) {
variables.put(varName, var);
cplex.add(var);
objective.put(varName, objWeight);
}
} catch(IloException e) {
throw new ILPException(e.getMessage());
}
}
示例2: initVariables
import ilog.concert.IloNumVar; //导入依赖的package包/类
private void initVariables() throws IloException {
y = new LinkedHashMap<>();
w = new LinkedHashMap<>();
d = new LinkedHashMap<>();
x = new LinkedHashMap<>();
x0 = new LinkedHashMap<>();
for (Node node : graph.vertexSet()) {
String nodeName = Integer.toString(node.getNum() + 1);
d.put(node, cplex.numVar(0, Double.MAX_VALUE, "d" + nodeName));
y.put(node, cplex.boolVar("y" + nodeName));
x0.put(node, cplex.boolVar("x_0_" + (node.getNum() + 1)));
}
for (Edge edge : graph.edgeSet()) {
Node from = graph.getEdgeSource(edge);
Node to = graph.getEdgeTarget(edge);
String edgeName = (from.getNum() + 1) + "_" + (to.getNum() + 1);
w.put(edge, cplex.boolVar("w_" + edgeName));
IloNumVar in = cplex.boolVar("x_" + edgeName + "_in");
IloNumVar out = cplex.boolVar("x_" + edgeName + "_out");
x.put(edge, new Pair<>(in, out));
}
}
示例3: breakRootSymmetry
import ilog.concert.IloNumVar; //导入依赖的package包/类
private void breakRootSymmetry() throws IloException {
int n = graph.vertexSet().size();
PriorityQueue<Node> nodes = new PriorityQueue<>();
nodes.addAll(graph.vertexSet());
int k = n;
IloNumExpr[] terms = new IloNumExpr[n];
IloNumExpr[] rs = new IloNumExpr[n];
while (!nodes.isEmpty()) {
Node node = nodes.poll();
terms[k - 1] = cplex.prod(k, x0.get(node));
rs[k - 1] = cplex.prod(k, y.get(node));
k--;
}
IloNumVar sum = cplex.numVar(0, n, "prSum");
cplex.addEq(sum, cplex.sum(terms));
for (int i = 0; i < n; i++) {
cplex.addGe(sum, rs[i]);
}
}
示例4: sumConstraints
import ilog.concert.IloNumVar; //导入依赖的package包/类
private void sumConstraints() throws IloException {
// (31)
cplex.addLe(cplex.sum(graph.vertexSet().stream().map(x -> x0.get(x)).toArray(IloNumVar[]::new)), 1);
if (root != null) {
cplex.addEq(x0.get(root), 1);
}
// (32)
for (Node node : graph.vertexSet()) {
Set<Edge> edges = graph.edgesOf(node);
IloNumVar xSum[] = new IloNumVar[edges.size() + 1];
int i = 0;
for (Edge edge : edges) {
xSum[i++] = getX(edge, node);
}
xSum[xSum.length - 1] = x0.get(node);
cplex.addEq(cplex.sum(xSum), y.get(node));
}
}
示例5: removeColumn
import ilog.concert.IloNumVar; //导入依赖的package包/类
/**
* @param index
* @throws CPException
*/
@Override
public void removeColumn(int index) throws CPException {
IloNumVar var = getVar( index );
try {
m_model.delete( var );
removeVar( index );
ArrayUtils.remove( m_objCoeffs, index );
} catch( IloException e ) {
m_logger.fatal( e );
throw new CPException( e );
}
}
示例6: removeColumns
import ilog.concert.IloNumVar; //导入依赖的package包/类
public void removeColumns(String namePrefix) {
try {
for (Iterator<IloNumVar> varIter = m_vars.iterator(); varIter.hasNext();) {
IloNumVar var = varIter.next();
String name = var.getName();
if (name != null && name.startsWith( namePrefix )) {
m_model.delete( var );
m_nameVarMap.remove( var.getName() );
varIter.remove();
}
}
} catch( IloException e ) {
m_logger.fatal( e );
throw new RuntimeException( e );
}
}
示例7: addColumn
import ilog.concert.IloNumVar; //导入依赖的package包/类
/**
* Function which adds a new column to the cplex problem
*/
@Override
public void addColumn(CuttingPattern column) {
try {
//Register column with objective
IloColumn iloColumn= cplex.column(obj,1);
//Register column with demand constraint
for(int i=0; i< dataModel.nrFinals; i++)
iloColumn=iloColumn.and(cplex.column(satisfyDemandConstr[i], column.yieldVector[i]));
//Create the variable and store it
IloNumVar var= cplex.numVar(iloColumn, 0, Double.MAX_VALUE, "z_"+","+masterData.getNrColumns());
cplex.add(var);
masterData.addColumn(column, var);
} catch (IloException e) {
e.printStackTrace();
}
}
示例8: getSolution
import ilog.concert.IloNumVar; //导入依赖的package包/类
/**
* Return the solution, i.e columns with non-zero values in the cplex problem
*/
@Override
public List<CuttingPattern> getSolution() {
List<CuttingPattern> solution=new ArrayList<>();
try {
CuttingPattern[] cuttingPatterns=masterData.getVarMap().getKeysAsArray(new CuttingPattern[masterData.getNrColumns()]);
IloNumVar[] vars=masterData.getVarMap().getValuesAsArray(new IloNumVar[masterData.getNrColumns()]);
double[] values= cplex.getValues(vars);
//Iterate over each column and add it to the solution if it has a non-zero value
for(int i=0; i<cuttingPatterns.length; i++){
cuttingPatterns[i].value=values[i];
if(values[i]>=config.PRECISION){
solution.add(cuttingPatterns[i]);
}
}
} catch (IloException e) {
e.printStackTrace();
}
return solution;
}
示例9: getSolution
import ilog.concert.IloNumVar; //导入依赖的package包/类
/**
* Gets the solution from the master problem
* @return Returns all non-zero valued columns from the master problem
*/
@Override
public List<Matching> getSolution() {
List<Matching> solution=new ArrayList<>();
try {
for(PricingProblemByColor pricingProblem : pricingProblems){
Matching[] matchings=masterData.getVarMapForPricingProblem(pricingProblem).getKeysAsArray(new Matching[masterData.getNrColumnsForPricingProblem(pricingProblem)]);
IloNumVar[] vars=masterData.getVarMapForPricingProblem(pricingProblem).getValuesAsArray(new IloNumVar[masterData.getNrColumnsForPricingProblem(pricingProblem)]);
double[] values=masterData.cplex.getValues(vars);
//Iterate over each column and add it to the solution if it has a non-zero value
for(int i=0; i<matchings.length; i++){
matchings[i].value=values[i];
if(values[i]>=config.PRECISION){
solution.add(matchings[i]);
}
}
}
} catch (IloException e) {
e.printStackTrace();
}
return solution;
}
示例10: setUpModel
import ilog.concert.IloNumVar; //导入依赖的package包/类
/**
* Builds the LP model based on the game instance.
* @throws IloException
*/
private void setUpModel() throws IloException {
setCplexParameters();
objective = cplex.linearNumExpr();
// The empty sequence is the 0'th sequence for each player
numSequencesP1 = numSequencesP2 = 1;
primalSequenceNames[0] = "root";
CreateSequenceFormIds(game.getRoot(), new TIntHashSet(), new TIntHashSet());
assert(numSequencesP1 == game.getNumSequencesP1()); // Ensure that our recursive function agrees with the game reader on how many sequences there are
assert(numSequencesP2 == game.getNumSequencesP2());
// create root sequence var
IloNumVar rootSequence = cplex.numVar(1, 1, "Xroot");
strategyVarsBySequenceId[0] = rootSequence;
CreateSequenceFormVariablesAndConstraints(game.getRoot(), rootSequence, new TIntHashSet(), 1);
setObjective();
}
示例11: getPrecedenceVariableValues
import ilog.concert.IloNumVar; //导入依赖的package包/类
private ImmutableTable<V, V, Double> getPrecedenceVariableValues(
VariableSet.VariableExtractor variableExtractor) {
ImmutableTable.Builder<V, V, Double> ans = ImmutableTable.builder();
List<Cell<V, V, IloNumVar>> varsAsList = Lists
.newArrayList(this.precedenceVariables.cellSet());
IloNumVar[] varArray = new IloNumVar[varsAsList.size()];
int i = 0;
for (Cell<V, V, IloNumVar> cell : varsAsList) {
varArray[i++] = cell.getValue();
}
double[] varVals;
try {
varVals = variableExtractor.getValuesVE(varArray);
} catch (IloException e) {
throw new RuntimeException(e);
}
for (int j = 0; j < varsAsList.size(); j++) {
ans.put(varsAsList.get(j).getRowKey(), varsAsList.get(j).getColumnKey(),
varVals[j]);
}
return ans.build();
}
示例12: printDeltaVars
import ilog.concert.IloNumVar; //导入依赖的package包/类
private void printDeltaVars(VariableSet.VariableExtractor variableExtractor) {
List<Cell<V, V, IloNumVar>> varsAsList = Lists
.newArrayList(this.precedenceVariables.cellSet());
IloNumVar[] varArray = new IloNumVar[varsAsList.size()];
int i = 0;
for (Cell<V, V, IloNumVar> cell : varsAsList) {
varArray[i++] = cell.getValue();
}
double[] varVals;
try {
varVals = variableExtractor.getValuesVE(varArray);
} catch (IloException e) {
throw new RuntimeException(e);
}
for (int j = 0; j < varsAsList.size(); j++) {
System.out.println(varsAsList.get(j).getRowKey() + ", "
+ varsAsList.get(j).getColumnKey() + " = " + varVals[j]);
}
}
示例13: exportToDisk
import ilog.concert.IloNumVar; //导入依赖的package包/类
public void exportToDisk(IMIP mip, Path path) {
try {
IloCplex cplex = CPLEXInstanceManager.INSTANCE.checkOutCplex();
setControlParams(cplex, mip.getSpecifiedSolveParams(),mip::getSolveParam);
Map<String, IloNumVar> vars = setupVariables(mip, cplex);
setupConstraints(mip, cplex, vars);
setUpObjective(mip, cplex, vars);
cplex.exportModel(path.toString());
} catch (Exception ex) {
logger.error("Failed to write cplex model to disk", ex);
}
}
示例14: setupVariables
import ilog.concert.IloNumVar; //导入依赖的package包/类
private Map<String, IloNumVar> setupVariables(IMIP mip, IloCplex cplex) throws IloException {
// Setup Variables:
// ////////////////
Map<String, IloNumVar> vars = new HashMap<>(); // varName to
// IloNumVar
int numberOfBooleanAndIntVariables = 0;
IloNumVarType varType = IloNumVarType.Float;
for (Variable var : mip.getVars().values()) {
if (var.ignore()) {
logger.debug("Skipping variable: " + var);
continue;
}
logger.debug("Adding variable: " + var);
VarType type = var.getType();
if (VarType.DOUBLE.equals(type)) {
varType = IloNumVarType.Float;
} else if (VarType.INT.equals(type)) {
varType = IloNumVarType.Int;
numberOfBooleanAndIntVariables++;
} else if (VarType.BOOLEAN.equals(type)) {
varType = IloNumVarType.Bool;
numberOfBooleanAndIntVariables++;
}
IloNumVar numVar = cplex.numVar(var.getLowerBound(), var.getUpperBound(), varType, var.getName());
vars.put(var.getName(), numVar);
}
cplex.add(vars.values().toArray(new IloNumVar[vars.size()]));
// Propose Values, if any:
// ///////////////////////
proposeValues(mip, cplex, vars, numberOfBooleanAndIntVariables);
return vars;
}
示例15: addLinear
import ilog.concert.IloNumVar; //导入依赖的package包/类
static void addLinear(final Expression source, final IloLinearNumExpr destination, final ExpressionsBasedModel model, final List<IloNumVar> variables)
throws IloException {
for (final IntIndex key : source.getLinearKeySet()) {
final int freeInd = model.indexOfFreeVariable(key.index);
if (freeInd >= 0) {
destination.addTerm(source.getAdjustedLinearFactor(key), variables.get(freeInd));
}
}
}