How to use gurobi for solving a particular problem
Awaiting user inputI have the following code in R
> library(robustbase)
>
> library(mgcv)
Loading required package: nlme
This is mgcv 1.8-40. For overview type 'help("mgcv-package")'.
> library(MASS)
>
> library(quadprog)
>
> # We generate 100 values from N(0,1)
> set.seed(123)
> u <- rnorm(100)
> u[60] <- 15
>
> # We now generate y(t)=μ+φ1y(t-1)+φ2y(t-2)+u(t)
> # Let μ=0.0, φ1=0.25, φ2=0.5
> A <- matrix(NA, nrow = 98, ncol = 3)
> Y <- numeric(100)
> y <- numeric(100)
> y[1] <- u[1]
> y[2] <- 0.25*y[1] + u[2]
> Y[1:2] <- c(y[1], y[2])
>
> for (i in 3:100) {
+ y[i] <- 0.0 + 0.25*y[i-1] + 0.5*y[i-2] + u[i]
+ A[i-2,] <- c(y[i], y[i-1], y[i-2])
+ Y[i] <- y[i]
+ }
>
> # Now we add AO outliers. We add in the 10th, 30th
> # and 50nd observation the value 4.
> A[7,1] <- A[7,1]+4
> A[8,2] <- A[8,2]+4
> A[9,3] <- A[9,3]+4
> A[27,1] <- A[27,1]+4
> A[28,2] <- A[28,2]+4
> A[29,3] <- A[29,3]+4
> A[47,1] <- A[47,1]+4
> A[48,2] <- A[48,2]+4
> A[49,3] <- A[49,3]+4
>
> #Y[10] <- Y[10]+4
> #Y[30] <- Y[30]+4
> #Y[50] <- Y[50]+4
>
> ar100model<-A
>
> Xmat<-ar100model[,1:3]
>
> cov.mcd(Xmat,quantile.used=60)
$center
[1] 0.6538957 0.6561078 0.5844738
$cov
[,1] [,2] [,3]
[1,] 1.5315770 0.5809420 0.7483602
[2,] 0.5809420 1.3235511 0.5575139
[3,] 0.7483602 0.5575139 1.3142478
$msg
[1] "0 singular samples of size 4 out of 2000"
$crit
[1] -1.635303
$best
[1] 1 2 3 4 5 10 11 12 13 14 15 17 18 19 20 21 30 31 32 33 34 35 36 37 38
[26] 39 40 41 44 45 46 50 51 52 53 54 57 71 72 74 75 76 77 78 79 80 81 82 83 84
[51] 85 86 87 88 89 90 91 92 93 94
$n.obs
[1] 98
> MCD<-cov.mcd(Xmat,quantile.used=60)$best
> bestMCD<-Xmat[MCD,]
> bestMCD
[,1] [,2] [,3]
[1,] 1.185896391 -0.370296401 -0.560475647
[2,] 0.181834289 1.185896391 -0.370296401
[3,] 0.767694503 0.181834289 1.185896391
[4,] 1.997905757 0.767694503 0.181834289
[5,] 1.344239896 1.997905757 0.767694503
[6,] 0.435555108 1.122959945 -0.409997411
[7,] 1.071140200 0.435555108 1.122959945
[8,] 0.596245320 1.071140200 0.435555108
[9,] 0.128790295 0.596245320 1.071140200
[10,] 2.117233371 0.128790295 0.596245320
[11,] 1.091553969 2.117233371 0.128790295
[12,] 1.088354891 -0.635111979 1.091553969
[13,] -0.518258675 1.088354891 -0.635111979
[14,] -0.653210929 -0.518258675 1.088354891
[15,] -0.640406984 -0.653210929 -0.518258675
[16,] -1.512711659 -0.640406984 -0.653210929
[17,] -0.387569937 -0.493331874 0.061669028
[18,] 0.551567240 -0.387569937 -0.493331874
[19,] 0.822240329 0.551567240 -0.387569937
[20,] 1.302924784 0.822240329 0.551567240
[21,] 1.425491614 1.302924784 0.822240329
[22,] 1.561752949 1.425491614 1.302924784
[23,] 1.041272334 1.561752949 1.425491614
[24,] 0.735231894 1.041272334 1.561752949
[25,] 0.323973139 0.735231894 1.041272334
[26,] -0.246097747 0.323973139 0.735231894
[27,] -0.107455145 -0.246097747 0.323973139
[28,] -1.415309011 -0.107455145 -0.246097747
[29,] -0.007243569 0.940657778 1.761401140
[30,] 0.065633161 -0.007243569 0.940657778
[31,] -0.453868848 0.065633161 -0.007243569
[32,] 0.045992570 0.569107040 -0.135474869
[33,] 0.253181206 0.045992570 0.569107040
[34,] 1.454893871 0.253181206 0.045992570
[35,] 0.264543085 1.454893871 0.253181206
[36,] 2.310053311 0.264543085 1.454893871
[37,] 0.086844882 1.529898422 -0.838967934
[38,] 1.841855597 -0.036323867 1.690396079
[39,] -0.266898797 1.841855597 -0.036323867
[40,] 0.933670592 0.166194483 -0.266898797
[41,] 0.031741882 0.933670592 0.166194483
[42,] -0.745946946 0.031741882 0.933670592
[43,] 0.010687685 -0.745946946 0.031741882
[44,] -0.509192914 0.010687685 -0.745946946
[45,] -0.116190200 -0.509192914 0.010687685
[46,] 0.101636394 -0.116190200 -0.509192914
[47,] -0.403346033 0.101636394 -0.116190200
[48,] 0.594358237 -0.403346033 0.101636394
[49,] -0.273570019 0.594358237 -0.403346033
[50,] 0.560568578 -0.273570019 0.594358237
[51,] 1.100196148 0.560568578 -0.273570019
[52,] 0.990514817 1.100196148 0.560568578
[53,] 0.471795193 0.990514817 1.100196148
[54,] 1.762013825 0.471795193 0.990514817
[55,] 1.669904908 1.762013825 0.471795193
[56,] 1.846880099 1.669904908 1.762013825
[57,] 1.535404214 1.846880099 1.669904908
[58,] 0.679385027 1.535404214 1.846880099
[59,] 2.298200812 0.679385027 1.535404214
[60,] 0.313983129 2.298200812 0.679385027
> Z<-bestMCD[,2:3]
> W<-bestMCD[,1]
> model<-lm(W~Z)
> parameter_estimates<-coef(model)
> #Calculate MDA of residuals
> mda<-mean(abs(residuals))
> mda
[1] 0.6772515
> c<-2
> σ<-mda
> M<-10000
> library(gurobi)
Loading required package: slam
> env <- gurobi::gurobi_env()
Error: 'gurobi_env' is not an exported object from 'namespace:gurobi'
> T<-length(Z)
> T
[1] 120
> T<-length(bestMCD)
> T
[1] 180
> T<-length(Y)
> T
[1] 100
> # Length of the time series
> T <- length(Y)
> # Formulating the QMIP problem
> model <- list()
> model
list()
>
> # Decision variables
> model$vars <- addVars(1:2*T, lb=0, ub=1, vtype="B")
Error in addVars(1:2 * T, lb = 0, ub = 1, vtype = "B") :
could not find function "addVars"
> # Decision variables
> model$vars <- gurobi::addVar(obj = rep(0, 2*T), lb = rep(0, 2*T), ub = rep(1, 2*T), vtype = rep("B", 2*T))
Error: 'addVar' is not an exported object from 'namespace:gurobi'
and I donot Know how to continue
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Vassiliki,
I'm not that familiar with our R interface but looking through our examples it seems like there are some differences to the way you are modelling. Here is a link to the R examples for reference:
https://www.gurobi.com/documentation/10.0/examples/r_examples.html
and a link to the R API
https://www.gurobi.com/documentation/10.0/refman/r_api_details.htmlAre you basing your code off an example you have found somewhere?
- Riley
0
Post is closed for comments.
Comments
2 comments