Optimization functions ASAP

hi i am writing this code for a class problem, it is not a computer science class. its another discipline, we were given the option to use excel but i wanted to use c++. the problem involves a lot of iterations and assumptions, i have already written the complete code. the final part of the code is to use an optimization function of two inputs to generate the two assumptions that would give the lowest results.

so basically i need a function that treats the code like a blackbox. i have downloaded nlopt but i don't understand how to use it on Xcode ( which i am currently using to type the code)

is there any library already on c++ that can help me optimize ? if not how can one use nlopt.

#include <iostream>
#include <cmath>
#include <math.h>
#include <nlopt.h>
using namespace std;


float xyz (float P2,float P3)
{
float z,a,b,c,d,e,f,g,a2,b2,q2,d2,e2,f2,g2,a3,b3,q3,d3,e3,f3,g3,c1=0.4,c2=0.2,c3=0.1,c4=0.1,c5=0.1,c6=0.05,c7=0.05,k1,k2,k3,k4,k5,k6,k7,v,v1 = 0.0,v2= 0.0, v3= 0.0;
float C1,C2,C3,C4,C5,C6,C7,K1,K2,K3,K4,K5,K6,K7,Kf1,Kf2,Kf3,Kf4,Kf5,Kf6,Kf7,Cf1,Cf2,Cf3,Cf4,Cf5,Cf6,Cf7;
float P1=514.7;
float P4=14.7;
float xyz;



a= -292860/pow(550,2)+ 8.2445 - (0.8951*log(P1))+ 59.8465/pow(P1,2);
b= -687248.2/pow(550,2)+ 7.90694 - (0.866*log(P1))+ 49.02654/pow(P1,2);
c= -970688.6/pow(550,2) + 7.15059 - (0.76984*log(P1)) + 6.90224/P1;
d= -1280557/pow(550,2)+ 7.94986 - (0.96455*log(P1))+ 0/pow(P1,2);
e= -1524891/pow(550,2)+ 7.33129 - (0.89143*log(P1))+ 0/pow(P1,2);
f= -1778901/pow(550,2)+ 6.96783 - (0.84634*log(P1))+ 0/pow(P1,2);
g= -2013803/pow(550,2)+ 6.52914 - (0.79543*log(P1))+ 0/pow(P1,2);

k1= exp(a);
k2= exp(b);
k3= exp(c);
k4= exp(d);
k5= exp(e);
k6= exp(f);
k7= exp(g);


do
{
z=((k1-1)*c1/((1-v)+(k1*v)))+((k2-1)*c2/((1-v)+(k2*v)))+((k3-1)*c3/((1-v)+(k3*v)))+((k4-1)*c4/((1-v)+(k4*v)))+((k5-1)*c5/((1-v)+(k5*v)))+((k6-1)*c6/((1-v)+(k6*v)))+((k7-1)*c7/((1-v)+(k7*v)));
v=v+0.00001;
}

while ((abs(z)>0.01));



c1=c1/(1+v*(k1-1));
c2=c2/(1+v*(k2-1));
c3=c3/(1+v*(k3-1));
c4=c4/(1+v*(k4-1));
c5=c5/(1+v*(k5-1));
c6=c6/(1+v*(k6-1));
c7=c7/(1+v*(k7-1));


a= -292860/pow(550,2)+ 8.2445 - (0.8951*log(P2))+ 59.8465/pow(P2,2);
b= -687248.2/pow(550,2)+ 7.90694 - (0.866*log(P2))+ 49.02654/pow(P2,2);
c= -970688.6/pow(550,2) + 7.15059 - (0.76984*log(P2)) + 6.90224/P2;
d= -1280557/pow(550,2)+ 7.94986 - (0.96455*log(P2))+ 0/pow(P2,2);
e= -1524891/pow(550,2)+ 7.33129 - (0.89143*log(P2))+ 0/pow(P2,2);
f= -1778901/pow(550,2)+ 6.96783 - (0.84634*log(P2))+ 0/pow(P2,2);
g= -2013803/pow(550,2)+ 6.52914 - (0.79543*log(P2))+ 0/pow(P2,2);

a2= -292860/pow(550,2)+ 8.2445 - (0.8951*log(P3))+ 59.8465/pow(P3,2);
b2= -687248.2/pow(550,2)+ 7.90694 - (0.866*log(P3))+ 49.02654/pow(P3,2);
q2= -970688.6/pow(550,2) + 7.15059 - (0.76984*log(P3)) + 6.90224/P3;
d2= -1280557/pow(550,2)+ 7.94986 - (0.96455*log(P3))+ 0/pow(P3,2);
e2= -1524891/pow(550,2)+ 7.33129 - (0.89143*log(P3))+ 0/pow(P3,2);
f2= -1778901/pow(550,2)+ 6.96783 - (0.84634*log(P3))+ 0/pow(P3,2);
g2= -2013803/pow(550,2)+ 6.52914 - (0.79543*log(P3))+ 0/pow(P3,2);

a3= -292860/pow(550,2)+ 8.2445 - (0.8951*log(P4))+ 59.8465/pow(P4,2);
b3= -687248.2/pow(550,2)+ 7.90694 - (0.866*log(P4))+ 49.02654/pow(P4,2);
q3= -970688.6/pow(550,2) + 7.15059 - (0.76984*log(P4)) + 6.90224/P4;
d3= -1280557/pow(550,2)+ 7.94986 - (0.96455*log(P4))+ 0/pow(P4,2);
e3= -1524891/pow(550,2)+ 7.33129 - (0.89143*log(P4))+ 0/pow(P4,2);
f3= -1778901/pow(550,2)+ 6.96783 - (0.84634*log(P4))+ 0/pow(P4,2);
g3= -2013803/pow(550,2)+ 6.52914 - (0.79543*log(P4))+ 0/pow(P4,2);

k1= exp(a);
k2= exp(b);
k3= exp(c);
k4= exp(d);
k5= exp(e);
k6= exp(f);
k7= exp(g);

K1= exp(a2);
K2= exp(b2);
K3= exp(q2);
K4= exp(d2);
K5= exp(e2);
K6= exp(f2);
K7= exp(g2);

Kf1= exp(a3);
Kf2= exp(b3);
Kf3= exp(q3);
Kf4= exp(d3);
Kf5= exp(e3);
Kf6= exp(f3);
Kf7= exp(g3);


do
{
z=((k1-1)*c1/((1-v1)+(k1*v1)))+((k2-1)*c2/((1-v1)+(k2*v1)))+((k3-1)*c3/((1-v1)+(k3*v1)))+((k4-1)*c4/((1-v1)+(k4*v1)))+((k5-1)*c5/((1-v1)+(k5*v1)))+((k6-1)*c6/((1-v1)+(k6*v1)))+((k7-1)*c7/((1-v1)+(k7*v1)));
v1=v1+0.00001;

C1=c1/(1+v1*(k1-1));
C2=c2/(1+v1*(k2-1));
C3=c3/(1+v1*(k3-1));
C4=c4/(1+v1*(k4-1));
C5=c5/(1+v1*(k5-1));
C6=c6/(1+v1*(k6-1));
C7=c7/(1+v1*(k7-1));


}
while ((abs(z)>0.01));



do
{
z=((K1-1)*C1/((1-v2)+(K1*v2)))+((K2-1)*C2/((1-v2)+(K2*v2)))+((K3-1)*C3/((1-v2)+(K3*v2)))+((K4-1)*C4/((1-v2)+(K4*v2)))+((K5-1)*C5/((1-v2)+(k5*v2)))+((K6-1)*C6/((1-v2)+(K6*v2)))+((K7-1)*C7/((1-v2)+(K7*v2)));
v2=v2+0.000001;

Cf1=C1/(1+v1*(K1-1));
Cf2=C2/(1+v2*(K2-1));
Cf3=C3/(1+v2*(K3-1));
Cf4=C4/(1+v2*(K4-1));
Cf5=C5/(1+v2*(K5-1));
Cf6=C6/(1+v2*(K6-1));
Cf7=C7/(1+v2*(K7-1));

}

while ((abs(z)>0.001));

do
{
z=((Kf1-1)*Cf1/((1-v3)+(Kf1*v3)))+((Kf2-1)*Cf2/((1-v3)+(Kf2*v3)))+((Kf3-1)*Cf3/((1-v3)+(Kf3*v3)))+((Kf4-1)*Cf4/((1-v3)+(Kf4*v3)))+((Kf5-1)*Cf5/((1-v3)+(Kf5*v3)))+((Kf6-1)*Cf6/((1-v3)+(Kf6*v3)))+((Kf7-1)*Cf7/((1-v3)+(Kf7*v3)));
v3=v3+0.00001;


}

while ((abs(z)>0.001));

cout<<v<<" "<<v1<<" "<<v2<<" "<<v3<<endl;

xyz=(v+(v1*(1-v))+(v2*(1-v)*(1-v1))+((v3*(1-v)*(1-v1)*(1-v2))))/((1-v)+((1-v)*(1-v1))+((1-v)*(1-v1)*(1-v2))+((1-v)*(1-v1)*(1-v2)*(1-v3)));

return xyz;
}
// parameters for contraint

