Skip to content
Snippets Groups Projects
Commit 74304955 authored by ExtraDev's avatar ExtraDev
Browse files

advance on rapport and implementation of quadratic error.

parent 89e9e703
Branches
No related tags found
No related merge requests found
No preview for this file type
#include "equation.h"
double get_random_between_0_and_1() {
return ((double)(rand() % (RND_MAX + 1 - RND_MIN) + RND_MIN))/1; // non rabatu entre 0 et 1 pour un meilleur résultat visuel
return ((double)(rand() % (RND_MAX + 1 - RND_MIN) + RND_MIN))/10; // non rabatu entre 0 et 1 pour un meilleur résultat visuel
}
int get_random_int(int min, int max) {
......@@ -11,125 +11,13 @@ int get_random_int(int min, int max) {
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; // -nan and inf, get_random_between_0_and_1() work
pt.x = ((i * GRAPH_X) / NB_POINTS); // -nan and inf, get_random_between_0_and_1() work
pt.y = EQUATION_PENTE * pt.x + EQUATION_ORDO + get_random_between_0_and_1();
// printf("points[%d] = (point_t){.x=%f, .y=%f};\n", i, pt.x, pt.y);
points[i] = pt;
}
}
void create_group(group_t *group, point_t* points, int n_points, int n_groups) {
group->n_points = n_points;
group->n_groups = n_groups;
group->n_points_in_group = (int)(n_points/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++;
}
}
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->n_points_in_group = (int)(n_points/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) * group->n_points_in_group);
}
int cpt = 0;
int cpt2 = 0;
for (int i = 0; i < n_points; i++) {
if(i % group->n_points_in_group == 0) {
cpt++;
cpt2 = 0;
}
group->points[cpt-1][cpt2] = points[i];
cpt2++;
}
// free data
free(indexes);
free(tmp);
}
point_t* regroup(group_t *group, int cadran_a, int cadran_b) {
int nb_points = group->n_points_in_group * 2; // 30 / 3 * 2 = 20
point_t* result = malloc(sizeof(point_t) * nb_points);
for (int i = 0; i < nb_points/2; i++)
{
result[i] = group->points[cadran_a][i];
result[nb_points/2+i] = group->points[cadran_b][i];
}
return result;
}
void print_group(group_t *group){
printf("nb points %d ", group->n_points);
printf("nb groups %d ", group->n_groups);
printf("points per group %d \n", group->n_points_in_group);
for (int i = 0; i < group->n_groups; i++)
{
for (int j = 0; j < group->n_points_in_group; j++)
{
printf("[%d:%d] = %f,%f\n", i, j, group->points[i][j].x, group->points[i][j].y);
}
}
}
void destroy_group(group_t *group) {
for (int i = 0; i < group->n_groups; i++)
{
free(group->points[i]);
}
free(group->points);
}
double erreur_quadratique(double a, double b, point_t* points, int nb_points) {
double res = 0;
double tmp = 0;
......@@ -138,7 +26,6 @@ double erreur_quadratique(double a, double b, point_t* points, int nb_points) {
tmp = (a * points[i].x + b - points[i].y);
res += (tmp*tmp);
}
return res;
}
......@@ -179,14 +66,6 @@ vector_t descente_gradient(point_t* points, int N)
return vnew;
}
double fd(vector_t x) {
return (2 * x.x) + (2 * x.y);
}
double f(vector_t x) {
return (x.x * x.x) + (x.y * x.y);
}
vector_t produit_scalaire(double scalaire, vector_t a) {
vector_t result;
result.x = scalaire*a.x;
......@@ -221,7 +100,7 @@ double norme(vector_t a) {
return result;
}
vector_t print_vector(vector_t vec) {
void print_vector(vector_t vec) {
printf("x: %f\n", vec.x);
printf("y: %f\n", vec.y);
}
\ No newline at end of file
......@@ -20,11 +20,6 @@ typedef struct _point_t {
double y;
} point_t;
typedef struct _group_t {
int n_points, n_groups, n_points_in_group;
point_t **points;
} group_t;
typedef struct _vector_t {
double x;
double y;
......@@ -32,6 +27,7 @@ typedef struct _vector_t {
double get_random_between_0_and_1();
int get_random_int(int min, int max);
void generate_points(point_t* points, int N);
......@@ -40,15 +36,8 @@ vector_t descente_gradient(point_t* points, int N);
double erreur_quadratique(double a, double b, point_t* points, int nb_points);
// 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);
point_t* regroup(group_t *group, int cadran_a, int cadran_b);
void print_group(group_t *group);
void destroy_group(group_t *group);
// Arithmétique de vecteurs
vector_t print_vector(vector_t vec);
void print_vector(vector_t vec);
vector_t produit_scalaire(double scalaire, vector_t a);
vector_t addiction_vecteur(vector_t a, vector_t b);
vector_t soustraction_vecteur(vector_t a, vector_t b);
......
#include "group.h"
void create_group(group_t *group, point_t* points, int n_points, int n_groups) {
group->n_points = n_points;
group->n_groups = n_groups;
group->n_points_in_group = (int)(n_points/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++;
}
}
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->n_points_in_group = (int)(n_points/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) * group->n_points_in_group);
}
int cpt = 0;
int cpt2 = 0;
for (int i = 0; i < n_points; i++) {
if(i % group->n_points_in_group == 0) {
cpt++;
cpt2 = 0;
}
group->points[cpt-1][cpt2] = points[i];
cpt2++;
}
// free data
free(indexes);
free(tmp);
}
point_t* regroup(group_t *group, int cadran_a, int cadran_b) {
int nb_points = group->n_points_in_group * 2; // 30 / 3 * 2 = 20
point_t* result = malloc(sizeof(point_t) * nb_points);
for (int i = 0; i < nb_points/2; i++)
{
result[i] = group->points[cadran_a][i];
result[nb_points/2+i] = group->points[cadran_b][i];
}
return result;
}
void print_group(group_t *group){
printf("nb points %d ", group->n_points);
printf("nb groups %d ", group->n_groups);
printf("points per group %d \n", group->n_points_in_group);
for (int i = 0; i < group->n_groups; i++)
{
for (int j = 0; j < group->n_points_in_group; j++)
{
printf("[%d:%d] = %f,%f\n", i, j, group->points[i][j].x, group->points[i][j].y);
}
}
}
void destroy_group(group_t *group) {
for (int i = 0; i < group->n_groups; i++)
{
free(group->points[i]);
}
free(group->points);
}
\ No newline at end of file
#ifndef _GROUP_H
#define _GROUP_H
#include "equation.h"
typedef struct _group_t {
int n_points, n_groups, n_points_in_group;
point_t **points;
} group_t;
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);
point_t* regroup(group_t *group, int cadran_a, int cadran_b);
void print_group(group_t *group);
void destroy_group(group_t *group);
#endif
\ No newline at end of file
......@@ -4,6 +4,7 @@
#include <time.h>
#include "vector.h"
#include "equation.h"
#include "group.h"
int main(int argc, char* argv[]) {
srand( time( NULL ) );
......@@ -68,10 +69,18 @@ int main(int argc, char* argv[]) {
// Faire la comparaison des gradients
printf("----- Calcul des gradients -----\n");
printf("Gradient nuage complet: %f; %f\n", droite_nuage.x, droite_nuage.y);
printf("Gradient groupe 0-1: %f; %f\n", droite_0_1.x, droite_0_1.y);
printf("Gradient groupe 0-2: %f; %f\n", droite_0_2.x, droite_0_2.y);
printf("Gradient groupe 1-2: %f; %f\n", droite_1_2.x, droite_1_2.y);
printf("Gradient nuage complet: %f; %f ", droite_nuage.x, droite_nuage.y);
printf("Erreur quadratique: %f\n", erreur_quadratique(droite_nuage.x, droite_nuage.y, nuage_points, nb_points));
printf("Gradient groupe 0-1: %f; %f ", droite_0_1.x, droite_0_1.y);
printf("Erreur quadratique: %f\n", erreur_quadratique(droite_0_1.x, droite_0_1.y, groupe_0_1, groups.n_points_in_group));
printf("Gradient groupe 0-2: %f; %f ", droite_0_2.x, droite_0_2.y);
printf("Erreur quadratique: %f\n", erreur_quadratique(droite_0_2.x, droite_0_2.y, groupe_0_2, groups.n_points_in_group));
printf("Gradient groupe 1-2: %f; %f ", droite_1_2.x, droite_1_2.y);
printf("Erreur quadratique: %f\n", erreur_quadratique(droite_1_2.x, droite_1_2.y, groupe_1_2, groups.n_points_in_group));
printf("\n");
printf("----- Calcul des différence gradients en fonction des groupes -----\n");
printf("Différence gradient complet ET groupe 0-1: %f; %f \n", droite_nuage.x - droite_0_1.x, droite_nuage.y - droite_0_1.y);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment