Skip to content
Snippets Groups Projects
opti.c 4.99 KiB
Newer Older
philippe.montando's avatar
philippe.montando committed
#include "opti.h"
philippe.montando's avatar
philippe.montando committed
int LINE_POINTS = 1000000;
int CLOUD_POINTS = 1000;
double X_VALUE = 0.000001;
double RANDOMNESS = 0.001;
// Learning rate
double LR = 0.001;
philippe.montando's avatar
philippe.montando committed


philippe.montando's avatar
philippe.montando committed
// y(x)=ax+b
// crée plein de points sur une même ligne
philippe.montando's avatar
philippe.montando committed
Point* line(double a, double b){
    Point * points = (Point*)malloc(sizeof(Point)*LINE_POINTS);
    double x = 0.0;
    for(int i=0;i<LINE_POINTS;i++){
philippe.montando's avatar
philippe.montando committed
        Point d;
        d.x = x;
        d.y = (a*x)+b;
        points[i]=d;
        //printf("%lf, %lf", d.x, d.y);
        x+=X_VALUE;
    }
    return points;
}
philippe.montando's avatar
philippe.montando committed

// la fonction pow() de math.h avait un problème sur mon pc windows donc je l'ai refaite
double popow(double v, double w){
    double u=v;
    for(int i=1;i<w;i++){
        v*=u;
    }
    return v;
}

// Génère un double random...
philippe.montando's avatar
philippe.montando committed
double double_random(double min, double max) 
{
    double my_random;
    my_random = (double)rand()/RAND_MAX*(max-min)+min;
    //printf ( "%f\n", my_random);
    return my_random;
}
philippe.montando's avatar
philippe.montando committed

// Version 1 du nuage, se doit d'appeler line indirectement
Point* cloud1(Point* a_line){
    Point * my_cloud = malloc(sizeof(Point)*CLOUD_POINTS);
    for(int i=0;i<CLOUD_POINTS;i++){
philippe.montando's avatar
philippe.montando committed
        Point chosen_point = a_line[rand()%LINE_POINTS-1];
        double new_y = double_random(chosen_point.y-RANDOMNESS, chosen_point.y+RANDOMNESS);
        my_cloud[i].x=chosen_point.x;
        my_cloud[i].y=new_y;
        printf("\nx = %f | y = %f\n", my_cloud[i].x,my_cloud[i].y);
    }
    return my_cloud;
}
philippe.montando's avatar
philippe.montando committed

// Version 2 du nuage, peut être généré grâce aux a et b d'une droite
// Génère aléatoirement des points sur l'axe des x (qui ne sont donc pas espacés uniformément)
Point* cloud2(double a, double b){
    Point * my_cloud = malloc(sizeof(Point)*CLOUD_POINTS);
    for(int i=0;i<CLOUD_POINTS;i++){
        Point chosen_point;
        chosen_point.x = double_random(0,1);
        for(int j=0;j<CLOUD_POINTS-1;j++){
            while(chosen_point.x==my_cloud[j].x){
                chosen_point.x = double_random(0,1);
            }
        }
        double rj = double_random(-RANDOMNESS, RANDOMNESS);
        chosen_point.y = (a*chosen_point.x)+b+rj;
        my_cloud[i].x=chosen_point.x;
        my_cloud[i].y=chosen_point.y;
        //printf("\nx = %f | y = %f\n", my_cloud[i].x,my_cloud[i].y);
    }
    return my_cloud;
}

// version 3 du nuage, plus simple, x espacés uniformément
Point* cloud3(double a, double b){
    Point * my_cloud = malloc(sizeof(Point)*CLOUD_POINTS);
    for(int i=0;i<CLOUD_POINTS;i++){
        my_cloud[i].x=i/CLOUD_POINTS;
        double rj = double_random(-RANDOMNESS, RANDOMNESS);
        my_cloud[i].y = (a*my_cloud[i].x)+b+rj;
    }
    return my_cloud;
}

// Méthode des moindres carrés
philippe.montando's avatar
philippe.montando committed
double* averages(Point* my_cloud, int cloud_size){
    //double* avg = (double*)malloc(sizeof(double)*4);
    double* avg = malloc(sizeof(double)*4);
    double x_avg, y_avg, x2_avg, xy_avg;
    for(int i=0; i<cloud_size-1;i++){
        x_avg+=my_cloud[i].x;
        y_avg+=my_cloud[i].y;
        x2_avg+=popow(my_cloud[i].x,2);
        xy_avg+=(my_cloud[i].x*my_cloud[i].y);
    x_avg/=cloud_size;y_avg/=cloud_size;x2_avg/=cloud_size;xy_avg/=cloud_size;
    avg[0]=x_avg;avg[1]=y_avg;avg[2]=x2_avg;avg[3]=xy_avg;
    printf("x=%f - y=%f - x2=%f - xy=%f", avg[0],avg[1],avg[2],avg[3]);
    return avg;
}

// Méthode des moindres carrés
philippe.montando's avatar
philippe.montando committed
double* a_and_b(double* my_averages){
    double* a_n_b = (double*)malloc(sizeof(double)*2);
philippe.montando's avatar
philippe.montando committed
    double x_avg = my_averages[0];
    double y_avg = my_averages[1];
    double x2_avg = my_averages[2];
    double xy_avg = my_averages[3];
    double a = (xy_avg-(x_avg*y_avg))/(x2_avg-popow(x_avg,2));
    double b = y_avg-(a*x_avg);
    a_n_b[0]=a;
    a_n_b[1]=b;
    return a_n_b;
}

philippe.montando's avatar
philippe.montando committed
// Sommes (similaire à averages() mais ne divise pas par le nb de points)
double* sums(Point* my_cloud, int cloud_size){
    //double* avg = (double*)malloc(sizeof(double)*4);
    double* my_sums = malloc(sizeof(double)*4);
    double x, y, x2, xy;
    for(int i=0; i<cloud_size-1;i++){
        x+=my_cloud[i].x;
        y+=my_cloud[i].y;
        x2+=popow(my_cloud[i].x,2);
        xy+=(my_cloud[i].x*my_cloud[i].y);
    }
    my_sums[0]=x;my_sums[1]=y;my_sums[2]=x2;my_sums[3]=xy;
    printf("x=%f - y=%f - x2=%f - xy=%f", my_sums[0],my_sums[1],my_sums[2],my_sums[3]);
    return my_sums;
}

double slope(){
return 0.0;
}


double cost(){
    return 0.0;
}

philippe.montando's avatar
philippe.montando committed
int main (void){
    srand ( time ( NULL));
    /*// Create a vector X = [0,1,2...99]
    double_vector_t *X = iota(100);
    // Create a vector Y = my_function(x)
    double_vector_t *Y = apply_function(X, my_function);

    // Export our vectors into files
    export_vector("./X.vec", X);
    export_vector("./Y.vec", Y);

    // Free our vectors
    destroy_vector(&Y);
    destroy_vector(&X);
    */
    //Point* first_line = line(7.0, 5.5);
    Point* fluffy = cloud2(7.0, 5.5);
philippe.montando's avatar
philippe.montando committed
    averages(fluffy, CLOUD_POINTS);
philippe.montando's avatar
philippe.montando committed
    printf("\ndone\n");
philippe.montando's avatar
philippe.montando committed
    return 0;
}