Skip to content
Snippets Groups Projects
Commit 8edae7ff authored by sgegito's avatar sgegito
Browse files

problème réglé, c'était une erreur de taille dans ma descente de gradient qui...

problème réglé, c'était une erreur de taille dans ma descente de gradient qui prenait une variable globale
parent 4f04f076
No related branches found
No related tags found
No related merge requests found
#include "opti.h"
int CLOUD_POINTS = 5000;
int CLOUD_POINTS = 6000;
double RANDOMNESS = 0.01;
// Learning rate
double LR = 0.0001;
// Error rate
double ER = 0.001;
double ER = 0.000001;
// La fonction pow() de math.h avait un problème aavec mon Makefile donc je l'ai refaite
// et puis après avoir résolu le problème (emplacement du -lm) afin de pouvoir utiliser
......@@ -18,6 +18,9 @@ double popow(double v, double w){
return v;
}
double norm(double u){
return sqrt(popow(u,2));
}
// Génère un double random...
double double_random(double min, double max)
{
......@@ -51,29 +54,6 @@ Point* cloud2_5(double *a, double *b){
return my_cloud;
}
Point** cloud_splitter(Point* my_cloud){
Point ** tiny_clouds = malloc(sizeof(Point*)*3);
Point * g1 = (Point*)malloc(sizeof(Point)*(CLOUD_POINTS/3));
Point * g2 = (Point*)malloc(sizeof(Point)*((CLOUD_POINTS/3)));
Point * g3 = (Point*)malloc(sizeof(Point)*(CLOUD_POINTS-((CLOUD_POINTS*2)/3)));
// Pas besoin de shuffle les points ici car déjà fait dans cloud2_5()
for(int i=0;i<CLOUD_POINTS;i++){
if(i<(CLOUD_POINTS/3)){
g1[i]=my_cloud[i];
}if(i>=(CLOUD_POINTS/3) && i<((CLOUD_POINTS*2)/3)){
g2[i-(CLOUD_POINTS/3)]=my_cloud[i];
}else{
g3[i-((CLOUD_POINTS*2)/3)]=my_cloud[i];
}
}
tiny_clouds[0]=g1;
tiny_clouds[1]=g2;
tiny_clouds[2]=g3;
//free(g1);free(g2);free(g3);
printf("\nSplitter ok\n");
return tiny_clouds;
}
// version 3 du nuage, plus simple, x espacés uniformément
Point* cloud3(double *a, double *b){
......@@ -112,7 +92,7 @@ double gradient(double *a, double *b, Point* my_cloud, int cloud_size, bool is_a
return my_gradient;
}
void gradient_descent_v4(double *a, double *b, Point* my_cloud){
void gradient_descent_v4(double *a, double *b, Point* my_cloud, int my_cloud_size){
double gr_A,gr_B,my_new_a,my_new_b,current_cost;
double *zero;
double z = 0.0;
......@@ -120,9 +100,9 @@ void gradient_descent_v4(double *a, double *b, Point* my_cloud){
current_cost = cost2(zero,zero,a,b);
int nb_it = 0;
//double* a_n_b = (double*)malloc(sizeof(double)*2);
while(current_cost>=ER){
gr_A = gradient(a, b, my_cloud, CLOUD_POINTS, true);
gr_B = gradient(a, b, my_cloud, CLOUD_POINTS, false);
while(current_cost>ER){
gr_A = gradient(a, b, my_cloud, my_cloud_size, true);
gr_B = gradient(a, b, my_cloud, my_cloud_size, false);
// Les 2 lignes ci-dessous pourraient être condensées, mais c'est pour rendre le code
// plus explicite que je sépare volontairement les étapes.
my_new_a = (*a) - (LR * gr_A);
......@@ -135,30 +115,53 @@ void gradient_descent_v4(double *a, double *b, Point* my_cloud){
//current_cost = cost(a, b, my_cloud, CLOUD_POINTS);
nb_it++;
}
printf("\nCost : %g || Nombre d'itérations : %d\n ", current_cost, nb_it);
//printf("\nCost : %g || Nombre d'itérations : %d\n ", current_cost, nb_it);
}
double cost2(double *a, double *b, double *new_a, double *new_b){
return sqrt(popow((*new_a - *a),2)+popow((*new_b - *b), 2));
}
Point** cloud_splitter(Point* my_cloud){
Point ** tiny_clouds = malloc(sizeof(Point*)*3);
Point * g1 = (Point*)malloc(sizeof(Point)*(CLOUD_POINTS/3));
Point * g2 = (Point*)malloc(sizeof(Point)*((CLOUD_POINTS/3)));
Point * g3 = (Point*)malloc(sizeof(Point)*(CLOUD_POINTS-((CLOUD_POINTS*2)/3)));
// Pas besoin de shuffle les points ici car déjà fait dans cloud2_5()
for(int i=0;i<CLOUD_POINTS;i++){
if(i<(CLOUD_POINTS/3)){
g1[i]=my_cloud[i];
}if(i>=(CLOUD_POINTS/3) && i<((CLOUD_POINTS*2)/3)){
g2[i-(CLOUD_POINTS/3)]=my_cloud[i];
}else{
g3[i-((CLOUD_POINTS*2)/3)]=my_cloud[i];
}
}
tiny_clouds[0]=g1;
tiny_clouds[1]=g2;
tiny_clouds[2]=g3;
//free(g1);free(g2);free(g3);
return tiny_clouds;
}
Point* cloud_merger(Point* cloud_a, Point* cloud_b, bool g3_here){
int cloud_size;
if(!g3_here){
cloud_size = 2*(CLOUD_POINTS)/3;
cloud_size = (2*CLOUD_POINTS)/3;
}else{
cloud_size = CLOUD_POINTS-(CLOUD_POINTS/3);
}
Point * merged_cloud = (Point*)malloc(sizeof(Point)*cloud_size);
Point* merged_cloud = (Point*)malloc(sizeof(Point)*cloud_size);
for(int i=0;i<cloud_size;i++){
if(i<(CLOUD_POINTS/3)){
merged_cloud[i]=cloud_a[i];
printf("BEFORA : x=%g y=%g\n", cloud_a[i].x, cloud_a[i].y);
//printf("BEFORA : x=%g y=%g\n", cloud_a[i].x, cloud_a[i].y);
}else{
merged_cloud[i]=cloud_b[i-(CLOUD_POINTS/3)];
printf("BEFORB : x=%g y=%g\n", cloud_b[i-(CLOUD_POINTS/3)].x, cloud_b[i-(CLOUD_POINTS/3)].y);
//printf("BEFORB : x=%g y=%g\n", cloud_b[i-(CLOUD_POINTS/3)].x, cloud_b[i-(CLOUD_POINTS/3)].y);
}
printf("MERGED : x=%g y=%g\n\n", merged_cloud[i].x, merged_cloud[i].y);
//printf("MERGED : x=%g y=%g\n\n", merged_cloud[i].x, merged_cloud[i].y);
}
return merged_cloud;
}
......@@ -183,12 +186,23 @@ int main (void){
double b_init = -0.1537833935;
//Point* fluffy = cloud_test();
printf("\na optimal = %f || b optimal = %f\n", a_init, b_init);
double a = double_random(0,1);
double b = double_random(0,1);
double a = double_random(0,10);
double b = double_random(0,10);
double c = a;
double d = b;
double e = a;
double f = b;
double g = a;
double h = b;
double k = a;
double l = b;
double m = a;
double n = b;
int one_third = CLOUD_POINTS/3;
int last_third = CLOUD_POINTS-(2*CLOUD_POINTS/3);
int without_g3_size = (2*CLOUD_POINTS)/3;
int with_g3_size = CLOUD_POINTS-(CLOUD_POINTS/3);
Point* fluffy = cloud2_5(&a_init, &b_init);
//printf("\na random initial = %f || b random initial = %f\n", a, b);
......@@ -198,18 +212,36 @@ int main (void){
printf("\nG%d x = %g\nG%d y = %g\n", j+1,cloud_slayer[j][i].x , j+1, cloud_slayer[j][i].y);
}
}*/
printf("\nPart B 1st it : x = %g y = %g\n\n", cloud_slayer[1][0].x, cloud_slayer[1][0].y);
Point* merged = cloud_merger(cloud_slayer[0], cloud_slayer[1], false);
for(int i=0;i<CLOUD_POINTS*2/3;i++){
//printf("\nMERGED : x=%g y=%g\n", merged[i].x, merged[i].y);
}
Point* merged_1 = cloud_merger(cloud_slayer[0], cloud_slayer[1], false);
Point* merged_2 = cloud_merger(cloud_slayer[0], cloud_slayer[2], true);
Point* merged_3 = cloud_merger(cloud_slayer[1], cloud_slayer[2], true);
/*for(int i=0;i<CLOUD_POINTS*2/3;i++){
printf("\nMERGED : x=%g y=%g\n", merged_1[i].x, merged_1[i].y);
}*/
//gradient_descent_v4(&a, &b, fluffy);
gradient_descent_v4(&a, &b, merged);
printf("\na1 trouvé = %f || b1 trouvé = %f\n", a, b);
gradient_descent_v4(&c, &d, cloud_slayer[2]);
printf("\na2 trouvé = %f || b2 trouvé = %f\n", c, d);
//G1 U G2 et test sur G3
printf("\nG1 U G2 et test sur G3\n");
gradient_descent_v4(&a, &b, merged_1, without_g3_size);
printf("\nam trouvé = %f || bm trouvé = %f\n", a, b);
gradient_descent_v4(&c, &d, cloud_slayer[2], last_third);
printf("\nag trouvé = %f || bg trouvé = %f\n\n", c, d);
//G1 U G3 et test sur G2
printf("\nG1 U G3 et test sur G2\n");
gradient_descent_v4(&e, &f, merged_2, with_g3_size);
printf("\nam trouvé = %f || bm trouvé = %f\n", e, f);
gradient_descent_v4(&g, &h, cloud_slayer[1], one_third);
printf("\nag trouvé = %f || bg trouvé = %f\n\n", g, h);
//G2 U G3 et test sur G1
printf("\nG2 U G3 et test sur G1\n");
gradient_descent_v4(&k, &l, merged_3, with_g3_size);
printf("\nam trouvé = %f || bm trouvé = %f\n", k, l);
gradient_descent_v4(&m, &n, cloud_slayer[0], one_third);
printf("\nag trouvé = %f || bg trouvé = %f\n\n", m, n);
//if(norm(a)-norm(c)<ER && norm(b)-norm(d)<ER);
//printf("\nx = %g\ny = %g\n",cloud_slayer[0][1].x , cloud_slayer[0][1].y);
free(merged);
free(merged_1);
free(cloud_slayer);
free(fluffy);
printf("\ndone\n");
......
......@@ -30,7 +30,7 @@ double* a_and_b(double* my_averages);
double* sums(Point* my_cloud, int cloud_size);
double gradient(double *a, double *b, Point* my_cloud, int cloud_size, bool is_a);
double cost(double *a, double *b, Point* my_cloud);
void gradient_descent_v4(double *a, double *b, Point* my_cloud);
void gradient_descent_v4(double *a, double *b, Point* my_cloud, int my_cloud_size);
double cost2(double *a, double *b, double *new_a, double *new_b);
Point ** cloud_splitter(Point* my_cloud);
Point* cloud_merger(Point* cloud_a, Point* cloud_b, bool g3_here);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment