Constrained Optimization Problem in C++

  • Thread starter Explore_Imagination
  • Start date
E

Explore_Imagination

The task is to solve a constrained optimization problem in C/C++.
Computational
Time is of high priority. One approach can be to use ready functions
in a "free ware" Optimization Library (if available). If any one of
you have any idea about such library please inform me. I am dealing
with Constrained Optimization for the first time so please guide me
How I should solve this problem. I will appreciate suggestions from
you.


Find values of x that minimize f=-x1*x2*x3 and subject to the
constraints:

x1+2*x2+2*x3>0
x1+2*x2+2*x3<72
x2=10

In Matlab it can be solved by:

x0 = [10; 10; 10]; % Starting guess at the solution

A=[]; b=[]; Aeq=[]; beq=[]; lb=[]; ub=[];

[x,fval] = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@(x)constr(x))


%myfun

function f = myfun(x)

f = -x(1)* x(2)* x(3); %f=-x1*x2*x3


%constr

function [c,ceq]=constr(x)

c(1)=0-(x(1)+2*x(2)+2*x(3)); %x1+2*x2+2*x3>0

c(2)=x(1)+2*x(2)+2*x(3)-72; %x1+2*x2+2*x3<72

ceq(1)=x(2)-10; %x2=10

Hoping to hear from you guys !!!!
 
V

Victor Bazarov

Explore_Imagination said:
The task is to solve a constrained optimization problem in C/C++.

This is not a language problem. Find the algorithm, convert it to
C/C++ (whatever that is!), then if you encounter _language_ problems
in the C++ part, come back and post. Read the FAQ before doing so,
too.

V
 
J

jkherciueh

Explore_Imagination said:
The task is to solve a constrained optimization problem in C/C++.
Computational
Time is of high priority. One approach can be to use ready functions
in a "free ware" Optimization Library (if available). If any one of
you have any idea about such library please inform me. I am dealing
with Constrained Optimization for the first time so please guide me
How I should solve this problem. I will appreciate suggestions from
you.


Find values of x that minimize f=-x1*x2*x3 and subject to the
constraints:

x1+2*x2+2*x3>0
x1+2*x2+2*x3<72
x2=10

In Matlab it can be solved by:

x0 = [10; 10; 10]; % Starting guess at the solution

A=[]; b=[]; Aeq=[]; beq=[]; lb=[]; ub=[];

[x,fval] = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@(x)constr(x))


%myfun

function f = myfun(x)

f = -x(1)* x(2)* x(3); %f=-x1*x2*x3


%constr

function [c,ceq]=constr(x)

c(1)=0-(x(1)+2*x(2)+2*x(3)); %x1+2*x2+2*x3>0

c(2)=x(1)+2*x(2)+2*x(3)-72; %x1+2*x2+2*x3<72

ceq(1)=x(2)-10; %x2=10

Hoping to hear from you guys !!!!

In C++, the problem can be solved by a program like this:

#include <iostream>
int main ( void ) {
std::cout << "x1 = " << 26 << '\n'
<< "x2 = " << 10 << '\n'
<< "x3 = " << 13 << '\n';
}

Since you said that time is of high priority, I made the program as fast as
I could without compromising readability.


Best

Kai-Uwe Bux
 
K

Kira Yamato

Explore_Imagination said:
The task is to solve a constrained optimization problem in C/C++.
Computational
Time is of high priority. One approach can be to use ready functions
in a "free ware" Optimization Library (if available). If any one of
you have any idea about such library please inform me. I am dealing
with Constrained Optimization for the first time so please guide me
How I should solve this problem. I will appreciate suggestions from
you.


Find values of x that minimize f=-x1*x2*x3 and subject to the
constraints:

x1+2*x2+2*x3>0
x1+2*x2+2*x3<72
x2=10

In Matlab it can be solved by:

x0 = [10; 10; 10]; % Starting guess at the solution

A=[]; b=[]; Aeq=[]; beq=[]; lb=[]; ub=[];

[x,fval] = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@(x)constr(x))


%myfun

function f = myfun(x)

f = -x(1)* x(2)* x(3); %f=-x1*x2*x3


%constr

function [c,ceq]=constr(x)

c(1)=0-(x(1)+2*x(2)+2*x(3)); %x1+2*x2+2*x3>0

c(2)=x(1)+2*x(2)+2*x(3)-72; %x1+2*x2+2*x3<72

ceq(1)=x(2)-10; %x2=10

Hoping to hear from you guys !!!!

In C++, the problem can be solved by a program like this:

