#include "stdio.h"
#include <vector>
using namespace std;
float function(float alfa[5],float H[5][5],float sign[5])
{
float ret = alfa[0]+alfa[1]+alfa[2]+alfa[3]+alfa[4];
for(int j=0;j<5;++j)
{
float t=0.0;
for(int i=0;i<5;++i)
{
t+=sign[i]*alfa[i]*H[j][i];
}
ret += -1*(t*alfa[j]*sign[j])/2;
}
return ret;
}
int main(void)
{
float matrix[5][4]={
{1,5,1},
{1,2,1},
{2,2,-1},
{2,1,-1},
{1,1,-1}};
float H[5][5];
vector<float> c1;
vector<float> c2;
for(int i=0;i<5;++i)
{
c1.push_back(matrix[i][0]);
c2.push_back(matrix[i][1]);
}
for(int i=0;i<5;++i)
{
for(int j=0;j<5;++j)
{
H[i][j]=c1[i]*c1[j]+c2[i]*c2[j];
printf("%f ",H[i][j]);
}
printf("
");
}
float alfa[5]={3,3,2,2,2};
float sign[5];
for(int i=0;i<5;++i)
sign[i]=matrix[i][2];
float last_r = function(alfa,H,sign);
float new_r;
float con_r;
for(int i=0;i<5;++i)
{
for(int j=0;j<5;j++)
{
printf("%f,alfa={%f,%f,%f,%f,%f}
",last_r,alfa[0],alfa[1],alfa[2],alfa[3],alfa[4]);
if(i==j) continue;
else if((alfa[i]<0.01&&alfa[i]>-0.01)&&(alfa[j]<0.01&&alfa[j]>-0.01)) continue;
else if((alfa[j]>0.01)&&(alfa[i]<0.01&&alfa[i]>-0.01))
{
while(alfa[j]>0.01){
alfa[i]+=0.1;
new_r = function(alfa,H,sign);
if( new_r > last_r )
{
alfa[j] -= 0.1*sign[i]*sign[j];
last_r = function(alfa,H,sign);
}
else
{
alfa[i]-=0.1;
break;
}
};
}
else if((alfa[i]>0.01)&&(alfa[j]<0.01&&alfa[j]>-0.01))
{
while(alfa[i]>0.01){
alfa[j]+=0.1;
new_r = function(alfa,H,sign);
if( new_r > last_r )
{
alfa[i] -= 0.1*sign[i]*sign[j];
last_r = function(alfa,H,sign);
}
else
{
alfa[j]-=0.1;
break;
}
};
}
else
{
alfa[j]+=0.1;
new_r = function(alfa,H,sign);
alfa[j]-=0.2;
con_r = function(alfa,H,sign);
alfa[j]+=0.1;
if(new_r>con_r&&new_r>last_r)
{
while(alfa[i]>0.01&&alfa[j]>0.01)
{
alfa[j] += 0.1;
alfa[i] -= 0.1*sign[i]*sign[j];
new_r = function(alfa,H,sign);
if(new_r > last_r)
{
last_r = new_r;
}
else
{
alfa[j] -= 0.1;
alfa[i] += 0.1*sign[i]*sign[j];
break;
}
};
}
else if(con_r>new_r&&con_r>last_r)
{
while(alfa[i]>0.01&&alfa[j]>0.01)
{
alfa[j] -= 0.1;
alfa[i] += 0.1*sign[i]*sign[j];
con_r = function(alfa,H,sign);
if(con_r > last_r)
{
last_r = con_r;
}
else
{
alfa[j] += 0.1;
alfa[i] -= 0.1*sign[i]*sign[j];
break;
}
}
}
else
{}
}
}
}
printf("%f,alfa={%f,%f,%f,%f,%f}
",last_r,alfa[0],alfa[1],alfa[2],alfa[3],alfa[4]);
return 0;
}