typedef struct
{
float P2, P3;
} my_constraint_data;

//setup contraint function

//setup Obj function


int main ()
{
float GOR;
GOR= xyz (200,100);
cout<<GOR<<endl;


return 0;
}

I need an optimization function to provide me with values for P2 and P3 that give the minimum value for function xyz

thanks so much
Last edited on
have i done something wrong ?
Not wrong, we just need more details and a specific question to help with. Unless someone here just happens to have solid knowledge of the nlopt package it's hard to help unless you share some code and explain your needs a little better. ;-)
#include <iostream>
#include <cmath>
#include <math.h>
#include <nlopt.h>
using namespace std;


float xyz (float P2,float P3)
{
float z,a,b,c,d,e,f,g,a2,b2,q2,d2,e2,f2,g2,a3,b3,q3,d3,e3,f3,g3,c1=0.4,c2=0.2,c3=0.1,c4=0.1,c5=0.1,c6=0.05,c7=0.05,k1,k2,k3,k4,k5,k6,k7,v,v1 = 0.0,v2= 0.0, v3= 0.0;
float C1,C2,C3,C4,C5,C6,C7,K1,K2,K3,K4,K5,K6,K7,Kf1,Kf2,Kf3,Kf4,Kf5,Kf6,Kf7,Cf1,Cf2,Cf3,Cf4,Cf5,Cf6,Cf7;
float P1=514.7;
float P4=14.7;
float xyz;



a= -292860/pow(550,2)+ 8.2445 - (0.8951*log(P1))+ 59.8465/pow(P1,2);
b= -687248.2/pow(550,2)+ 7.90694 - (0.866*log(P1))+ 49.02654/pow(P1,2);
c= -970688.6/pow(550,2) + 7.15059 - (0.76984*log(P1)) + 6.90224/P1;
d= -1280557/pow(550,2)+ 7.94986 - (0.96455*log(P1))+ 0/pow(P1,2);
e= -1524891/pow(550,2)+ 7.33129 - (0.89143*log(P1))+ 0/pow(P1,2);
f= -1778901/pow(550,2)+ 6.96783 - (0.84634*log(P1))+ 0/pow(P1,2);
g= -2013803/pow(550,2)+ 6.52914 - (0.79543*log(P1))+ 0/pow(P1,2);

k1= exp(a);
k2= exp(b);
k3= exp(c);
k4= exp(d);
k5= exp(e);
k6= exp(f);
k7= exp(g);


do
{
z=((k1-1)*c1/((1-v)+(k1*v)))+((k2-1)*c2/((1-v)+(k2*v)))+((k3-1)*c3/((1-v)+(k3*v)))+((k4-1)*c4/((1-v)+(k4*v)))+((k5-1)*c5/((1-v)+(k5*v)))+((k6-1)*c6/((1-v)+(k6*v)))+((k7-1)*c7/((1-v)+(k7*v)));
v=v+0.00001;
}

while ((abs(z)>0.01));



c1=c1/(1+v*(k1-1));
c2=c2/(1+v*(k2-1));
c3=c3/(1+v*(k3-1));
c4=c4/(1+v*(k4-1));
c5=c5/(1+v*(k5-1));
c6=c6/(1+v*(k6-1));
c7=c7/(1+v*(k7-1));


a= -292860/pow(550,2)+ 8.2445 - (0.8951*log(P2))+ 59.8465/pow(P2,2);
b= -687248.2/pow(550,2)+ 7.90694 - (0.866*log(P2))+ 49.02654/pow(P2,2);
c= -970688.6/pow(550,2) + 7.15059 - (0.76984*log(P2)) + 6.90224/P2;
d= -1280557/pow(550,2)+ 7.94986 - (0.96455*log(P2))+ 0/pow(P2,2);
e= -1524891/pow(550,2)+ 7.33129 - (0.89143*log(P2))+ 0/pow(P2,2);
f= -1778901/pow(550,2)+ 6.96783 - (0.84634*log(P2))+ 0/pow(P2,2);
g= -2013803/pow(550,2)+ 6.52914 - (0.79543*log(P2))+ 0/pow(P2,2);

a2= -292860/pow(550,2)+ 8.2445 - (0.8951*log(P3))+ 59.8465/pow(P3,2);
b2= -687248.2/pow(550,2)+ 7.90694 - (0.866*log(P3))+ 49.02654/pow(P3,2);
q2= -970688.6/pow(550,2) + 7.15059 - (0.76984*log(P3)) + 6.90224/P3;
d2= -1280557/pow(550,2)+ 7.94986 - (0.96455*log(P3))+ 0/pow(P3,2);
e2= -1524891/pow(550,2)+ 7.33129 - (0.89143*log(P3))+ 0/pow(P3,2);
f2= -1778901/pow(550,2)+ 6.96783 - (0.84634*log(P3))+ 0/pow(P3,2);
g2= -2013803/pow(550,2)+ 6.52914 - (0.79543*log(P3))+ 0/pow(P3,2);

a3= -292860/pow(550,2)+ 8.2445 - (0.8951*log(P4))+ 59.8465/pow(P4,2);
b3= -687248.2/pow(550,2)+ 7.90694 - (0.866*log(P4))+ 49.02654/pow(P4,2);
q3= -970688.6/pow(550,2) + 7.15059 - (0.76984*log(P4)) + 6.90224/P4;
d3= -1280557/pow(550,2)+ 7.94986 - (0.96455*log(P4))+ 0/pow(P4,2);
e3= -1524891/pow(550,2)+ 7.33129 - (0.89143*log(P4))+ 0/pow(P4,2);
f3= -1778901/pow(550,2)+ 6.96783 - (0.84634*log(P4))+ 0/pow(P4,2);
g3= -2013803/pow(550,2)+ 6.52914 - (0.79543*log(P4))+ 0/pow(P4,2);

k1= exp(a);
k2= exp(b);
k3= exp(c);
k4= exp(d);
k5= exp(e);
k6= exp(f);
k7= exp(g);

K1= exp(a2);
K2= exp(b2);
K3= exp(q2);
K4= exp(d2);
K5= exp(e2);
K6= exp(f2);
K7= exp(g2);

Kf1= exp(a3);
Kf2= exp(b3);
Kf3= exp(q3);
Kf4= exp(d3);
Kf5= exp(e3);
Kf6= exp(f3);
Kf7= exp(g3);


do
{
z=((k1-1)*c1/((1-v1)+(k1*v1)))+((k2-1)*c2/((1-v1)+(k2*v1)))+((k3-1)*c3/((1-v1)+(k3*v1)))+((k4-1)*c4/((1-v1)+(k4*v1)))+((k5-1)*c5/((1-v1)+(k5*v1)))+((k6-1)*c6/((1-v1)+(k6*v1)))+((k7-1)*c7/((1-v1)+(k7*v1)));
v1=v1+0.00001;

C1=c1/(1+v1*(k1-1));
C2=c2/(1+v1*(k2-1));
C3=c3/(1+v1*(k3-1));
C4=c4/(1+v1*(k4-1));
C5=c5/(1+v1*(k5-1));
C6=c6/(1+v1*(k6-1));
C7=c7/(1+v1*(k7-1));


}
while ((abs(z)>0.01));



do
{
z=((K1-1)*C1/((1-v2)+(K1*v2)))+((K2-1)*C2/((1-v2)+(K2*v2)))+((K3-1)*C3/((1-v2)+(K3*v2)))+((K4-1)*C4/((1-v2)+(K4*v2)))+((K5-1)*C5/((1-v2)+(k5*v2)))+((K6-1)*C6/((1-v2)+(K6*v2)))+((K7-1)*C7/((1-v2)+(K7*v2)));
v2=v2+0.000001;

Cf1=C1/(1+v1*(K1-1));
Cf2=C2/(1+v2*(K2-1));
Cf3=C3/(1+v2*(K3-1));
Cf4=C4/(1+v2*(K4-1));
Cf5=C5/(1+v2*(K5-1));
Cf6=C6/(1+v2*(K6-1));
Cf7=C7/(1+v2*(K7-1));

}

while ((abs(z)>0.001));

do
{
z=((Kf1-1)*Cf1/((1-v3)+(Kf1*v3)))+((Kf2-1)*Cf2/((1-v3)+(Kf2*v3)))+((Kf3-1)*Cf3/((1-v3)+(Kf3*v3)))+((Kf4-1)*Cf4/((1-v3)+(Kf4*v3)))+((Kf5-1)*Cf5/((1-v3)+(Kf5*v3)))+((Kf6-1)*Cf6/((1-v3)+(Kf6*v3)))+((Kf7-1)*Cf7/((1-v3)+(Kf7*v3)));
v3=v3+0.00001;


}

while ((abs(z)>0.001));

cout<<v<<" "<<v1<<" "<<v2<<" "<<v3<<endl;

xyz=(v+(v1*(1-v))+(v2*(1-v)*(1-v1))+((v3*(1-v)*(1-v1)*(1-v2))))/((1-v)+((1-v)*(1-v1))+((1-v)*(1-v1)*(1-v2))+((1-v)*(1-v1)*(1-v2)*(1-v3)));

return xyz;
}
// parameters for contraint

