I have to generate all the pareto points, but I got this error.
using JuMP
using Gurobi
using Gadfly
using Ipopt
m = Model(solver=IpoptSolver(print_level=0))
#variable(m, 0.1 <= x <= 1.0)
#variable(m, 0.0 <= y <= 1.0)
pareto_x = Float16[]
pareto_y = Float16[]
for i in 0.0:0.1:1.0
for j in 0.0:0.1:1.0
f1(x,y) = x
f2(x,y) = (2.0-exp(-((y-0.2)/0.004)^2)-0.8*exp(-((y-0.6)/0.4)^2) )/x
#NLobjective(m, Min, i*f1(x,y) + j*f2(x,y) ) ## <<-- ERROR HERE
status = solve(m)
println("Objective value: ", getobjectivevalue(m))
x_opt = getvalue(x)
y_opt = getvalue(y)
println("x = ", x_opt)
println("y = ", y_opt)
push!(pareto_x,f1(x_opt,y_opt))
push!(pareto_y,f2(x_opt,y_opt))
end
end
plot(x=pareto_x, y=pareto_y)
http://jump.readthedocs.io/en/latest/nlp.html#user-defined-functions
mysquare(x) = x^2
myf(x,y) = (x-1)^2+(y-2)^2
JuMP.register(:myf, 2, myf, autodiff=true)
JuMP.register(:mysquare, 1, mysquare, autodiff=true)
m = Model()
#variable(m, x[1:2] >= 0.5)
#NLobjective(m, Min, myf(x[1],mysquare(x[2])))
SOLVED with this
f1(x,y) = x
f2(x,y) = (2.0-exp(-((y-0.2)/0.004)^2)-0.8*exp(-((y-0.6)/0.4)^2) )/x
isdefined(:f1) || JuMP.register(:f1, 2, f1, autodiff=true)
isdefined(:f2) || JuMP.register(:f2, 2, f2, autodiff=true)
#objective(m, Min, f1(x,y) )
Related
I am trying to minimize the function:
f(x) = -x[1]*x[2]*x[3]
subject to the constraints:
0 <= x[1] + 2*x[2] + 2*x[3] <= 72.
What I did so far is that I wrote the constraint as two separate constraints:
constraint_1: -x[1] - 2*x[2] - 2*x[3] <=0
constraint_2: x[1] + 2*x[2] + 2*x[3] <= 72
Then I used the following codes, however I cannot figure out what I should write for the objective.in:
library(lpSolve)
lp(direction = "min", objective.in, const.mat, const.dir, const.rhs)
const.mat = matrix(c(-1,-2,-2,1,2,2), nrow = 2, ncol = 3, byrow=TRUE)
constraint_1 = 0
constraint_2 = 72
const.rhs = c(constraint_1, constraint_2)
const.dir = c("<=", "<=")
EDIT:
I used nloptr package, but I am having this error: "Error in .checkfunargs(eval_f, arglist, "eval_f") :
eval_f requires argument 'x_2' but this has not been passed to the 'nloptr' function."
When I apply these codes:
objective function
eval_f0 <-function( x_1, x_2, x_3 ){
return(-x_1*x_2*x_3)
}
eval_grad_f0 <-function( x_1, x_2, x_3 ){
return(c(-x_2*x_3, -x_1*x_3, -x_1*x_2))
}
constraint function
eval_g0 <- function(x_1, x_2, x_3) {
return((-x_1 - 2*x_2 - 2*x_3),
(x_1 + 2*x_2 + 2*x_3 - 72))
}
eval_jac_g0 <- function(x_1, x_2, x_3) {
return(rbind(c(-1,-2,-2),
c(1,2,2)))
}
res0 <-nloptr(x0=c(0,0,0),
eval_f=eval_f0,
eval_grad_f=eval_grad_f0,
lb =c(-Inf,-Inf,-Inf),
ub =c(Inf,Inf,Inf),
eval_g_ineq =eval_g0,
eval_jac_g_ineq =eval_jac_g0,
opts =list("algorithm"="NLOPT_LD_MMA",
"xtol_rel"=1.0e-8,
"print_level"=3,
"check_derivatives"=TRUE,
"check_derivatives_print"="all"))
Have a problem with data generating and I have no idea how to solve this. All information provided in photo: Problem.
I think that X_i(t) in both cases should be 200 x 100 if we say that t is from 0 to 1 (length = 100). Furthermore, coefficients for polynomial should contain 200 x 4 and coefficients for fourier should contain 200 x 5. Bu I have no idea how to start to solve this problem.
Here is some code. So, I have already defined my beta's, but I can't defeat generating of X_i(t).
t <- seq(0, 1, length = 100)
beta_1t <- rep(0, 100)
plot(t, beta_1t, type = "l")
beta_2t <- (t >= 0 & t < 0.342) * ((t - 0.5)^2 - 0.025) +
(t >= 0.342 & t <= 0.658) * 0 +
(t > 0.658 & t <= 1) * (-(t - 0.5)^2 + 0.025)
plot(t, beta_2t, type = "l")
beta_3t <- t^3 - 1.6 * t^2 + 0.76 * t + 1
plot(t, beta_3t, type = "l")
poly_c <- matrix(rnorm(n = 800, mean = 0, sd = 1), ncol = 4)
four_c <- matrix(rnorm(n = 1000, mean = 0, sd = 1), ncol = 5)
As I mentioned before, there should be (X_i(t), Y_i(t)) samples. Here i = 1, 2, ..., 200; t from [0, 1] (length = 100).
I am currently doesn't some testing and analysis of the Micahelis-Menten enzyme kinetics model. And what my code is attempting to do is to change the rate parameter th1 to 50% up to 200% of it's max value.
What I want to do though is I want to produce 4 separate graphs which show what happens to MM1, MM2, MM3, MM4 when changing the rate parameter.
The issue that I have is regarding the "q" loop and where I use the "if", "else if" and "else" functions found near the end of my code.
MM <- list(Pre = matrix(c(1,0,0,1,0,0,0,1,1,0,0,0), ncol=4), Post =
matrix(c(0,1,0,0,1,1,1,0,0,0,0,1),ncol=4), M= c("x1"=301,"x2"=120, "x3"=0,
"x4"=0), h = function (x, t, th = c(1.66e-3, 1e-4 , 0.1))
{
with(as.list(c(x, th)), {
return(c(th[1] * x1 * x2, th[2] * x3, th[3] * x3))
})
})
gillespied1 <- function (N, T = 100, dt = 1, ...)
{
tt = 0
n = T%/%dt
x = N$M
S = t(N$Post - N$Pre)
u = nrow(S)
v = ncol(S)
xmat = matrix(ncol = u, nrow = n)
i = 1
target = 0
repeat {
h = N$h(x, tt, ...)
h0 = sum(h)
if (h0 < 1e-10)
tt = 1e+99
else if (h0>3000){
tt=1e+99
xmat[i] <- xmat[i-1] ###
i = i + 1
if(i > n)
return(ts(xmat, start = 0, deltat = dt)) ###
}
else tt = tt + rexp(1, h0)
while (tt >= target) {
xmat[i, ] = x
i = i + 1
target = target + dt
if (i > n)
return(ts(xmat, start = 0, deltat = dt))
}
j = sample(v, 1, prob = h)
x = x + S[, j]
}
}
cl = rainbow(13)
for(q in 1:4){
plot(1, type="n", xlab="Time", ylab="Concentration of Substrate",xaxt='n',
xlim=c(0, 1200), ylim=c(0, 310), main="Micahaelis-Menten:Changing Substrate rate parameter")
for(i in seq(from=50, to=200, by=25)){
MM$h = function (x, t, th = c(1.66e-3*(i/100), 1e-4, 0.1))
{
with(as.list(c(x, th)), {
return(c(th[1] * x1 * x2, th[2] * x3, th[3] * x3))
})
}
out = gillespied1(MM,T=300,dt=0.1)
MM1 <- out[,1]
MM2 = out[,2]
MM3 = out[,3]
MM4 = out[,4]
for (j in 1:40) {
out = gillespied1(MM, T=300, dt=0.1)
MM1 = cbind(MM1,out[,1])
MM2 = cbind(MM2,out[,2])
MM3 = cbind(MM3,out[,3])
MM4 = cbind(MM4,out[,4])
}
a =matrix(rowMeans(MM1))
b = matrix(rowMeans(MM2))
c = matrix(rowMeans(MM3))
d = matrix(rowMeans(MM4))
if (q = 1) {
lines(a, lwd="1.5", col =cl[2*((i/25)-1)-1])
} else if ( q=2) {
lines(b, lwd="1.5", col =cl[2*((i/25)-1)-1])
} else if ( q=3) {
lines(c, lwd="1.5", col =cl[2*((i/25)-1)-1])
} else
lines(d, lwd="1.5", col =cl[2*((i/25)-1)-1])
}
axis(side = 1, at = (0:300)*10 , labels = 0:300)
legend("topright", legend=c("50%","75%","100%","125%","150%","175%", "200%"), lty =c(rep(1)), lwd=c(rep(1)), title ="% of original substrate rate parameter", col=cl[seq(1,13,2)], cex=0.4)
}
I keep getting this error
Error: unexpected '}' in "}"
but I can't tell why.
If my code was working perfectly I should end up with 4 graphs, each graph containing 7 lines.
Any help would be amazing. Thanks.
From the link:
Relational Operators
Description
Binary operators which allow the comparison of values in atomic vectors.
Usage
x == y
I have an R function that provides the 95% confidence Interval for the ncp (non-centrality parameter) of a t distribution.
Via simulation in R, is it possible to show that in the long-run the CIs from this R function capture a given TRUE ncp (here "2" same as input t) 95% of the time?
(I appreciate any ideas as to how to do this)
CI.ncp <- function(t, N){
f <- function (ncp, alpha, q, df) {
abs(suppressWarnings(pt(q = t, df = N - 1, ncp, lower.tail = FALSE)) - alpha) }
sapply(c(0.025, 0.975),
function(x) optim(1, f, alpha = x, q = t, df = N - 1, control = list(reltol = (.Machine$double.eps)))[[1]]) }
#Example of Use:
CI.ncp(t = 2, N = 20) # gives: -0.08293755 4.03548862
#(in the long-run 95% of the time, "2" is contained within these
# two numbers, how to show this in R?)
Here is what I have tried with no success:
fun <- function(t = 2, N = 20){
ncp = rt(1, N - 1, t)
CI.ncp(t = 2, N = 20)
mean(ncp <= 2 & 2 <= ncp )
}
R <- 1000
sim <- t(replicate(R, fun()))
coverage <- mean(sim[,1] <= 2 & 2 <= sim[,2])
The problem is the that we need to feed the random ncp obtained from the fun in the CI.ncp:
fun <- function(t = 2, N = 20){ ;
ncp = rt(1, N - 1, t);
CI.ncp(t = ncp, N = 20);
}
R <- 1e4 ;
sim <- t(replicate(R, fun()));
coverage <- mean(sim[,1] <= 2 & 2 <= sim[,2])
I would use package MBESS.
#install.packages("MBESS")
library(MBESS)
fun <- function(t = 2, N = 20, alpha = 0.95){
x = rt(1, N - 1, t)
conf.limits.nct(x, df = N, conf.level = alpha)[c(1, 3)]
}
set.seed(5221)
R <- 1000
sim <- t(replicate(R, fun()))
head(sim)
coverage <- mean(sim[,1] <= 2 & 2 <= sim[,2])
coverage
[1] 0.941
Here's the code I made so far:
z = vector()
for(i in 1:20){
Alkie = function(T=20, lambda=2.5, k=2, mu=3) {
t = 0
N = 0
i = 1
A.t = rexp(1, lambda)
D.t = Inf
while(t[i] < T) {
t[i+1] = min(A.t, D.t)
N[i+1] = N[i] + ifelse(A.t < D.t, 1, -1)
if(A.t < D.t) {
A.t = A.t + rexp(1,lambda)
if(N[i+1] == 1) D.t = t[i+1] + rgamma(1, k, mu)
if(N[i+1] == 6) D.t = t[i+1] + rgamma(1, 0, mu)
}
else
D.t = ifelse(N[i+1] == 0, Inf, t[i+1] + rgamma(1, k, mu))
i = i + 1
}
cbind(t=t, N=N)
}
x = Alkie(T=20, lambda=2.5, k=2, mu=3)
n = nrow(x)
plot(c(x[1,1], rep(x[-1,1], each=2), x[n,1]), rep(x[,2], each=2), type="l",
xlab="t(mins)", ylab="N(t)", col="blue")
How do I store the counts?