|
|
Hi All,
I am facing an issue with an optimization problem which I am trying to solve using NlcOptim package in R. I have tried reaching out to the package maintainer but not received any response, hence posting this here.
Below is the code snippet I am using:
#Optimization
obj_F <- function(vect_mat){
return (sum((c(InputTM) - vect_mat)^2))
}
numel = nrow(InputTM)*ncol(InputTM)
opt_vect = solnl(X=c(InputTM), objfun=obj_F, A=-constr_new, B=-x_than0, Aeq=as.matrix(aeq2), Beq=beq2, lb=c(rep(0,numel)),ub=c(rep(1,numel)),tolX = 0)
I am attaching in the email the data being used as function arguments.
Input_TM is a 9*9 matrix
Constr_new is a 120*81 matrix
x_than0 is a 120*1 matrix
aeq2 is a 17*81 matrix
beq2 is a 17*1 matrix
Below is the error I am getting :
R> opt_vect = solnl(X=c(InputTM), objfun=obj_F, A=-constr_new, B=-x_than0, Aeq=as.matrix(aeq2), Beq=beq2, lb=c(rep(0,numel)),ub=c(rep(1,numel)),tolX = 0)
Error in as.matrix(A %*% Xtarget) - matrix(B, ncol = 1) :
non-conformable arrays
Calls: solnl -> rbind -> rbind
In addition: Warning message:
In rbind(rbind(lbright, ubright), B) :
number of columns of result is not a multiple of vector length (arg 2)
Calls: solnl -> rbind
Enter a frame number, or 0 to exit
1: solnl(X = c(InputTM), objfun = obj_F, A = -constr_new, B = -x_than0, Aeq = as.matrix(aeq2), Beq = beq2, lb =
2: rbind(rbind(rbind(Aeq %*% Xtarget - Beq, as.matrix(nceq)), as.matrix(A %*% Xtarget) - matrix(B, ncol = 1)), a
3: rbind(rbind(Aeq %*% Xtarget - Beq, as.matrix(nceq)), as.matrix(A %*% Xtarget) - matrix(B, ncol = 1))
Can you kindly help with this? I am mostly sure that the constraint matrices have been correctly formulated. Am I going wrong with the way I am specifying the arguments?
Thanks a lot for any help any of you can offer.
Thanks and Regards,
Aveek Mukhopadhyay
<!--#yiv2239857533 _filtered #yiv2239857533 {font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;} _filtered #yiv2239857533 {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;} _filtered #yiv2239857533 {font-family:"Lucida Console";panose-1:2 11 6 9 4 5 4 2 2 4;}#yiv2239857533 #yiv2239857533 p.yiv2239857533MsoNormal, #yiv2239857533 li.yiv2239857533MsoNormal, #yiv2239857533 div.yiv2239857533MsoNormal {margin:0in;margin-bottom:.0001pt;font-size:11.0pt;font-family:"Calibri", sans-serif;}#yiv2239857533 a:link, #yiv2239857533 span.yiv2239857533MsoHyperlink {color:#0563C1;text-decoration:underline;}#yiv2239857533 a:visited, #yiv2239857533 span.yiv2239857533MsoHyperlinkFollowed {color:#954F72;text-decoration:underline;}#yiv2239857533 pre {margin:0in;margin-bottom:.0001pt;font-size:10.0pt;font-family:"Courier New";}#yiv2239857533 span.yiv2239857533HTMLPreformattedChar {font-family:"Courier New";}#yiv2239857533 span.yiv2239857533EmailStyle19 {font-family:"Calibri", sans-serif;color:windowtext;}#yiv2239857533 span.yiv2239857533gnkrckgcmsb {}#yiv2239857533 span.yiv2239857533gnkrckgcmrb {}#yiv2239857533 span.yiv2239857533gnkrckgcasb {}#yiv2239857533 span.yiv2239857533gnkrckgcgsb {}#yiv2239857533 span.yiv2239857533EmailStyle24 {font-family:"Calibri", sans-serif;color:#1F497D;}#yiv2239857533 span.yiv2239857533EmailStyle25 {font-family:"Calibri", sans-serif;color:windowtext;}#yiv2239857533 .yiv2239857533MsoChpDefault {font-size:10.0pt;} _filtered #yiv2239857533 {margin:1.0in 1.0in 1.0in 1.0in;}#yiv2239857533 div.yiv2239857533WordSection1 {}-->
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
Hi Aveek,
1. This is an "all-text" mailing list. Your attachment did not come
through.
You can check out the posting guide (see the link at the bottom of your
email)
and/or
use dput(...) on your structures and paste them into your email so
that members of the list can try to reproduce the problem.
2. One way to check out whether you are using a package correctly is to try
a tiny example that you can calculate by hand, and see if you can reproduce
the solution via the package.
e.g. instead of a 9x9 matrix (hence 81 dimensional problem in your
case), try a 2x2 matrix with maybe just one or two constraints.
HTH,
Eric
On Tue, Dec 11, 2018 at 1:18 PM aveek via R-help < [hidden email]>
wrote:
> Hi All,
> I am facing an issue with an optimization problem which I am trying to
> solve using NlcOptim package in R. I have tried reaching out to the package
> maintainer but not received any response, hence posting this here.
>
>
> Below is the code snippet I am using:
>
>
>
> #Optimization
>
> obj_F <- function(vect_mat){
>
> return (sum((c(InputTM) - vect_mat)^2))
>
> }
>
>
>
> numel = nrow(InputTM)*ncol(InputTM)
>
> opt_vect = solnl(X=c(InputTM), objfun=obj_F, A=-constr_new, B=-x_than0,
> Aeq=as.matrix(aeq2), Beq=beq2, lb=c(rep(0,numel)),ub=c(rep(1,numel)),tolX =
> 0)
>
>
>
> I am attaching in the email the data being used as function arguments.
>
>
>
> Input_TM is a 9*9 matrix
>
> Constr_new is a 120*81 matrix
>
> x_than0 is a 120*1 matrix
>
> aeq2 is a 17*81 matrix
>
> beq2 is a 17*1 matrix
>
>
>
> Below is the error I am getting :
>
>
>
> R> opt_vect = solnl(X=c(InputTM), objfun=obj_F, A=-constr_new,
> B=-x_than0, Aeq=as.matrix(aeq2), Beq=beq2,
> lb=c(rep(0,numel)),ub=c(rep(1,numel)),tolX = 0)
>
> Error in as.matrix(A %*% Xtarget) - matrix(B, ncol = 1) :
>
> non-conformable arrays
>
> Calls: solnl -> rbind -> rbind
>
> In addition: Warning message:
>
> In rbind(rbind(lbright, ubright), B) :
>
> number of columns of result is not a multiple of vector length (arg 2)
>
> Calls: solnl -> rbind
>
>
>
> Enter a frame number, or 0 to exit
>
>
>
> 1: solnl(X = c(InputTM), objfun = obj_F, A = -constr_new, B = -x_than0,
> Aeq = as.matrix(aeq2), Beq = beq2, lb =
>
> 2: rbind(rbind(rbind(Aeq %*% Xtarget - Beq, as.matrix(nceq)), as.matrix(A
> %*% Xtarget) - matrix(B, ncol = 1)), a
>
> 3: rbind(rbind(Aeq %*% Xtarget - Beq, as.matrix(nceq)), as.matrix(A %*%
> Xtarget) - matrix(B, ncol = 1))
>
>
>
>
>
>
>
> Can you kindly help with this? I am mostly sure that the constraint
> matrices have been correctly formulated. Am I going wrong with the way I am
> specifying the arguments?
>
> Thanks a lot for any help any of you can offer.
>
>
>
> Thanks and Regards,
>
> Aveek Mukhopadhyay
>
>
>
>
> <!--#yiv2239857533 _filtered #yiv2239857533 {font-family:"Cambria
> Math";panose-1:2 4 5 3 5 4 6 3 2 4;} _filtered #yiv2239857533
> {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;} _filtered
> #yiv2239857533 {font-family:"Lucida Console";panose-1:2 11 6 9 4 5 4 2 2
> 4;}#yiv2239857533 #yiv2239857533 p.yiv2239857533MsoNormal, #yiv2239857533
> li.yiv2239857533MsoNormal, #yiv2239857533 div.yiv2239857533MsoNormal
> {margin:0in;margin-bottom:.0001pt;font-size:11.0pt;font-family:"Calibri",
> sans-serif;}#yiv2239857533 a:link, #yiv2239857533
> span.yiv2239857533MsoHyperlink
> {color:#0563C1;text-decoration:underline;}#yiv2239857533 a:visited,
> #yiv2239857533 span.yiv2239857533MsoHyperlinkFollowed
> {color:#954F72;text-decoration:underline;}#yiv2239857533 pre
> {margin:0in;margin-bottom:.0001pt;font-size:10.0pt;font-family:"Courier
> New";}#yiv2239857533 span.yiv2239857533HTMLPreformattedChar
> {font-family:"Courier New";}#yiv2239857533 span.yiv2239857533EmailStyle19
> {font-family:"Calibri", sans-serif;color:windowtext;}#yiv2239857533
> span.yiv2239857533gnkrckgcmsb {}#yiv2239857533
> span.yiv2239857533gnkrckgcmrb {}#yiv2239857533
> span.yiv2239857533gnkrckgcasb {}#yiv2239857533
> span.yiv2239857533gnkrckgcgsb {}#yiv2239857533
> span.yiv2239857533EmailStyle24 {font-family:"Calibri",
> sans-serif;color:#1F497D;}#yiv2239857533 span.yiv2239857533EmailStyle25
> {font-family:"Calibri", sans-serif;color:windowtext;}#yiv2239857533
> .yiv2239857533MsoChpDefault {font-size:10.0pt;} _filtered #yiv2239857533
> {margin:1.0in 1.0in 1.0in 1.0in;}#yiv2239857533
> div.yiv2239857533WordSection1 {}-->
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
Hello Eric,
Thanks for your response and suggestions.
I have used dput() on the R objects - sharing below so that it is possible for anyone to recreate the situation.
I have still kept it as a 9*9 matrix but for simplicity we now only have 2 equality and 2 non equality constraints.
Thanks again for your help.
InputTM
structure(c(0.813231189406663, 0.0199464964676128, 0.00100552815128915,
0.000465771436428336, 0.000736922016196076, 0.00203037431732662,
0.000596998285890709, 0.0011699714577823, 0, 0.103116692172408,
0.751775368068589, 0.0160957427707042, 0.00285542569941823, 0.0020295541916448,
0.00954562743564027, 0.00173399818906894, 0.00292299139663608,
0, 0.0481959576543631, 0.177032393868544, 0.811609524051149,
0.146703962329218, 0.0698423269415636, 0.168241524872922, 0.0505757280338206,
0.0324917017565673, 0, 0.026504623193874, 0.038430496838613,
0.134709744786799, 0.758322716164413, 0.176013939161559, 0.234265359508999,
0.108188555487004, 0.0476663548325017, 0, 0.00520614395937929,
0.00868690292550468, 0.0223895752360805, 0.0581458712447338,
0.681496895121054, 0.0733970775224908, 0.0508491985259732, 0.0268876385360338,
0, 0.000749001395622802, 0.00181690494827145, 0.00317194515883476,
0.00705434604769267, 0.0211989316284324, 0.464732208379131, 0.0165146818291576,
0.00721872506710652, 0, 0.000960493069403903, 0.00138444384054219,
0.00703528202498607, 0.0163983255053438, 0.0301780379843763,
0.0280699612529658, 0.491157627745315, 0.0235353949469527, 0,
0.00112628575287418, 0.000477300396419488, 0.00223238360574478,
0.00521558462566306, 0.00925149338117537, 0.00878272484051914,
0.163654071683595, 0.611806567272906, 0, 0.000909613395411595,
0.000449692645903539, 0.00175027421441265, 0.00483799694708872,
0.00925189957399783, 0.0109351418700064, 0.116729140220175, 0.246300654733514,
1), .Dim = c(9L, 9L))
Constr_new
structure(c(1, 0, -1, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(2L,
81L), .Dimnames = list(NULL, c("X11", "X12", "X13", "X14", "X15",
"X16", "X17", "X18", "X19", "X21", "X22", "X23", "X24", "X25",
"X26", "X27", "X28", "X29", "X31", "X32", "X33", "X34", "X35",
"X36", "X37", "X38", "X39", "X41", "X42", "X43", "X44", "X45",
"X46", "X47", "X48", "X49", "X51", "X52", "X53", "X54", "X55",
"X56", "X57", "X58", "X59", "X61", "X62", "X63", "X64", "X65",
"X66", "X67", "X68", "X69", "X71", "X72", "X73", "X74", "X75",
"X76", "X77", "X78", "X79", "X81", "X82", "X83", "X84", "X85",
"X86", "X87", "X88", "X89", "X91", "X92", "X93", "X94", "X95",
"X96", "X97", "X98", "X99")))
x_than0
c(1e-04, 1e-04)
aeq2
structure(list(X1 = c(1, 0), X2 = c(1, 0), X3 = c(1, 0), X4 = c(1,
0), X5 = c(1, 0), X6 = c(1, 0), X7 = c(1, 0), X8 = c(1, 0), X9 = c(1,
0), X10 = c(0, 1), X11 = c(0, 1), X12 = c(0, 1), X13 = c(0, 1
), X14 = c(0, 1), X15 = c(0, 1), X16 = c(0, 1), X17 = c(0, 1),
X18 = c(0, 1), X19 = c(0, 0), X20 = c(0, 0), X21 = c(0, 0
), X22 = c(0, 0), X23 = c(0, 0), X24 = c(0, 0), X25 = c(0,
0), X26 = c(0, 0), X27 = c(0, 0), X28 = c(0, 0), X29 = c(0,
0), X30 = c(0, 0), X31 = c(0, 0), X32 = c(0, 0), X33 = c(0,
0), X34 = c(0, 0), X35 = c(0, 0), X36 = c(0, 0), X37 = c(0,
0), X38 = c(0, 0), X39 = c(0, 0), X40 = c(0, 0), X41 = c(0,
0), X42 = c(0, 0), X43 = c(0, 0), X44 = c(0, 0), X45 = c(0,
0), X46 = c(0, 0), X47 = c(0, 0), X48 = c(0, 0), X49 = c(0,
0), X50 = c(0, 0), X51 = c(0, 0), X52 = c(0, 0), X53 = c(0,
0), X54 = c(0, 0), X55 = c(0, 0), X56 = c(0, 0), X57 = c(0,
0), X58 = c(0, 0), X59 = c(0, 0), X60 = c(0, 0), X61 = c(0,
0), X62 = c(0, 0), X63 = c(0, 0), X64 = c(0, 0), X65 = c(0,
0), X66 = c(0, 0), X67 = c(0, 0), X68 = c(0, 0), X69 = c(0,
0), X70 = c(0, 0), X71 = c(0, 0), X72 = c(0, 0), X73 = c(0,
0), X74 = c(0, 0), X75 = c(0, 0), X76 = c(0, 0), X77 = c(0,
0), X78 = c(0, 0), X79 = c(0, 0), X80 = c(0, 0), X81 = c(0,
0)), .Names = c("X1", "X2", "X3", "X4", "X5", "X6", "X7",
"X8", "X9", "X10", "X11", "X12", "X13", "X14", "X15", "X16",
"X17", "X18", "X19", "X20", "X21", "X22", "X23", "X24", "X25",
"X26", "X27", "X28", "X29", "X30", "X31", "X32", "X33", "X34",
"X35", "X36", "X37", "X38", "X39", "X40", "X41", "X42", "X43",
"X44", "X45", "X46", "X47", "X48", "X49", "X50", "X51", "X52",
"X53", "X54", "X55", "X56", "X57", "X58", "X59", "X60", "X61",
"X62", "X63", "X64", "X65", "X66", "X67", "X68", "X69", "X70",
"X71", "X72", "X73", "X74", "X75", "X76", "X77", "X78", "X79",
"X80", "X81"), row.names = 1:2, class = "data.frame")
beq2
c(1, 1)
Regards,
Aveek
Sent from Yahoo Mail on Android
On Tue, Dec 11, 2018 at 6:25 PM, Eric Berger< [hidden email]> wrote: Hi Aveek,1. This is an "all-text" mailing list. Your attachment did not come through. You can check out the posting guide (see the link at the bottom of your email) and/or use dput(...) on your structures and paste them into your email so that members of the list can try to reproduce the problem.2. One way to check out whether you are using a package correctly is to try a tiny example that you can calculate by hand, and see if you can reproduce the solution via the package. e.g. instead of a 9x9 matrix (hence 81 dimensional problem in your case), try a 2x2 matrix with maybe just one or two constraints.
HTH,Eric
On Tue, Dec 11, 2018 at 1:18 PM aveek via R-help < [hidden email]> wrote:
Hi All,
I am facing an issue with an optimization problem which I am trying to solve using NlcOptim package in R. I have tried reaching out to the package maintainer but not received any response, hence posting this here.
Below is the code snippet I am using:
#Optimization
obj_F <- function(vect_mat){
return (sum((c(InputTM) - vect_mat)^2))
}
numel = nrow(InputTM)*ncol(InputTM)
opt_vect = solnl(X=c(InputTM), objfun=obj_F, A=-constr_new, B=-x_than0, Aeq=as.matrix(aeq2), Beq=beq2, lb=c(rep(0,numel)),ub=c(rep(1,numel)),tolX = 0)
I am attaching in the email the data being used as function arguments.
Input_TM is a 9*9 matrix
Constr_new is a 120*81 matrix
x_than0 is a 120*1 matrix
aeq2 is a 17*81 matrix
beq2 is a 17*1 matrix
Below is the error I am getting :
R> opt_vect = solnl(X=c(InputTM), objfun=obj_F, A=-constr_new, B=-x_than0, Aeq=as.matrix(aeq2), Beq=beq2, lb=c(rep(0,numel)),ub=c(rep(1,numel)),tolX = 0)
Error in as.matrix(A %*% Xtarget) - matrix(B, ncol = 1) :
non-conformable arrays
Calls: solnl -> rbind -> rbind
In addition: Warning message:
In rbind(rbind(lbright, ubright), B) :
number of columns of result is not a multiple of vector length (arg 2)
Calls: solnl -> rbind
Enter a frame number, or 0 to exit
1: solnl(X = c(InputTM), objfun = obj_F, A = -constr_new, B = -x_than0, Aeq = as.matrix(aeq2), Beq = beq2, lb =
2: rbind(rbind(rbind(Aeq %*% Xtarget - Beq, as.matrix(nceq)), as.matrix(A %*% Xtarget) - matrix(B, ncol = 1)), a
3: rbind(rbind(Aeq %*% Xtarget - Beq, as.matrix(nceq)), as.matrix(A %*% Xtarget) - matrix(B, ncol = 1))
Can you kindly help with this? I am mostly sure that the constraint matrices have been correctly formulated. Am I going wrong with the way I am specifying the arguments?
Thanks a lot for any help any of you can offer.
Thanks and Regards,
Aveek Mukhopadhyay
<!--#yiv2239857533 _filtered #yiv2239857533 {font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;} _filtered #yiv2239857533 {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;} _filtered #yiv2239857533 {font-family:"Lucida Console";panose-1:2 11 6 9 4 5 4 2 2 4;}#yiv2239857533 #yiv2239857533 p.yiv2239857533MsoNormal, #yiv2239857533 li.yiv2239857533MsoNormal, #yiv2239857533 div.yiv2239857533MsoNormal {margin:0in;margin-bottom:.0001pt;font-size:11.0pt;font-family:"Calibri", sans-serif;}#yiv2239857533 a:link, #yiv2239857533 span.yiv2239857533MsoHyperlink {color:#0563C1;text-decoration:underline;}#yiv2239857533 a:visited, #yiv2239857533 span.yiv2239857533MsoHyperlinkFollowed {color:#954F72;text-decoration:underline;}#yiv2239857533 pre {margin:0in;margin-bottom:.0001pt;font-size:10.0pt;font-family:"Courier New";}#yiv2239857533 span.yiv2239857533HTMLPreformattedChar {font-family:"Courier New";}#yiv2239857533 span.yiv2239857533EmailStyle19 {font-family:"Calibri", sans-serif;color:windowtext;}#yiv2239857533 span.yiv2239857533gnkrckgcmsb {}#yiv2239857533 span.yiv2239857533gnkrckgcmrb {}#yiv2239857533 span.yiv2239857533gnkrckgcasb {}#yiv2239857533 span.yiv2239857533gnkrckgcgsb {}#yiv2239857533 span.yiv2239857533EmailStyle24 {font-family:"Calibri", sans-serif;color:#1F497D;}#yiv2239857533 span.yiv2239857533EmailStyle25 {font-family:"Calibri", sans-serif;color:windowtext;}#yiv2239857533 .yiv2239857533MsoChpDefault {font-size:10.0pt;} _filtered #yiv2239857533 {margin:1.0in 1.0in 1.0in 1.0in;}#yiv2239857533 div.yiv2239857533WordSection1 {}-->
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
In reply to this post by R help mailing list-2
This is still not complete: `x_than0` is missing.
`Constr_new` is written with a capital 'C'.
And aeq2 is a list of column vectors, not a matrix.
Setting the tolerance to 0 does not seem to be a good idea.
Making aeq2 a matrix and adding `x_than0 <- matrix(c(1, 1))`, then
aeq2 <- as.matrix(aeq2)
x_than0 <- matrix(c(1, 1))
NlcOptim::solnl(X=c(InputTM), objfun=obj_F, A=-Constr_new, B=-x_than0,
Aeq=as.matrix(aeq2), Beq=beq2,
lb=c(rep(0,numel)),ub=c(rep(1,numel)), tolX = 0)
will indeed return in the same error, while it runs without error if you
either leave out the inequality constraints or the bounds constraints. So
I guess there may be a bug when the function internally combines these
constraints and the bounds.
You could / should write to the maintainer. I know he is very responsive.
For the moment, you can combine the bounds constraints and the lower and
upper bounds yourself:
myA <- rbind(-Constr_new, diag(-1,numel), diag(1,numel))
myB <- c(-x_than0, rep(0,numel), rep(1,numel))
NlcOptim::solnl(X=c(InputTM), objfun=obj_F, A=myA, B=myB,
Aeq=as.matrix(aeq2), Beq=beq2)
returns "constraints are inconsistent, no solution!", but that may be the
case because I don't know your `x_than` value.
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
In reply to this post by R help mailing list-2
Hello,
Can anyone plz help with the below problem?
Thanks,Aveek
Sent from Yahoo Mail on Android
On Wed, Dec 12, 2018 at 12:14 PM, aveek< [hidden email]> wrote:
Hello Eric,
Thanks for your response and suggestions.
I have used dput() on the R objects - sharing below so that it is possible for anyone to recreate the situation.
I have still kept it as a 9*9 matrix but for simplicity we now only have 2 equality and 2 non equality constraints.
Thanks again for your help.
InputTM
structure(c(0.813231189406663, 0.0199464964676128, 0.00100552815128915,
0.000465771436428336, 0.000736922016196076, 0.00203037431732662,
0.000596998285890709, 0.0011699714577823, 0, 0.103116692172408,
0.751775368068589, 0.0160957427707042, 0.00285542569941823, 0.0020295541916448,
0.00954562743564027, 0.00173399818906894, 0.00292299139663608,
0, 0.0481959576543631, 0.177032393868544, 0.811609524051149,
0.146703962329218, 0.0698423269415636, 0.168241524872922, 0.0505757280338206,
0.0324917017565673, 0, 0.026504623193874, 0.038430496838613,
0.134709744786799, 0.758322716164413, 0.176013939161559, 0.234265359508999,
0.108188555487004, 0.0476663548325017, 0, 0.00520614395937929,
0.00868690292550468, 0.0223895752360805, 0.0581458712447338,
0.681496895121054, 0.0733970775224908, 0.0508491985259732, 0.0268876385360338,
0, 0.000749001395622802, 0.00181690494827145, 0.00317194515883476,
0.00705434604769267, 0.0211989316284324, 0.464732208379131, 0.0165146818291576,
0.00721872506710652, 0, 0.000960493069403903, 0.00138444384054219,
0.00703528202498607, 0.0163983255053438, 0.0301780379843763,
0.0280699612529658, 0.491157627745315, 0.0235353949469527, 0,
0.00112628575287418, 0.000477300396419488, 0.00223238360574478,
0.00521558462566306, 0.00925149338117537, 0.00878272484051914,
0.163654071683595, 0.611806567272906, 0, 0.000909613395411595,
0.000449692645903539, 0.00175027421441265, 0.00483799694708872,
0.00925189957399783, 0.0109351418700064, 0.116729140220175, 0.246300654733514,
1), .Dim = c(9L, 9L))
Constr_new
structure(c(1, 0, -1, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(2L,
81L), .Dimnames = list(NULL, c("X11", "X12", "X13", "X14", "X15",
"X16", "X17", "X18", "X19", "X21", "X22", "X23", "X24", "X25",
"X26", "X27", "X28", "X29", "X31", "X32", "X33", "X34", "X35",
"X36", "X37", "X38", "X39", "X41", "X42", "X43", "X44", "X45",
"X46", "X47", "X48", "X49", "X51", "X52", "X53", "X54", "X55",
"X56", "X57", "X58", "X59", "X61", "X62", "X63", "X64", "X65",
"X66", "X67", "X68", "X69", "X71", "X72", "X73", "X74", "X75",
"X76", "X77", "X78", "X79", "X81", "X82", "X83", "X84", "X85",
"X86", "X87", "X88", "X89", "X91", "X92", "X93", "X94", "X95",
"X96", "X97", "X98", "X99")))
x_than0
c(1e-04, 1e-04)
aeq2
structure(list(X1 = c(1, 0), X2 = c(1, 0), X3 = c(1, 0), X4 = c(1,
0), X5 = c(1, 0), X6 = c(1, 0), X7 = c(1, 0), X8 = c(1, 0), X9 = c(1,
0), X10 = c(0, 1), X11 = c(0, 1), X12 = c(0, 1), X13 = c(0, 1
), X14 = c(0, 1), X15 = c(0, 1), X16 = c(0, 1), X17 = c(0, 1),
X18 = c(0, 1), X19 = c(0, 0), X20 = c(0, 0), X21 = c(0, 0
), X22 = c(0, 0), X23 = c(0, 0), X24 = c(0, 0), X25 = c(0,
0), X26 = c(0, 0), X27 = c(0, 0), X28 = c(0, 0), X29 = c(0,
0), X30 = c(0, 0), X31 = c(0, 0), X32 = c(0, 0), X33 = c(0,
0), X34 = c(0, 0), X35 = c(0, 0), X36 = c(0, 0), X37 = c(0,
0), X38 = c(0, 0), X39 = c(0, 0), X40 = c(0, 0), X41 = c(0,
0), X42 = c(0, 0), X43 = c(0, 0), X44 = c(0, 0), X45 = c(0,
0), X46 = c(0, 0), X47 = c(0, 0), X48 = c(0, 0), X49 = c(0,
0), X50 = c(0, 0), X51 = c(0, 0), X52 = c(0, 0), X53 = c(0,
0), X54 = c(0, 0), X55 = c(0, 0), X56 = c(0, 0), X57 = c(0,
0), X58 = c(0, 0), X59 = c(0, 0), X60 = c(0, 0), X61 = c(0,
0), X62 = c(0, 0), X63 = c(0, 0), X64 = c(0, 0), X65 = c(0,
0), X66 = c(0, 0), X67 = c(0, 0), X68 = c(0, 0), X69 = c(0,
0), X70 = c(0, 0), X71 = c(0, 0), X72 = c(0, 0), X73 = c(0,
0), X74 = c(0, 0), X75 = c(0, 0), X76 = c(0, 0), X77 = c(0,
0), X78 = c(0, 0), X79 = c(0, 0), X80 = c(0, 0), X81 = c(0,
0)), .Names = c("X1", "X2", "X3", "X4", "X5", "X6", "X7",
"X8", "X9", "X10", "X11", "X12", "X13", "X14", "X15", "X16",
"X17", "X18", "X19", "X20", "X21", "X22", "X23", "X24", "X25",
"X26", "X27", "X28", "X29", "X30", "X31", "X32", "X33", "X34",
"X35", "X36", "X37", "X38", "X39", "X40", "X41", "X42", "X43",
"X44", "X45", "X46", "X47", "X48", "X49", "X50", "X51", "X52",
"X53", "X54", "X55", "X56", "X57", "X58", "X59", "X60", "X61",
"X62", "X63", "X64", "X65", "X66", "X67", "X68", "X69", "X70",
"X71", "X72", "X73", "X74", "X75", "X76", "X77", "X78", "X79",
"X80", "X81"), row.names = 1:2, class = "data.frame")
beq2
c(1, 1)
Regards,
Aveek
Sent from Yahoo Mail on Android
On Tue, Dec 11, 2018 at 6:25 PM, Eric Berger< [hidden email]> wrote: Hi Aveek,1. This is an "all-text" mailing list. Your attachment did not come through. You can check out the posting guide (see the link at the bottom of your email) and/or use dput(...) on your structures and paste them into your email so that members of the list can try to reproduce the problem.2. One way to check out whether you are using a package correctly is to try a tiny example that you can calculate by hand, and see if you can reproduce the solution via the package. e.g. instead of a 9x9 matrix (hence 81 dimensional problem in your case), try a 2x2 matrix with maybe just one or two constraints.
HTH,Eric
On Tue, Dec 11, 2018 at 1:18 PM aveek via R-help < [hidden email]> wrote:
Hi All,
I am facing an issue with an optimization problem which I am trying to solve using NlcOptim package in R. I have tried reaching out to the package maintainer but not received any response, hence posting this here.
Below is the code snippet I am using:
#Optimization
obj_F <- function(vect_mat){
return (sum((c(InputTM) - vect_mat)^2))
}
numel = nrow(InputTM)*ncol(InputTM)
opt_vect = solnl(X=c(InputTM), objfun=obj_F, A=-constr_new, B=-x_than0, Aeq=as.matrix(aeq2), Beq=beq2, lb=c(rep(0,numel)),ub=c(rep(1,numel)),tolX = 0)
I am attaching in the email the data being used as function arguments.
Input_TM is a 9*9 matrix
Constr_new is a 120*81 matrix
x_than0 is a 120*1 matrix
aeq2 is a 17*81 matrix
beq2 is a 17*1 matrix
Below is the error I am getting :
R> opt_vect = solnl(X=c(InputTM), objfun=obj_F, A=-constr_new, B=-x_than0, Aeq=as.matrix(aeq2), Beq=beq2, lb=c(rep(0,numel)),ub=c(rep(1,numel)),tolX = 0)
Error in as.matrix(A %*% Xtarget) - matrix(B, ncol = 1) :
non-conformable arrays
Calls: solnl -> rbind -> rbind
In addition: Warning message:
In rbind(rbind(lbright, ubright), B) :
number of columns of result is not a multiple of vector length (arg 2)
Calls: solnl -> rbind
Enter a frame number, or 0 to exit
1: solnl(X = c(InputTM), objfun = obj_F, A = -constr_new, B = -x_than0, Aeq = as.matrix(aeq2), Beq = beq2, lb =
2: rbind(rbind(rbind(Aeq %*% Xtarget - Beq, as.matrix(nceq)), as.matrix(A %*% Xtarget) - matrix(B, ncol = 1)), a
3: rbind(rbind(Aeq %*% Xtarget - Beq, as.matrix(nceq)), as.matrix(A %*% Xtarget) - matrix(B, ncol = 1))
Can you kindly help with this? I am mostly sure that the constraint matrices have been correctly formulated. Am I going wrong with the way I am specifying the arguments?
Thanks a lot for any help any of you can offer.
Thanks and Regards,
Aveek Mukhopadhyay
<!--#yiv2239857533 _filtered #yiv2239857533 {font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;} _filtered #yiv2239857533 {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;} _filtered #yiv2239857533 {font-family:"Lucida Console";panose-1:2 11 6 9 4 5 4 2 2 4;}#yiv2239857533 #yiv2239857533 p.yiv2239857533MsoNormal, #yiv2239857533 li.yiv2239857533MsoNormal, #yiv2239857533 div.yiv2239857533MsoNormal {margin:0in;margin-bottom:.0001pt;font-size:11.0pt;font-family:"Calibri", sans-serif;}#yiv2239857533 a:link, #yiv2239857533 span.yiv2239857533MsoHyperlink {color:#0563C1;text-decoration:underline;}#yiv2239857533 a:visited, #yiv2239857533 span.yiv2239857533MsoHyperlinkFollowed {color:#954F72;text-decoration:underline;}#yiv2239857533 pre {margin:0in;margin-bottom:.0001pt;font-size:10.0pt;font-family:"Courier New";}#yiv2239857533 span.yiv2239857533HTMLPreformattedChar {font-family:"Courier New";}#yiv2239857533 span.yiv2239857533EmailStyle19 {font-family:"Calibri", sans-serif;color:windowtext;}#yiv2239857533 span.yiv2239857533gnkrckgcmsb {}#yiv2239857533 span.yiv2239857533gnkrckgcmrb {}#yiv2239857533 span.yiv2239857533gnkrckgcasb {}#yiv2239857533 span.yiv2239857533gnkrckgcgsb {}#yiv2239857533 span.yiv2239857533EmailStyle24 {font-family:"Calibri", sans-serif;color:#1F497D;}#yiv2239857533 span.yiv2239857533EmailStyle25 {font-family:"Calibri", sans-serif;color:windowtext;}#yiv2239857533 .yiv2239857533MsoChpDefault {font-size:10.0pt;} _filtered #yiv2239857533 {margin:1.0in 1.0in 1.0in 1.0in;}#yiv2239857533 div.yiv2239857533WordSection1 {}-->
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
Aveek,
Did you try contacting the package maintainer as Hans suggested?
On Mon, Jan 14, 2019 at 10:31 AM aveek < [hidden email]> wrote:
> Hello,
>
> Can anyone plz help with the below problem?
>
> Thanks,
> Aveek
>
> Sent from Yahoo Mail on Android
> < https://go.onelink.me/107872968?pid=InProduct&c=Global_Internal_YGrowth_AndroidEmailSig__AndroidUsers&af_wl=ym&af_sub1=Internal&af_sub2=Global_YGrowth&af_sub3=EmailSignature>
>
> On Wed, Dec 12, 2018 at 12:14 PM, aveek
> < [hidden email]> wrote:
>
> Hello Eric,
>
> Thanks for your response and suggestions.
>
> I have used dput() on the R objects - sharing below so that it is possible
> for anyone to recreate the situation.
>
> I have still kept it as a 9*9 matrix but for simplicity we now only have 2
> equality and 2 non equality constraints.
>
> Thanks again for your help.
>
> InputTM
>
> structure(c(0.813231189406663, 0.0199464964676128, 0.00100552815128915,
>
> 0.000465771436428336, 0.000736922016196076, 0.00203037431732662,
>
> 0.000596998285890709, 0.0011699714577823, 0, 0.103116692172408,
>
> 0.751775368068589, 0.0160957427707042, 0.00285542569941823,
> 0.0020295541916448,
>
> 0.00954562743564027, 0.00173399818906894, 0.00292299139663608,
>
> 0, 0.0481959576543631, 0.177032393868544, 0.811609524051149,
>
> 0.146703962329218, 0.0698423269415636, 0.168241524872922,
> 0.0505757280338206,
>
> 0.0324917017565673, 0, 0.026504623193874, 0.038430496838613,
>
> 0.134709744786799, 0.758322716164413, 0.176013939161559, 0.234265359508999,
>
> 0.108188555487004, 0.0476663548325017, 0, 0.00520614395937929,
>
> 0.00868690292550468, 0.0223895752360805, 0.0581458712447338,
>
> 0.681496895121054, 0.0733970775224908, 0.0508491985259732,
> 0.0268876385360338,
>
> 0, 0.000749001395622802, 0.00181690494827145, 0.00317194515883476,
>
> 0.00705434604769267, 0.0211989316284324, 0.464732208379131,
> 0.0165146818291576,
>
> 0.00721872506710652, 0, 0.000960493069403903, 0.00138444384054219,
>
> 0.00703528202498607, 0.0163983255053438, 0.0301780379843763,
>
> 0.0280699612529658, 0.491157627745315, 0.0235353949469527, 0,
>
> 0.00112628575287418, 0.000477300396419488, 0.00223238360574478,
>
> 0.00521558462566306, 0.00925149338117537, 0.00878272484051914,
>
> 0.163654071683595, 0.611806567272906, 0, 0.000909613395411595,
>
> 0.000449692645903539, 0.00175027421441265, 0.00483799694708872,
>
> 0.00925189957399783, 0.0109351418700064, 0.116729140220175,
> 0.246300654733514,
>
> 1), .Dim = c(9L, 9L))
>
>
>
>
>
> Constr_new
>
> structure(c(1, 0, -1, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
>
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
>
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
>
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
>
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
>
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
>
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
>
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(2L,
>
> 81L), .Dimnames = list(NULL, c("X11", "X12", "X13", "X14", "X15",
>
> "X16", "X17", "X18", "X19", "X21", "X22", "X23", "X24", "X25",
>
> "X26", "X27", "X28", "X29", "X31", "X32", "X33", "X34", "X35",
>
> "X36", "X37", "X38", "X39", "X41", "X42", "X43", "X44", "X45",
>
> "X46", "X47", "X48", "X49", "X51", "X52", "X53", "X54", "X55",
>
> "X56", "X57", "X58", "X59", "X61", "X62", "X63", "X64", "X65",
>
> "X66", "X67", "X68", "X69", "X71", "X72", "X73", "X74", "X75",
>
> "X76", "X77", "X78", "X79", "X81", "X82", "X83", "X84", "X85",
>
> "X86", "X87", "X88", "X89", "X91", "X92", "X93", "X94", "X95",
>
> "X96", "X97", "X98", "X99")))
>
>
>
>
>
> x_than0
>
> c(1e-04, 1e-04)
>
>
>
> aeq2
>
> structure(list(X1 = c(1, 0), X2 = c(1, 0), X3 = c(1, 0), X4 = c(1,
>
> 0), X5 = c(1, 0), X6 = c(1, 0), X7 = c(1, 0), X8 = c(1, 0), X9 = c(1,
>
> 0), X10 = c(0, 1), X11 = c(0, 1), X12 = c(0, 1), X13 = c(0, 1
>
> ), X14 = c(0, 1), X15 = c(0, 1), X16 = c(0, 1), X17 = c(0, 1),
>
> X18 = c(0, 1), X19 = c(0, 0), X20 = c(0, 0), X21 = c(0, 0
>
> ), X22 = c(0, 0), X23 = c(0, 0), X24 = c(0, 0), X25 = c(0,
>
> 0), X26 = c(0, 0), X27 = c(0, 0), X28 = c(0, 0), X29 = c(0,
>
> 0), X30 = c(0, 0), X31 = c(0, 0), X32 = c(0, 0), X33 = c(0,
>
> 0), X34 = c(0, 0), X35 = c(0, 0), X36 = c(0, 0), X37 = c(0,
>
> 0), X38 = c(0, 0), X39 = c(0, 0), X40 = c(0, 0), X41 = c(0,
>
> 0), X42 = c(0, 0), X43 = c(0, 0), X44 = c(0, 0), X45 = c(0,
>
> 0), X46 = c(0, 0), X47 = c(0, 0), X48 = c(0, 0), X49 = c(0,
>
> 0), X50 = c(0, 0), X51 = c(0, 0), X52 = c(0, 0), X53 = c(0,
>
> 0), X54 = c(0, 0), X55 = c(0, 0), X56 = c(0, 0), X57 = c(0,
>
> 0), X58 = c(0, 0), X59 = c(0, 0), X60 = c(0, 0), X61 = c(0,
>
> 0), X62 = c(0, 0), X63 = c(0, 0), X64 = c(0, 0), X65 = c(0,
>
> 0), X66 = c(0, 0), X67 = c(0, 0), X68 = c(0, 0), X69 = c(0,
>
> 0), X70 = c(0, 0), X71 = c(0, 0), X72 = c(0, 0), X73 = c(0,
>
> 0), X74 = c(0, 0), X75 = c(0, 0), X76 = c(0, 0), X77 = c(0,
>
> 0), X78 = c(0, 0), X79 = c(0, 0), X80 = c(0, 0), X81 = c(0,
>
> 0)), .Names = c("X1", "X2", "X3", "X4", "X5", "X6", "X7",
>
> "X8", "X9", "X10", "X11", "X12", "X13", "X14", "X15", "X16",
>
> "X17", "X18", "X19", "X20", "X21", "X22", "X23", "X24", "X25",
>
> "X26", "X27", "X28", "X29", "X30", "X31", "X32", "X33", "X34",
>
> "X35", "X36", "X37", "X38", "X39", "X40", "X41", "X42", "X43",
>
> "X44", "X45", "X46", "X47", "X48", "X49", "X50", "X51", "X52",
>
> "X53", "X54", "X55", "X56", "X57", "X58", "X59", "X60", "X61",
>
> "X62", "X63", "X64", "X65", "X66", "X67", "X68", "X69", "X70",
>
> "X71", "X72", "X73", "X74", "X75", "X76", "X77", "X78", "X79",
>
> "X80", "X81"), row.names = 1:2, class = "data.frame")
>
>
>
> beq2
>
> c(1, 1)
>
>
> Regards,
>
> Aveek
>
> Sent from Yahoo Mail on Android
> < https://go.onelink.me/107872968?pid=InProduct&c=Global_Internal_YGrowth_AndroidEmailSig__AndroidUsers&af_wl=ym&af_sub1=Internal&af_sub2=Global_YGrowth&af_sub3=EmailSignature>
>
> On Tue, Dec 11, 2018 at 6:25 PM, Eric Berger
> < [hidden email]> wrote:
> Hi Aveek,
> 1. This is an "all-text" mailing list. Your attachment did not come
> through.
> You can check out the posting guide (see the link at the bottom of
> your email)
> and/or
> use dput(...) on your structures and paste them into your email so
> that members of the list can try to reproduce the problem.
> 2. One way to check out whether you are using a package correctly is to
> try a tiny example that you can calculate by hand, and see if you can
> reproduce the solution via the package.
> e.g. instead of a 9x9 matrix (hence 81 dimensional problem in your
> case), try a 2x2 matrix with maybe just one or two constraints.
>
> HTH,
> Eric
>
> On Tue, Dec 11, 2018 at 1:18 PM aveek via R-help < [hidden email]>
> wrote:
>
> Hi All,
> I am facing an issue with an optimization problem which I am trying to
> solve using NlcOptim package in R. I have tried reaching out to the package
> maintainer but not received any response, hence posting this here.
>
>
> Below is the code snippet I am using:
>
>
>
> #Optimization
>
> obj_F <- function(vect_mat){
>
> return (sum((c(InputTM) - vect_mat)^2))
>
> }
>
>
>
> numel = nrow(InputTM)*ncol(InputTM)
>
> opt_vect = solnl(X=c(InputTM), objfun=obj_F, A=-constr_new, B=-x_than0,
> Aeq=as.matrix(aeq2), Beq=beq2, lb=c(rep(0,numel)),ub=c(rep(1,numel)),tolX =
> 0)
>
>
>
> I am attaching in the email the data being used as function arguments.
>
>
>
> Input_TM is a 9*9 matrix
>
> Constr_new is a 120*81 matrix
>
> x_than0 is a 120*1 matrix
>
> aeq2 is a 17*81 matrix
>
> beq2 is a 17*1 matrix
>
>
>
> Below is the error I am getting :
>
>
>
> R> opt_vect = solnl(X=c(InputTM), objfun=obj_F, A=-constr_new,
> B=-x_than0, Aeq=as.matrix(aeq2), Beq=beq2,
> lb=c(rep(0,numel)),ub=c(rep(1,numel)),tolX = 0)
>
> Error in as.matrix(A %*% Xtarget) - matrix(B, ncol = 1) :
>
> non-conformable arrays
>
> Calls: solnl -> rbind -> rbind
>
> In addition: Warning message:
>
> In rbind(rbind(lbright, ubright), B) :
>
> number of columns of result is not a multiple of vector length (arg 2)
>
> Calls: solnl -> rbind
>
>
>
> Enter a frame number, or 0 to exit
>
>
>
> 1: solnl(X = c(InputTM), objfun = obj_F, A = -constr_new, B = -x_than0,
> Aeq = as.matrix(aeq2), Beq = beq2, lb =
>
> 2: rbind(rbind(rbind(Aeq %*% Xtarget - Beq, as.matrix(nceq)), as.matrix(A
> %*% Xtarget) - matrix(B, ncol = 1)), a
>
> 3: rbind(rbind(Aeq %*% Xtarget - Beq, as.matrix(nceq)), as.matrix(A %*%
> Xtarget) - matrix(B, ncol = 1))
>
>
>
>
>
>
>
> Can you kindly help with this? I am mostly sure that the constraint
> matrices have been correctly formulated. Am I going wrong with the way I am
> specifying the arguments?
>
> Thanks a lot for any help any of you can offer.
>
>
>
> Thanks and Regards,
>
> Aveek Mukhopadhyay
>
>
>
>
>
> <!--#yiv2239857533 _filtered #yiv2239857533 {font-family:"Cambria
> Math";panose-1:2 4 5 3 5 4 6 3 2 4;} _filtered #yiv2239857533
> {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;} _filtered
> #yiv2239857533 {font-family:"Lucida Console";panose-1:2 11 6 9 4 5 4 2 2
> 4;}#yiv2239857533 #yiv2239857533 p.yiv2239857533MsoNormal, #yiv2239857533
> li.yiv2239857533MsoNormal, #yiv2239857533 div.yiv2239857533MsoNormal
> {margin:0in;margin-bottom:.0001pt;font-size:11.0pt;font-family:"Calibri",
> sans-serif;}#yiv2239857533 a:link, #yiv2239857533
> span.yiv2239857533MsoHyperlink
> {color:#0563C1;text-decoration:underline;}#yiv2239857533 a:visited,
> #yiv2239857533 span.yiv2239857533MsoHyperlinkFollowed
> {color:#954F72;text-decoration:underline;}#yiv2239857533 pre
> {margin:0in;margin-bottom:.0001pt;font-size:10.0pt;font-family:"Courier
> New";}#yiv2239857533 span.yiv2239857533HTMLPreformattedChar
> {font-family:"Courier New";}#yiv2239857533 span.yiv2239857533EmailStyle19
> {font-family:"Calibri", sans-serif;color:windowtext;}#yiv2239857533
> span.yiv2239857533gnkrckgcmsb {}#yiv2239857533
> span.yiv2239857533gnkrckgcmrb {}#yiv2239857533
> span.yiv2239857533gnkrckgcasb {}#yiv2239857533
> span.yiv2239857533gnkrckgcgsb {}#yiv2239857533
> span.yiv2239857533EmailStyle24 {font-family:"Calibri",
> sans-serif;color:#1F497D;}#yiv2239857533 span.yiv2239857533EmailStyle25
> {font-family:"Calibri", sans-serif;color:windowtext;}#yiv2239857533
> .yiv2239857533MsoChpDefault {font-size:10.0pt;} _filtered #yiv2239857533
> {margin:1.0in 1.0in 1.0in 1.0in;}#yiv2239857533
> div.yiv2239857533WordSection1 {}-->
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
>
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
Hi Eric,
Yes, in fact I did mention in my 1st email that I am posting this here as the package maintainer did not respond even after a couple of emails.
Thanks,Aveek
Sent from Yahoo Mail on Android
On Mon, Jan 14, 2019 at 2:11 PM, Eric Berger< [hidden email]> wrote: Aveek,Did you try contacting the package maintainer as Hans suggested?
On Mon, Jan 14, 2019 at 10:31 AM aveek < [hidden email]> wrote:
Hello,
Can anyone plz help with the below problem?
Thanks,Aveek
Sent from Yahoo Mail on Android
On Wed, Dec 12, 2018 at 12:14 PM, aveek< [hidden email]> wrote:
Hello Eric,
Thanks for your response and suggestions.
I have used dput() on the R objects - sharing below so that it is possible for anyone to recreate the situation.
I have still kept it as a 9*9 matrix but for simplicity we now only have 2 equality and 2 non equality constraints.
Thanks again for your help.
InputTM
structure(c(0.813231189406663, 0.0199464964676128, 0.00100552815128915,
0.000465771436428336, 0.000736922016196076, 0.00203037431732662,
0.000596998285890709, 0.0011699714577823, 0, 0.103116692172408,
0.751775368068589, 0.0160957427707042, 0.00285542569941823, 0.0020295541916448,
0.00954562743564027, 0.00173399818906894, 0.00292299139663608,
0, 0.0481959576543631, 0.177032393868544, 0.811609524051149,
0.146703962329218, 0.0698423269415636, 0.168241524872922, 0.0505757280338206,
0.0324917017565673, 0, 0.026504623193874, 0.038430496838613,
0.134709744786799, 0.758322716164413, 0.176013939161559, 0.234265359508999,
0.108188555487004, 0.0476663548325017, 0, 0.00520614395937929,
0.00868690292550468, 0.0223895752360805, 0.0581458712447338,
0.681496895121054, 0.0733970775224908, 0.0508491985259732, 0.0268876385360338,
0, 0.000749001395622802, 0.00181690494827145, 0.00317194515883476,
0.00705434604769267, 0.0211989316284324, 0.464732208379131, 0.0165146818291576,
0.00721872506710652, 0, 0.000960493069403903, 0.00138444384054219,
0.00703528202498607, 0.0163983255053438, 0.0301780379843763,
0.0280699612529658, 0.491157627745315, 0.0235353949469527, 0,
0.00112628575287418, 0.000477300396419488, 0.00223238360574478,
0.00521558462566306, 0.00925149338117537, 0.00878272484051914,
0.163654071683595, 0.611806567272906, 0, 0.000909613395411595,
0.000449692645903539, 0.00175027421441265, 0.00483799694708872,
0.00925189957399783, 0.0109351418700064, 0.116729140220175, 0.246300654733514,
1), .Dim = c(9L, 9L))
Constr_new
structure(c(1, 0, -1, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(2L,
81L), .Dimnames = list(NULL, c("X11", "X12", "X13", "X14", "X15",
"X16", "X17", "X18", "X19", "X21", "X22", "X23", "X24", "X25",
"X26", "X27", "X28", "X29", "X31", "X32", "X33", "X34", "X35",
"X36", "X37", "X38", "X39", "X41", "X42", "X43", "X44", "X45",
"X46", "X47", "X48", "X49", "X51", "X52", "X53", "X54", "X55",
"X56", "X57", "X58", "X59", "X61", "X62", "X63", "X64", "X65",
"X66", "X67", "X68", "X69", "X71", "X72", "X73", "X74", "X75",
"X76", "X77", "X78", "X79", "X81", "X82", "X83", "X84", "X85",
"X86", "X87", "X88", "X89", "X91", "X92", "X93", "X94", "X95",
"X96", "X97", "X98", "X99")))
x_than0
c(1e-04, 1e-04)
aeq2
structure(list(X1 = c(1, 0), X2 = c(1, 0), X3 = c(1, 0), X4 = c(1,
0), X5 = c(1, 0), X6 = c(1, 0), X7 = c(1, 0), X8 = c(1, 0), X9 = c(1,
0), X10 = c(0, 1), X11 = c(0, 1), X12 = c(0, 1), X13 = c(0, 1
), X14 = c(0, 1), X15 = c(0, 1), X16 = c(0, 1), X17 = c(0, 1),
X18 = c(0, 1), X19 = c(0, 0), X20 = c(0, 0), X21 = c(0, 0
), X22 = c(0, 0), X23 = c(0, 0), X24 = c(0, 0), X25 = c(0,
0), X26 = c(0, 0), X27 = c(0, 0), X28 = c(0, 0), X29 = c(0,
0), X30 = c(0, 0), X31 = c(0, 0), X32 = c(0, 0), X33 = c(0,
0), X34 = c(0, 0), X35 = c(0, 0), X36 = c(0, 0), X37 = c(0,
0), X38 = c(0, 0), X39 = c(0, 0), X40 = c(0, 0), X41 = c(0,
0), X42 = c(0, 0), X43 = c(0, 0), X44 = c(0, 0), X45 = c(0,
0), X46 = c(0, 0), X47 = c(0, 0), X48 = c(0, 0), X49 = c(0,
0), X50 = c(0, 0), X51 = c(0, 0), X52 = c(0, 0), X53 = c(0,
0), X54 = c(0, 0), X55 = c(0, 0), X56 = c(0, 0), X57 = c(0,
0), X58 = c(0, 0), X59 = c(0, 0), X60 = c(0, 0), X61 = c(0,
0), X62 = c(0, 0), X63 = c(0, 0), X64 = c(0, 0), X65 = c(0,
0), X66 = c(0, 0), X67 = c(0, 0), X68 = c(0, 0), X69 = c(0,
0), X70 = c(0, 0), X71 = c(0, 0), X72 = c(0, 0), X73 = c(0,
0), X74 = c(0, 0), X75 = c(0, 0), X76 = c(0, 0), X77 = c(0,
0), X78 = c(0, 0), X79 = c(0, 0), X80 = c(0, 0), X81 = c(0,
0)), .Names = c("X1", "X2", "X3", "X4", "X5", "X6", "X7",
"X8", "X9", "X10", "X11", "X12", "X13", "X14", "X15", "X16",
"X17", "X18", "X19", "X20", "X21", "X22", "X23", "X24", "X25",
"X26", "X27", "X28", "X29", "X30", "X31", "X32", "X33", "X34",
"X35", "X36", "X37", "X38", "X39", "X40", "X41", "X42", "X43",
"X44", "X45", "X46", "X47", "X48", "X49", "X50", "X51", "X52",
"X53", "X54", "X55", "X56", "X57", "X58", "X59", "X60", "X61",
"X62", "X63", "X64", "X65", "X66", "X67", "X68", "X69", "X70",
"X71", "X72", "X73", "X74", "X75", "X76", "X77", "X78", "X79",
"X80", "X81"), row.names = 1:2, class = "data.frame")
beq2
c(1, 1)
Regards,
Aveek
Sent from Yahoo Mail on Android
On Tue, Dec 11, 2018 at 6:25 PM, Eric Berger< [hidden email]> wrote: Hi Aveek,1. This is an "all-text" mailing list. Your attachment did not come through. You can check out the posting guide (see the link at the bottom of your email) and/or use dput(...) on your structures and paste them into your email so that members of the list can try to reproduce the problem.2. One way to check out whether you are using a package correctly is to try a tiny example that you can calculate by hand, and see if you can reproduce the solution via the package. e.g. instead of a 9x9 matrix (hence 81 dimensional problem in your case), try a 2x2 matrix with maybe just one or two constraints.
HTH,Eric
On Tue, Dec 11, 2018 at 1:18 PM aveek via R-help < [hidden email]> wrote:
Hi All,
I am facing an issue with an optimization problem which I am trying to solve using NlcOptim package in R. I have tried reaching out to the package maintainer but not received any response, hence posting this here.
Below is the code snippet I am using:
#Optimization
obj_F <- function(vect_mat){
return (sum((c(InputTM) - vect_mat)^2))
}
numel = nrow(InputTM)*ncol(InputTM)
opt_vect = solnl(X=c(InputTM), objfun=obj_F, A=-constr_new, B=-x_than0, Aeq=as.matrix(aeq2), Beq=beq2, lb=c(rep(0,numel)),ub=c(rep(1,numel)),tolX = 0)
I am attaching in the email the data being used as function arguments.
Input_TM is a 9*9 matrix
Constr_new is a 120*81 matrix
x_than0 is a 120*1 matrix
aeq2 is a 17*81 matrix
beq2 is a 17*1 matrix
Below is the error I am getting :
R> opt_vect = solnl(X=c(InputTM), objfun=obj_F, A=-constr_new, B=-x_than0, Aeq=as.matrix(aeq2), Beq=beq2, lb=c(rep(0,numel)),ub=c(rep(1,numel)),tolX = 0)
Error in as.matrix(A %*% Xtarget) - matrix(B, ncol = 1) :
non-conformable arrays
Calls: solnl -> rbind -> rbind
In addition: Warning message:
In rbind(rbind(lbright, ubright), B) :
number of columns of result is not a multiple of vector length (arg 2)
Calls: solnl -> rbind
Enter a frame number, or 0 to exit
1: solnl(X = c(InputTM), objfun = obj_F, A = -constr_new, B = -x_than0, Aeq = as.matrix(aeq2), Beq = beq2, lb =
2: rbind(rbind(rbind(Aeq %*% Xtarget - Beq, as.matrix(nceq)), as.matrix(A %*% Xtarget) - matrix(B, ncol = 1)), a
3: rbind(rbind(Aeq %*% Xtarget - Beq, as.matrix(nceq)), as.matrix(A %*% Xtarget) - matrix(B, ncol = 1))
Can you kindly help with this? I am mostly sure that the constraint matrices have been correctly formulated. Am I going wrong with the way I am specifying the arguments?
Thanks a lot for any help any of you can offer.
Thanks and Regards,
Aveek Mukhopadhyay
<!--#yiv2239857533 _filtered #yiv2239857533 {font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;} _filtered #yiv2239857533 {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;} _filtered #yiv2239857533 {font-family:"Lucida Console";panose-1:2 11 6 9 4 5 4 2 2 4;}#yiv2239857533 #yiv2239857533 p.yiv2239857533MsoNormal, #yiv2239857533 li.yiv2239857533MsoNormal, #yiv2239857533 div.yiv2239857533MsoNormal {margin:0in;margin-bottom:.0001pt;font-size:11.0pt;font-family:"Calibri", sans-serif;}#yiv2239857533 a:link, #yiv2239857533 span.yiv2239857533MsoHyperlink {color:#0563C1;text-decoration:underline;}#yiv2239857533 a:visited, #yiv2239857533 span.yiv2239857533MsoHyperlinkFollowed {color:#954F72;text-decoration:underline;}#yiv2239857533 pre {margin:0in;margin-bottom:.0001pt;font-size:10.0pt;font-family:"Courier New";}#yiv2239857533 span.yiv2239857533HTMLPreformattedChar {font-family:"Courier New";}#yiv2239857533 span.yiv2239857533EmailStyle19 {font-family:"Calibri", sans-serif;color:windowtext;}#yiv2239857533 span.yiv2239857533gnkrckgcmsb {}#yiv2239857533 span.yiv2239857533gnkrckgcmrb {}#yiv2239857533 span.yiv2239857533gnkrckgcasb {}#yiv2239857533 span.yiv2239857533gnkrckgcgsb {}#yiv2239857533 span.yiv2239857533EmailStyle24 {font-family:"Calibri", sans-serif;color:#1F497D;}#yiv2239857533 span.yiv2239857533EmailStyle25 {font-family:"Calibri", sans-serif;color:windowtext;}#yiv2239857533 .yiv2239857533MsoChpDefault {font-size:10.0pt;} _filtered #yiv2239857533 {margin:1.0in 1.0in 1.0in 1.0in;}#yiv2239857533 div.yiv2239857533WordSection1 {}-->
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
To be corrected:
`Constr_new` with a capital letter;
`aeq2` is a list, should be a matrix.
As I said last month, you can yourself combine inequality constraints
with bounds constraints as follows:
myA <- rbind(-Constr_new, diag(-1,numel), diag(1,numel))
myB <- c(-x_than0, rep(0,numel), rep(1,numel))
and `solnl` will return a result like this:
sol <- NlcOptim::solnl(X = c(InputTM), objfun = obj_F, A = myA, B = myB,
Aeq = as.matrix(aeq2), Beq = beq2)
c(sol$par)
[1] 0.8310997170, 0.0378150241, ..., 0.2463006547, 1.0000000000
sol$fn
[1] 0.00421616
I will write to the maintainer asking about why this example does not
work -- supplying functioning code, maybe that will trigger a
response.
Hans Werner
Please note: You are sending e-mail in HTML format which makes it
almost impossible to use as code in the R console.
On Wed, Dec 12, 2018 at 12:45 PM Hans W Borchers < [hidden email]> wrote:
>
> This is still not complete: `x_than0` is missing.
> `Constr_new` is written with a capital 'C'.
> And aeq2 is a list of column vectors, not a matrix.
> Setting the tolerance to 0 does not seem to be a good idea.
>
> Making aeq2 a matrix and adding `x_than0 <- matrix(c(1, 1))`, then
>
> aeq2 <- as.matrix(aeq2)
> x_than0 <- matrix(c(1, 1))
>
> NlcOptim::solnl(X=c(InputTM), objfun=obj_F, A=-Constr_new, B=-x_than0,
> Aeq=as.matrix(aeq2), Beq=beq2,
> lb=c(rep(0,numel)),ub=c(rep(1,numel)), tolX = 0)
>
> will indeed return in the same error, while it runs without error if you
> either leave out the inequality constraints or the bounds constraints. So
> I guess there may be a bug when the function internally combines these
> constraints and the bounds.
>
> You could / should write to the maintainer. I know he is very responsive.
>
> For the moment, you can combine the bounds constraints and the lower and
> upper bounds yourself:
>
> myA <- rbind(-Constr_new, diag(-1,numel), diag(1,numel))
> myB <- c(-x_than0, rep(0,numel), rep(1,numel))
>
> NlcOptim::solnl(X=c(InputTM), objfun=obj_F, A=myA, B=myB,
> Aeq=as.matrix(aeq2), Beq=beq2)
>
> returns "constraints are inconsistent, no solution!", but that may be the
> case because I don't know your `x_than` value.
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|