Non-linear Optimization with summation objective function and data set

Hello,
I'm looking for someone to help me implement a non linear optimization problem with one constraint and a dataset.
The objective function is a mult-summation NLP
The constraint is:
The dataset x is a vector with two columns, where x( i) = column 1 and x(j) = column 2. Both x(i) and x(j) map to dataset y(i) and y(j), respectively. Variable p(i) and p(j) is unknown.
How do I formulate a NLP problem in MATLAB to solve for the different iterations of p(i) and p(j)?

3 Commenti

What is the dimension of 'x' and 'y'? Are they 4x2 matrices? If they have two columns, then how do you distinguish between two columns, for example, what is the column for x(1), x(2), x(3), ...? What are the dimensions of 'p'?
MP
MP il 20 Apr 2020
Modificato: MP il 20 Apr 2020
Hi Ameer,
Yes, the dimensions of the matrice are 4x2. When I write the code, I plan to reference x(i) as column x1 and iterate through the rows (same for x2, y1, and y2). For example x(i) would be x(i,1).
x(i) | x(j) | y(i) | y(j)
1 -1 -1 -1
2 2 1 1
-2 -2 1 1
You mentioned x(i) is same as x(i,1). Then which variable will corresponds to column 2. I suggest to re-write the equation show the difference between columns and rows of matrix x and y.

Accedi per commentare.

 Risposta accettata

See fmincon(): https://www.mathworks.com/help/optim/ug/fmincon.html for nonlinear programming in MATLAB.

5 Commenti

Yes, I tried using this function but am having trouble. Here are two functions I've created, the objective, constraint, and the main function containing the fmincon(). I'm having difficulty getting this to execute.
main:
clear
close all
clc
% From scratch
global N x y
nvars = 4; %p variable?
d = csvread('dataset.csv');
x = d(:,1:2)
y = d(:,3:4)
N = 4;
%initial value
x0 = x(1,:);
%need to maximize objective function
[x, fval] = fmincon(@(x) -objectiveFun(x),x0,[],[], [], [], [0 0 0], [], @constraintsFun)
objective:
function [f_sum] = objectiveFun(x0)
global N x y
%p variable
A = sym('p', [2 4]);
for i = 1:N
for j = 1:N
f_xy(i) = A(i,1)*A(j,2)*y(i,1)*y(j,2)*(x(i,1)*x(j,2)+1)^2;
end
end
f_sum = sum(f_xy);
end
constraint:
function [c, ceq] = constraintsFun(x0)
global N y
A = sym('a', [2 4]);
%inequality constraint
c = []; %none
%equality constraint
for i = 1:N
c_eq_con(i) = A(i)*y(i);
end
c_eq = sum(c_eq_con);
end
I don't have the file dataset.csv. Can you share the value of variable 'd'?
I used a random value of value for variable d. check the following code. Your constraint is linear, so you don't need to write a seperate function.
clear
close all
clc
% From scratch
global N x y
nvars = 4; %p variable?
% d = csvread('dataset.csv');
d = rand(4,4);
x = d(:,1:2);
y = d(:,3:4);
N = 4;
Aeq = [y(:,1)' 0 0 0 0];
Beq = 0;
%initial value
x0 = rand(1,8);
%need to maximize objective function
[x, fval] = fmincon(@(x) -objectiveFun(x),x0, [], [], Aeq, Beq, zeros(1,8), [])
function [f_sum] = objectiveFun(p)
global x y
% fmincon use a row vector for optimization. p is 1x8, so we need divide in two parts
p1 = p(1:4).';
p2 = p(5:8).';
f_sum = sum(p1.*p2.*y(:,1).*y(:,2).*(x(:,1).*x(:,2)+1).^2);
end
Because p is a 4x2 matrix. I used a linear vector which can be later reshaped to 4x2.

Accedi per commentare.

Più risposte (1)

Thank you Ameer, this is very helpful! If I had multiple constraint equations could I just leave A, B, Aeq, and Beq as blank matrices into the fmincon() function and use a constraint function as an input? Only asking out of curiousity.
Also, is it possible to "see" the fmincon() function iterate through the objective function until it reaches its optimimum? Whether in a table or graphical format? Not sure if that makes sense.

1 Commento

1. Yes, that will also work. I guess the only difference is that the function might take longer because in implementation, when A and B are specified, MATLAB knows that these are linear constraints so It can apply optimized methods. But using constraint function, it will consider then to be a nonlinear constraint that might require more effort to solve. Nevertheless, the results should be similar.
2. Yes, it is also possible. You can use the outputFcn property of fmincon to do anything at the end of an iteration. See here, for example: https://www.mathworks.com/help/optim/ug/output-functions.html and here for more details: https://www.mathworks.com/help/optim/ug/output-function.html. I wrote a simple example to illustrate the concept.
f = @(x) sum(x.^2.*exp(-x.^2)); % objective function
opts = optimoptions('fmincon', 'OutputFcn', @myOutFcn, 'Display', 'off');
[x_sol, f_sol] = fmincon(f, rand(10,1), [], [], [], [], [], [], [], opts);
function stop = myOutFcn(x, optimValues, state)
persistent i
if isempty(i)
i = 1;
end
fprintf('Iteration: %d, Objective function value: %.16f\n', i, optimValues.fval)
i = i+1;
stop = 0;
end

Accedi per commentare.

Richiesto:

MP
il 20 Apr 2020

Commentato:

il 11 Nov 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by