#include <iostream>
int main ( void ) {
std::cout << "x1 = " << 26 << '\n'
<< "x2 = " << 10 << '\n'
<< "x3 = " << 13 << '\n';
}

Since you said that time is of high priority, I made the program as fast as
I could without compromising readability.

Actually, this problem has no solution.

Because of the strict inequality in the constraints, the region of the
domain is an open set. However, the point (26,10,13) lies on the
boundary of that set but not in the set, i.e., the point does not
satisfy the constraint
x1+2*x2+2*x3<72.

What we can say is that the *infimum* of f over the constraint region
occurs at (26,10,13).

So, any proper program attempting to solve this minimization problem
should result in an infnite loop. In this light, I offer my equivalent
C++ solution:

int main()
{
while(1);
return 0;
}
 
K

Kira Yamato

Explore_Imagination said:
The task is to solve a constrained optimization problem in C/C++.
Computational
Time is of high priority. One approach can be to use ready functions
in a "free ware" Optimization Library (if available). If any one of
you have any idea about such library please inform me. I am dealing
with Constrained Optimization for the first time so please guide me
How I should solve this problem. I will appreciate suggestions from
you.


Find values of x that minimize f=-x1*x2*x3 and subject to the
constraints:

x1+2*x2+2*x3>0
x1+2*x2+2*x3<72
x2=10

In Matlab it can be solved by:

x0 = [10; 10; 10]; % Starting guess at the solution

A=[]; b=[]; Aeq=[]; beq=[]; lb=[]; ub=[];

[x,fval] = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@(x)constr(x))


%myfun

function f = myfun(x)

f = -x(1)* x(2)* x(3); %f=-x1*x2*x3


%constr

function [c,ceq]=constr(x)

c(1)=0-(x(1)+2*x(2)+2*x(3)); %x1+2*x2+2*x3>0

c(2)=x(1)+2*x(2)+2*x(3)-72; %x1+2*x2+2*x3<72

ceq(1)=x(2)-10; %x2=10

Hoping to hear from you guys !!!!

In C++, the problem can be solved by a program like this:

#include <iostream>
int main ( void ) {
std::cout << "x1 = " << 26 << '\n'
<< "x2 = " << 10 << '\n'
<< "x3 = " << 13 << '\n';
}

Since you said that time is of high priority, I made the program as fast as
I could without compromising readability.

Actually, this problem has no solution.

Because of the strict inequality in the constraints, the region of the
domain is an open set.

Sorry, I should not have said open set. The constraints do not define
an open set in R^3.

However, the crucial point is that it is not a closed set, hence not
compact. Without compactness, there is no guarantee of existence of
solutions to extrema problems.

The rest of the post below is fine.
 
S

shazled

The task is to solve a constrained optimization problem in C/C++.
Computational
Time is of high priority. One approach can be to use ready functions
in a "free ware" Optimization Library (if available). If any one of
you have any idea about such library please inform me. I am dealing
with Constrained Optimization for the first time so please guide me
How I should solve this problem. I will appreciate suggestions from
you.

I'd recommend the GNU Scientific Library for this. Providing your
problem is linear, you may also find the discussion in the book
"Numerical Recipes" quite useful -- the code from this book isn't free
though.

Saul
 
K

Kira Yamato

The task is to solve a constrained optimization problem in C/C++.
Computational
Time is of high priority. One approach can be to use ready functions
in a "free ware" Optimization Library (if available). If any one of
you have any idea about such library please inform me. I am dealing
with Constrained Optimization for the first time so please guide me
How I should solve this problem. I will appreciate suggestions from
you.


Find values of x that minimize f=-x1*x2*x3 and subject to the
constraints:

x1+2*x2+2*x3>0
x1+2*x2+2*x3<72
x2=10

In Matlab it can be solved by:

x0 = [10; 10; 10]; % Starting guess at the solution

A=[]; b=[]; Aeq=[]; beq=[]; lb=[]; ub=[];

[x,fval] = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@(x)constr(x))


%myfun

function f = myfun(x)

f = -x(1)* x(2)* x(3); %f=-x1*x2*x3


%constr

function [c,ceq]=constr(x)

c(1)=0-(x(1)+2*x(2)+2*x(3)); %x1+2*x2+2*x3>0

c(2)=x(1)+2*x(2)+2*x(3)-72; %x1+2*x2+2*x3<72

ceq(1)=x(2)-10; %x2=10

Hoping to hear from you guys !!!!

I think MATLAB can generate C codes from m files. Since you seem to
have a solution in MATLAB already, why not look into that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top