|
From: | Atwood, Joseph |
Subject: | Re: Transladate problem from Octave to R |
Date: | Tue, 2 Aug 2022 20:14:34 +0000 |
Sebastian
The matrix arguments you are passing to the Rglpk_solve_LP function are not of conformable dimensions. (In addition, I don't think the Rglpk function will replicate the elements of argument dir
= c("<=", "<=", "<=") to be of the appropriate dimension either. With R the script dir=c("<=", "<=", "<=") will generate a vector of length of dimension 3)
From the code (and your email) the
object c is of length 30, the matrix A is a 21 by 30 matrix,
b is of length 50, the 'dir' vector will be of length 3, vlb is of length 30, vub is of length 30
In addition, if used, the types argument must be a character string vector. > str(ctype) chr [1:21] "S" "S" "S" "S" "S" "S" "S" "S" "S" "S" "S" "U" "U" "U" "U" "U" "U" "U" "U" "U" "U"
ctype is a vector
> str(vartype) chr [1:3, 1:10] "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C"
The matrix 3x10 vartype can be converted
to a vector using the command as.vector(vartype) but will read the matrix in column-major-order.
In any case in the command:
Rglpk_solve_LP(obj
= c, mat = A, dir = c("<=", "<=", "<="), rhs = b)
the length of the c vector (30) must match the number of columns in the A matrix (30), the length of the b vector i.e. (50) must match
the number of rows in the A matrix (21), and the length of the dir vector (3) must match the number of rows in the A matrix (21).
This is why the error message:
Error in Rglpk_call(obj = obj, mat = mat, dir = dir, rhs = rhs, bounds = bounds, : Arguments 'mat', 'dir', and/or 'rhs' not conformable.
Joseph Atwood
208 Linfield Hall
Montana State University
Bozeman, MT 59717
406-994-5614
From: Help-glpk <help-glpk-bounces+jatwood=montana.edu@gnu.org> on behalf of Sebastián Kruk Gencarelli <zloto_kruk@hotmail.com>
Sent: Tuesday, August 2, 2022 9:18 AM To: help-glpk@gnu.org <help-glpk@gnu.org> Subject: Transladate problem from Octave to R **External Sender** Dear glpk-users,
In Octave:
[z_opt,f_min,errnum,extra]=glpk(c,A,b,vlb,vub,ctype,vartype,sense)
How can be transformed to use it in R?
If I use it:
Rglpk_solve_LP(obj = c, mat = A, dir = c("<=", "<=", "<="), rhs = b)
Give me an error:
Error in Rglpk_call(obj = obj, mat = mat, dir = dir, rhs = rhs, bounds = bounds, : Arguments 'mat', 'dir', and/or 'rhs' not conformable.
Where:
> str(c) num [1:30, 1] 0 0 0 0 0 0 0 0 0 0 ...
> str(A) num [1:21, 1:30] 1 0 0 0 0 0 0 0 0 0 ...
> str(b) num [1:50, 1] 22 37 37 37 37 37 37 37 37 37 ...
> str(vlb) num [1:30, 1] 0 0 0 0 0 0 0 0 0 0 ...
> str(vub) num [1:30, 1] 150 150 150 150 150 150 150 150 150 150 ...
> str(ctype) chr [1:21] "S" "S" "S" "S" "S" "S" "S" "S" "S" "S" "S" "U" "U" "U" "U" "U" "U" "U" "U" "U" "U"
> str(vartype) chr [1:3, 1:10] "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" "C" "I" "C" - attr(*, "dimnames")=List of 2 ..$ : chr [1:3] "varcont" "varint" "varcont" ..$ : NULL
> str(sense) num 1
My script in R is:
#1) Datos
#Período de estudio (días) T = 10
#Demanda diaria dda = 37*matrix(1,T,1)
#Capacidad de camión (m3) K=30
#Capacidad de tanque (m3) cap_tqe=40
#Stock inicial (m3) s0=15
#Max cantidad diaria de camiones y_max=5
#Max volumen entrega diaria x_max=K*y_max
#2) Modelo sin entrega los domingos
#Variables: z=[x; y; s], entega x(t), cant camiones y(t), stock final s(t) c = matrix(rep(c(0,1,0),each = T)) #solo cuestan los viajes vlb = matrix(rep(0,3*T)) vub = matrix(c(x_max*rep(1,T),y_max*rep(1,T), cap_tqe*rep(1,T))) #
#Restricciones grupo1: balanceen cada t, s(t)=s(t-1)+x(t)-dda(t) A1=matrix(0,nrow=T,ncol=3*T) A1[,1:T]=diag(1,dim(A1[,1:T])) library(pracma) A1[,(2*T+1):(3*T)]=-diag(1,T,T)+Diag(rep(1,T-1),-1) b1 = dda b1[1] = b1[1]-s0
#Restricciones grupo2: balanceen cada t, s(t)=s(t-1)+x(t)-dda(t) n7=floor(T/7) A2=matrix(0,ncol=3*T,nrow=n7) b2=matrix(0,nrow=3*T,ncol=1) ifelse(rem(col(A2),7)==0,1,0)
#Restricciones grupo3: vol entregado menor y cant camiones, x(t)<K*y(t) A3=matrix(0,nrow=T,ncol=3*T) A3[,1:T]=diag(1,T) A3[,(T+1):(2*T)]=diag(-K,T) b3=matrix(0,nrow=T,ncol=1)
# A = rbind(A1,A2,A3) b = rbind(b1,b2,b3)
#Tipo de variables y restricciones varint=rep("I",T) varcont=rep("C",T) ctype=rep("S",T) vartype=rbind(varcont, varint, varcont) ctype=c(ctype, rep("S",n7)) ctype=c(ctype, rep("U",T))
# sense=1;
Thanks in advance!
See you!
Sebastián.
|
[Prev in Thread] | Current Thread | [Next in Thread] |