diff --git a/src/A.vec b/src/A.vec index d91c10ce59844ee1d7c2b41904a55de35b0d308e..3eb5cfa57a1fac9c0071d11b1a40abbf93a297d1 100644 Binary files a/src/A.vec and b/src/A.vec differ diff --git a/src/B.vec b/src/B.vec index 0cb59b88747379c1728f9423fa3a8c9ca50b62ef..38bac75cfe6ed316927e9dfdf92bfcd0eaf0fb94 100644 Binary files a/src/B.vec and b/src/B.vec differ diff --git a/src/C/descente_radiant b/src/C/descente_radiant index 5c551e08ec2eb31bb85ea28fcabfb630b6ad66b2..b818b0b20b92795a248423ef20fe7cdb79acc738 100755 Binary files a/src/C/descente_radiant and b/src/C/descente_radiant differ diff --git a/src/C/equation.c b/src/C/equation.c index e732e5ac421aa0ffdf658cfd66d584f1994fb997..14d5ceed3770e6bc32a077ef298cbb6172e51c57 100644 --- a/src/C/equation.c +++ b/src/C/equation.c @@ -1,13 +1,17 @@ #include "equation.h" double get_random() { - return ((double)(rand() % (RND_MAX + 1 - RND_MIN) + RND_MIN))/1; + return ((double)(rand() % (RND_MAX + 1 - RND_MIN) + RND_MIN))/1; // non rabatu entre 0 et 1 pour un meilleur résultat visuel +} + +int get_random_int(int min, int max) { + return ((double)(rand() % (max + 1 - min) + min)); } void generate_points(point_t* points, int N) { for(int i = 0; i < N; i++){ point_t pt; - pt.x = i * GRAPH_X / NB_POINTS; + pt.x = (i * GRAPH_X) / NB_POINTS; // -nan and inf, get_random() work pt.y = EQUATION_PENTE * pt.x + EQUATION_ORDO + get_random(); // printf("points[%d] = (point_t){.x=%f, .y=%f};\n", i, pt.x, pt.y); points[i] = pt; @@ -46,7 +50,59 @@ void create_group(group_t *group, point_t* points, int n_points, int n_groups) { group->points[cpt-1][cpt2] = points[i]; cpt2++; } +} + +void create_group_random(group_t *group, point_t* points, int n_points, int n_groups) { + // randomize points list in new tmp list + int* indexes = malloc(sizeof(int) * n_points); //index 0 -> 40-1 + int index_free = n_points; + + for (int i = 0; i < n_points; i++) { + indexes[i] = i; + } + + point_t* tmp = malloc(sizeof(point_t)*n_points); + + int index; + for (int i = 0; i < n_points; i++) { + index = get_random_int(0, n_points-1); + + while(indexes[index] == -1) { + index = get_random_int(0, n_points-1); + } + + indexes[index] = -1; + + tmp[i] = points[index]; + } + // replace data + points = tmp; + + // generate group + group->n_points = n_points; + group->n_groups = n_groups; + group->points = malloc(sizeof(point_t*) * n_groups); + + for (int i = 0; i < group->n_groups; i++) { + group->points[i] = malloc(sizeof(point_t) * (int)(n_points/n_groups)); + } + + int cpt = 0; + int cpt2 = 0; + for (int i = 0; i < n_points; i++) { + if(i % (int)(n_points/n_groups) == 0) { + cpt++; + cpt2 = 0; + } + + group->points[cpt-1][cpt2] = points[i]; + cpt2++; + } + + // free data + free(indexes); + free(tmp); } void print_group(group_t *group){ @@ -64,12 +120,7 @@ void destroy_group(group_t *group) { { free(group->points[i]); } - free(group); -} - - -vector_t direction_croissance() { - + free(group->points); } double erreur_quadratique(double a, double b, point_t* points, int nb_points) { @@ -98,13 +149,13 @@ vector_t descente_radian(point_t* points, int N) D += points[i].y; } - printf("a = %fl,b = %fl,c = %fd,d = %fd",A,B,C,D); + printf("A = %f, B = %f, C = %f, D = %f\n",A,B,C,D); vector_t vold = {.x=1, .y=1}; vector_t vnew = {.x=2, .y=2}; //x = a , y = b vector_t vcal; - double y = 0.001; //0,000296189 + double y = 0.0015; //0,000296189 int cpt = 0; while(norme(soustraction_vecteur(vnew,vold)) > 0.001) //0.00001 @@ -114,11 +165,10 @@ vector_t descente_radian(point_t* points, int N) vcal.x = (vold.x*A) + (vold.y*B) - C; vcal.y = (vold.x*B) + (double)(vold.y*N) - D; vnew = soustraction_vecteur(vold,produit_scalaire(y,vcal)); - //print_vector(vnew); cpt++; } - printf("\n%d\n",cpt); + printf("Iterations: %d\n",cpt); return vnew; } diff --git a/src/C/equation.h b/src/C/equation.h index 71e6363152bcf982657fe80c9ab5e7a25ce92047..73e17e42f4a86d7490b556c6df136850a20b958b 100644 --- a/src/C/equation.h +++ b/src/C/equation.h @@ -48,6 +48,8 @@ double fd(vector_t x); // test // Gestion des groupes void create_group(group_t *group, point_t* points, int n_points, int n_groups); +void create_group_random(group_t *group, point_t* points, int n_points, int n_groups); + void print_group(group_t *group); void destroy_group(group_t *group); diff --git a/src/C/main.c b/src/C/main.c index 1a341692f22180d760cd1559df19c9e888dacd84..3041d6eea7c68f967a1aa6fb679859969799fa8e 100644 --- a/src/C/main.c +++ b/src/C/main.c @@ -14,6 +14,11 @@ int main(int argc, char* argv[]) { if (argc > 1) { nb_points = atoi(argv[1]); + + while(nb_points%3 != 0) { // On s'assure qu'on puisse le diviser en 3 groupe + nb_points--; + } + points = malloc(sizeof(point_t)*nb_points); generate_points(points, nb_points); } else { @@ -51,13 +56,13 @@ int main(int argc, char* argv[]) { } group_t groups; - create_group(&groups, points, nb_points, 3); - print_group(&groups); + create_group_random(&groups, points, nb_points, 3); + //print_group(&groups); vector_t resAB = descente_radian(points, nb_points); point_t* AB = malloc(sizeof(point_t)); *AB = (point_t){resAB.x, resAB.y}; - print_vector(resAB); + printf("gradient(%f,%f)\n", resAB.x, resAB.y); double_vector_t *X = create_vector_x(points, nb_points); double_vector_t *Y = create_vector_y(points, nb_points); @@ -70,7 +75,6 @@ int main(int argc, char* argv[]) { export_vector("../B.vec", B); free(points); - //free(groups); free(AB); destroy_vector(&X); @@ -78,5 +82,5 @@ int main(int argc, char* argv[]) { destroy_vector(&A); destroy_vector(&B); - // destroy_group(&groups); + destroy_group(&groups); } \ No newline at end of file diff --git a/src/Python/__pycache__/load_vec.cpython-38.pyc b/src/Python/__pycache__/load_vec.cpython-38.pyc index 008896e4b7630f666d16ba4fb67a7029ebae6d04..980c002f6b613749dd09c94420f350e05b552a9b 100644 Binary files a/src/Python/__pycache__/load_vec.cpython-38.pyc and b/src/Python/__pycache__/load_vec.cpython-38.pyc differ diff --git a/src/Python/display.py b/src/Python/display.py new file mode 100644 index 0000000000000000000000000000000000000000..e9a74c7b9d5860689cdd1513bd871e74617a0543 --- /dev/null +++ b/src/Python/display.py @@ -0,0 +1,27 @@ +from matplotlib import pyplot as plt +from numpy.lib.npyio import loadtxt +from load_vec import load_vector +import numpy as np + +X_nuage_point = load_vector("../X.vec") +Y_nuage_point = load_vector("../Y.vec") + +A = load_vector("../A.vec") # +B = load_vector("../B.vec") # + +# Nuage de points +plt.scatter(X_nuage_point, Y_nuage_point, marker='x', label="Points") + + +# Meilleure droite +x = np.array(range(int(max(X_nuage_point))+1)) +y = x * A + B +plt.plot(x, y, label="Meilleure droite", color="red") + + +plt.title("Optimisation") +plt.xlabel("X") +plt.ylabel("Y") +plt.legend(loc="lower right") + +plt.show() diff --git a/src/Python/example.py b/src/Python/example.py deleted file mode 100644 index 14e5e61a53bd7aef64305a84d93f59741a6e3a95..0000000000000000000000000000000000000000 --- a/src/Python/example.py +++ /dev/null @@ -1,35 +0,0 @@ -from matplotlib import pyplot as plt -from numpy.lib.npyio import loadtxt -from load_vec import load_vector -import numpy as np - -X_nuage_point = load_vector("../X.vec") -Y_nuage_point = load_vector("../Y.vec") - -A = load_vector("../A.vec") # -B = load_vector("../B.vec") # - -# Nuage de points -type_of_data = 'cure' # curve = point connecté -if type_of_data == 'curve': - plt.plot(X_nuage_point, Y_nuage_point, label="Points") -else: - plt.scatter(X_nuage_point, Y_nuage_point, marker='x', label="Points") - - -# Meilleure droite -x = np.array(range(10)) -y = x * A + B - -type_of_data = 'curve' # curve = point connecté -if type_of_data == 'curve': - plt.plot(x, y, label="Meilleure droite") -else: - plt.scatter(x, y, marker='x', label="Meilleure droite") - -plt.title("Optimisation") -plt.xlabel("X") -plt.ylabel("Y") -plt.legend(loc="lower right") - -plt.show() diff --git a/src/Y.vec b/src/Y.vec index 876a7f2358fd8d2eec250d670d8709db0497c1a1..96483871cd390d8a80f91a5377a99714ed5268b3 100644 Binary files a/src/Y.vec and b/src/Y.vec differ