typedef struct
{
float P2, P3;
} my_constraint_data;

//setup contraint function

//setup Obj function


int main ()
{
float GOR;
GOR= xyz (200,100);
cout<<GOR<<endl;


return 0;
}

I need an optimization function to provide me with values for P2 and P3 that give the minimum value for function xyz
I take it you've decided to use nlopt, but XCode can't find it. Is that the problem? Or do you need help using nlopt too?

I don't really understand the code, but for what you're doing, I probably don't need to at this point. It reminds me of stuff I used see when I was a student.

EDIT:
I've been looking at your code, this is a side effect of badly written code. In the first loop:
1
2
3
4
5
6
do
{
z=((k1-1)*c1/((1-v)+(k1*v)))+((k2-1)*c2/((1-v)+(k2*v)))+((k3-1)*c3/((1-v)+(k3*v)))+((k4-1)*c4/((1-v)+(k4*v)))+((k5-1)*c5/((1-v)+(k5*v)))+((k6-1)*c6/((1-v)+(k6*v)))+((k7-1)*c7/((1-v)+(k7*v)));
v=v+0.00001;
}
while ((abs(z)>0.01));
v is unintialised, so the result v is undefined.

I'll post an improved version soon.
Last edited on
I tried to remove some of the duplication, but as you can see, there's still lot's to be removed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <math.h>
//#include <nlopt.h>
using namespace std;

