I have one function that wants to optimize (Maximize) in R, any one could help me.
My equation is as follow:
Y=-10.6134 -3.477*X1 +4.743*X2 +56.10*X3 -0.07671*X4 +0.1005*X1^2-0.0529*X2^2 -25.741*X3^2 +0.000279*X4^2 -0.0984*X1*X2 -1.351*X1*X3 -0.00407*X1*X4
With these limitations:
3 <= X1 <= 11
25 <= X2 <= 45
0.1 <= X3 <= 10
10 <= X4 <= 200
I already optimize that with “solver” function in “Excel” and also I try some packages such as “optim” and “nlminb” but they don’t work for me.
Please help me; I really need it for my university project.
Tnx
Define your function
f<-function(X1,X2,X3,X4){
-10.6134 -3.477*X1 +4.743*X2 +56.10*X3 -0.07671*X4 +
0.1005*X1^2-0.0529*X2^2 -25.741*X3^2 +0.000279*X4^2 -
0.0984*X1*X2 -1.351*X1*X3 -0.00407*X1*X4
}
Write your function as a one-argument function as this is needed for optim, and put a minus as your optimizing
f2 <- function(x) -f(x[1],x[2], x[3],x[4])
Use Optim
optim(par=c(6,35,5,100),fn=f2,lower = c(3,25,0.1,10),upper = c(11,45,10,200),method = "L-BFGS-B")
Related
I need to minimize (preferably symbolically) functional
F = Integral(f(x)*cos(x)dx)
with constraints:
1)f(x) >= 0
2) Integral(f(x)dx) = 1
3) f(0) = 1
4) f'(x) <= 0
Is there any way to solve problem like this? Could this problem be solved in principle?
Thanks in advance
The objective function is f(x,y)=sqrt(x^2+2*y^2-xy), subject to 10 > x > 0, 10 > y > 0, x > y. I am going to find the x and y which maximize objective function. I am required to use Nonlinear models in MathProgBase.jl packages. The tutorial from https://mathprogbasejl.readthedocs.io/en/latest/nlp.html is difficult for me to follow since I am a beginner. I really appreciate your help!
It seems that JuMP doesn't support strictly greater/lower constraints (as result that most solver engines doesn't either).
A modellisation in Julia of your problem would be:
using JuMP, Ipopt
m = Model(with_optimizer(Ipopt.Optimizer, print_level=0))
#variable(m, 0 <= x <= 10, start=1)
#variable(m, 0 <= y <= 10, start=1)
#constraint(m, c1, y <= x )
#NLobjective(m, Max, sqrt(x^2+2*y^2-x*y))
optimize!(m)
status = termination_status(m)
if (status == MOI.OPTIMAL || status == MOI.LOCALLY_SOLVED || status == MOI.TIME_LIMIT) && has_values(m)
if (status == MOI.OPTIMAL)
println("** Problem solved correctly **")
else
println("** Problem returned a (possibly suboptimal) solution **")
end
println("- Objective value : ", objective_value(m))
println("- Optimal solutions:")
println("x: $(value.(x))")
println("y: $(value.(y))")
else
println("The model was not solved correctly.")
println(status)
end
(see https://lobianco.org/antonello/personal/blog/2017/0203_jump_for_gams_users for an explanation of the various steps)
That results of the script is:
** Problem returned a (possibly suboptimal) solution **
- Objective value : 14.14213575988668
- Optimal solutions:
x: 10.0
y: 10.0
I know the formula is: n(h) = n(h-1) + n(h-2) + 1
And I know it can be reduced as:
n(h) = n(h-1) + n(h-2) + 1
>= n(h-2) + n(h-2) + 1
>= 2n(h-2) + 1
>= 2n(h-2)
After this step I don't understand the recurrence that would come here. I was reading a proof online and they did this:
>= 2n(h-2)
>= 2(2n(h-4))
>= 2(2(2n(h-6)))
I'm not understanding that block. Why is each step multiplied by 2 and why is 2 more subtracted each time from the height? I'm having trouble visualizing it or something. Then the rest of the proof shows:
>=(2^i)n(h-2i)
I understand how they got that answer based on the pattern, and I can solve the rest of the proof, but I don't understand how that recursive pattern was chosen. I hope I'm making sense. If anyone can clarify this for me, I would appreciate it very much!
Given that n(h) >= 2n(h-2) for all h, we can apply this very same inequality for h-2 as well. This would give us:
n(h-2) >= 2n(h-2-2)
which is the same as
n(h-2) >= 2n(h-4).
If we now apply it again for h-4 (as we did for h-2) we get
n(h-4) >= 2n(h-4-2) = 2n(h-6).
Now we can replace these two last inequalities into the first one:
n(h) >= 2n(h-2) >= 2(2n(h-4)) >= 2(2(2n(h-6)))
and so on...
This may be quite a basic question for someone who knows linear programming.
In most of the problems that I saw on LP has somewhat similar to following format
max 3x+4y
subject to 4x-5y = -34
3x-5y = 10 (and similar other constraints)
So in other words, we have same number of unknown in objective and constraint functions.
My problem is that I have one unknown variable in objective function and 3 unknowns in constraint functions.
The problem is like this
Objective function: min w1
subject to:
w1 + 0.1676x + 0.1692y >= 0.1666
w1 - 0.1676x - 0.1692y >= -0.1666
w1 + 0.3039x + 0.3058y >= 0.3
w1 - 0.3039x - 0.3058y >= -0.3
x + y = 1
x >= 0
y >= 0
As can be seen, the objective function has only one unknown i.e. w1 and constraint functions have 3 (or lets say 2) unknown i.e w1, x and y.
Can somebody please guide me how to solve this problem, especially using R or MATLAB linear programming toolbox.
Your objective only involves w1 but you can still view it as a function of w1,x,y, where the coefficient of w1 is 1, and the coeffs of x,y are zero:
min w1*1 + x*0 + y*0
Once you see this you can formulate it in the usual way as a "standard" LP.
Prasad is correct. The number of unknowns in the objective function does not matter. You can view unknowns that are not present as having a zero coefficient.
This LP is easily solved using Matlab's linprog function. For more
details on linprog see the documentation here.
% We lay out the variables as X = [w1; x; y]
c = [1; 0; 0]; % The objective is w1 = c'*X
% Construct the constraint matrix
% Inequality constraints will be written as Ain*X <= bin
% w1 x y
Ain = [ -1 -0.1676 -0.1692;
-1 0.1676 0.1692;
-1 -0.3039 -0.3058;
-1 0.3039 0.3058;
];
bin = [ -0.166; 0.166; -0.3; 0.3];
% Construct equality constraints Aeq*X == beq
Aeq = [ 0 1 1];
beq = 1;
%Construct lower and upper bounds l <= X <= u
l = [ -inf; 0; 0];
u = inf(3,1);
% Solve the LP using linprog
[X, optval] = linprog(c,Ain,bin,Aeq,beq,l,u);
% Extract the solution
w1 = X(1);
x = X(2);
y = X(3);
I am looking for a solution:
A= {0,1,2,3,4};
F(x) = 3x - 1 (mod5)
Could you help me to find the inverse. I am struggling with this as it seems to be not to be onto or 1to1.
Thank you for your help.
x = 2y + 2, where y = F(x)
-> 3x - 1 = 3(2y+2) - 1 = 6y + 5 = y (mod 5)
edit: if you want this to be evaluated for the list of principal values mod 5 [0,1,2,3,4], just evaluate 2y+2 for each of these, and what you get is [2,4,1,3,0]. Which, if you plug back into 3x-1, you get [0,1,2,3,4] as expected.