如何使用Gurobi的vbasis和cbasis与R-接口?(How can you use Gur

2019-09-28 13:36发布

我发现在文档的解释这里和这里 :

vbasis
对于计算的最佳基础上的可变基状态值。 你一般不应与此数组的内容关心自己。 如果希望以后使用高级的开始,您只需在vbasis和cbasis阵列复制到下一个模型的相应字段。 此数组包含A的每个列的一个条目

cbasis
对于计算的最佳基础约束基础状态值。 此数组包含A的每行一个条目

后来就:

最后,如果最后的溶液是碱性溶液(由单纯计算的),然后vbasis和cbasis将存在。

我不明白为什么我没有得到这样的价值观 - 也许我想念的东西。

重复的例子:

model <- list()
model$A          <- matrix(c(1, 1, 1, 0, 
                             1, 0, 0, 0, 
                             0, 1, 0, 0, 
                             0, 0, 1, 0, 
                             0, 0, 0, 1), nrow = 5, ncol = 4, byrow = T)
model$obj        <- c(2, -5, 3, 10)
model$modelsense <- "min"
model$rhs        <- c(15, 7, 3, 5, 1)
model$sense      <- c('=', '<=', '<=', '<=', '>')
model$vtype      <- 'I'
params <- list(OutputFlag = 1, Presolve = 2, TimeLimit = 3600)
result <- gurobi::gurobi(model, params)

names(result)
[1] "status" "runtime" "itercount" "baritercount" "nodecount" "objval" "x" "slack" "objbound"

Answer 1:

由于格雷格格洛克纳的答案在这里 ,我发现的东西文件 ,我复制到这里。

它似乎很很多已与Gurobi版本8.0.0改善!

# Copyright 2018, Gurobi Optimization, LLC
#
# This example reads an LP model from a file and solves it.
# If the model can be solved, then it finds the smallest positive variable,
# sets its upper bound to zero, and resultolves the model two ways:
# first with an advanced start, then without an advanced start
# (i.e. 'from scratch').

library(Matrix)
library(gurobi)

args <- commandArgs(trailingOnly = TRUE)
if (length(args) < 1) {
  stop('Usage: Rscript lpmod.R filename\n')
}

# Read model
cat('Reading model',args[1],'...')
model <- gurobi_read(args[1])
cat('... done\n')

# Determine whether it is an LP
if ('multiobj'  %in% names(model) ||
    'sos'       %in% names(model) ||
    'pwlobj'    %in% names(model) ||
    'cones'     %in% names(model) ||
    'quadcon'   %in% names(model) ||
    'genconstr' %in% names(model)   ) {
  stop('The model is not a linear program\n')
}

# Detect set of non-continuous variables
intvars    <- which(model$vtype != 'C')
numintvars <- length(intvars)
if (numintvars > 0) {
  stop('problem is a MIP, nothing to do\n')
}

# Optimize
result <- gurobi(model)
if (result$status != 'OPTIMAL') {
  cat('This model cannot be solved because its optimization status is',
      result$status, '\n')
  stop('Stop now\n')
}

# Recover number of variables in model
numvars   <- ncol(model$A)

# Ensure bounds array is initialized
if (is.null(model$lb)) {
  model$lb <- rep(0, numvars)
}
if (is.null(model$ub)) {
  model$ub <- rep(Inf, numvars)
}

# Find smallest (non-zero) variable value with zero lower bound
x      <- replace(result$x, result$x < 1e-4, Inf)
x      <- replace(x, model$lb > 1e-6, Inf)
minVar <- which.min(x)
minVal <- x[minVar]

# Get variable name
varname <- ''
if (is.null(model$varnames)) {
  varname <- sprintf('C%d',minVar)
} else {
  varname <- model$varnames[minVar]
}

cat('\n*** Setting', varname, 'from', minVal, 'to zero ***\n\n')
model$ub[minVar] <- 0

# Set advance start basis information
model$vbasis <- result$vbasis
model$cbasis <- result$cbasis

result2   <- gurobi(model)
warmCount <- result2$itercount
warmTime  <- result2$runtime

# Reset-advance start information
model$vbasis <- NULL
model$cbasis <- NULL

result2   <- gurobi(model)
coldCount <- result2$itercount
coldTime  <- result2$runtime

cat('\n*** Warm start:', warmCount, 'iterations,', warmTime, 'seconds\n')
cat('\n*** Cold start:', coldCount, 'iterations,', coldTime, 'seconds\n')

# Clear space
rm(model, result, result2)


文章来源: How can you use Gurobi's vbasis and cbasis with the R-Interface?
标签: r gurobi