typedef double real_t;
//typedef float real_t;

real_t xyz(real_t P2,real_t P3)
{
	real_t v{}, v1{}, v2{}, v3{};
	constexpr real_t dv[] = { 0.00001, 0.00001, 0.000001, 0.00001 };
	constexpr real_t zt[] = { 0.01, 0.01, 0.001, 0.001 };

	constexpr real_t value[][7]
	{
		{  -292860,   550, 2, 8.2445,  0.8951,  59.8465,  2 },
		{  -687248.2, 550, 2, 7.90694, 0.866,   49.02654, 2 },
		{  -970688.6, 550, 2, 7.15059, 0.76984,  6.90224, 1 },
		{ -1280557,   550, 2, 7.94986, 0.96455,  0,       2 },
		{ -1524891,   550, 2, 7.33129, 0.89143,  0,       2 },
		{ -1778901,   550, 2, 6.96783, 0.84634,  0,       2 },
		{ -2013803,   550, 2, 6.52914, 0.79543,  0,       2 }
	};

	auto init_func = [](real_t P, const real_t value[])
	{
		return
			value[0]/pow(value[1], value[2]) +
			value[3] -
			value[4]*log(P) +
			value[5]/pow(P, value[6]);
	};

	auto init_vect = [=](real_t P, const real_t value[][7])
	{
		vector<real_t> values
		{
			exp(init_func(P, value[0])),
			exp(init_func(P, value[1])),
			exp(init_func(P, value[2])),
			exp(init_func(P, value[3])),
			exp(init_func(P, value[4])),
			exp(init_func(P, value[5])),
			exp(init_func(P, value[6]))
		};

		return values;
	};

	vector<real_t> c{ 0.4, 0.2, 0.1, 0.1, 0.1, 0.05, 0.05 };
	{
		constexpr real_t P1{ 514.7 };
		const vector<real_t> k{ init_vect(P1, value) };

		real_t z;
		size_t cnt{};
		do
		{
			z = {};
			for (size_t i = 0; i != k.size(); ++i)
				z += (k[i]-1)*c[i] / ((1-v) + (k[i]*v));

			v = v+dv[0];
			++cnt;
		}
		while (abs(z)>zt[0]);

		for (size_t i = 0; i != c.size(); ++i)
			c[i] = c[i] / (1 + v*(k[i] - 1));
	}

	vector<real_t> C(c.size());
	{
		const vector<real_t> k{ init_vect(P2, value) };

		real_t z;
		size_t cnt{};
		do
		{
			z = {};
			for (size_t i = 0; i != c.size(); ++i)
				z += (k[i]-1)*c[i] / ((1-v1) + (k[i]*v1));

			v1 = v1+dv[1];
			++cnt;
		}
		while (abs(z)>zt[1]);

		for (size_t i = 0; i != c.size(); ++i)
			C[i] = c[i] / (1 + v1*(k[i] - 1));
	}

	vector<real_t> Cf(c.size());
	{
		const vector<real_t> K{ init_vect(P3, value) };

		real_t z;
		size_t cnt{};
		do
		{
			z = {};
			for (size_t i = 0; i != K.size(); ++i)
				z += (K[i]-1)*C[i] / ((1-v2) + (K[i]*v2));

			v2 = v2+dv[2];
			++cnt;
		}
		while (abs(z)>zt[2]);

		for (size_t i = 0; i != c.size(); ++i)
			Cf[i] = C[i] / (1 + v2*(K[i] - 1));
	}

	{
		constexpr real_t P4{ 14.7 };
		const vector<real_t> Kf{ init_vect(P4, value) };

		real_t z;
		size_t cnt{};
		do
		{
			z = {};
			for (size_t i = 0; i != Kf.size(); ++i)
				z += (Kf[i]-1)*Cf[i] / ((1-v3) + (Kf[i]*v3));

			v3 = v3+dv[3];
			++cnt;
		}
		while (abs(z)>zt[3]);
	}

	real_t xyz =
		(v + (v1*(1-v)) + (v2*(1-v)*(1-v1)) + ((v3*(1-v)*(1-v1)*(1-v2)))) / 
		((1-v) + ((1-v)*(1-v1)) + ((1-v)*(1-v1)*(1-v2)) + ((1-v)*(1-v1)*(1-v2)*(1-v3)));

	return xyz;
}

// parameters for contraint
struct my_constraint_data
{
	real_t P2, P3;
};

//setup contraint function

//setup Obj function

int main ()
{
	real_t GOR = xyz(200, 100);
	cout << GOR << endl;
}
Last edited on
Topic archived. No new replies allowed.