Browse Source

add brute force

pull/1/head
Chris Cromer 2 years ago
parent
commit
a595ed46ab
11 changed files with 194 additions and 27 deletions
  1. +1
    -1
      Makefile
  2. +41
    -0
      src/brute_force.c
  3. +8
    -17
      src/distance.c
  4. +19
    -0
      src/include/brute_force.h
  5. +3
    -3
      src/include/distance.h
  6. +1
    -0
      src/include/points.h
  7. +55
    -4
      src/points.c
  8. +0
    -0
      test/100.pto
  9. +6
    -0
      test/5.pto
  10. +2
    -1
      test/Makefile
  11. +58
    -1
      test/test.c

+ 1
- 1
Makefile View File

@ -1,7 +1,7 @@
CC=gcc
CFLAGS=-Wall -Isrc/include -DDEBUG -g
LDFLAGS=-lm
SRC=src/points.c src/timer.c src/random.c src/read_file.c
SRC=src/points.c src/timer.c src/read_file.c src/distance.c src/brute_force.c
OBJ=$(SRC:.c=.o)
all: points informe


+ 41
- 0
src/brute_force.c View File

@ -0,0 +1,41 @@
/*
* Copyright 2018 Christopher Cromer
* Copyright 2018 Rodolfo Cuevas
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include "points.h"
/**
* Encontrar los 2 puntos más cercano usando fuerza bruta
* @param points Los puntos a calcular
* @param n La cantidad de puntos en el array points
* @param minimum_dist La distancia mininmo encontrado
* @return Retorna los 2 puntos mas cercanos
*/
point_t * brute_force(point_t *points, unsigned int n, double *minimum_dist) {
point_t *closest_pair = malloc(sizeof(point_t) * 2);
int i;
int j;
double dist;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if ((dist = distance(points[i], points[j])) < *minimum_dist) {
*minimum_dist = dist;
closest_pair[0] = points[i];
closest_pair[1] = points[j];
}
}
}
return closest_pair;
}

src/random.c → src/distance.c View File


+ 19
- 0
src/include/brute_force.h View File

@ -0,0 +1,19 @@
/*
* Copyright 2018 Christopher Cromer
* Copyright 2018 Rodolfo Cuevas
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _POINTS_BRUTE_FORCE
#define _POINTS_BRUTE_FORCE
point_t * brute_force(point_t *points, unsigned int n, double *dist);
#endif

src/include/random.h → src/include/distance.h View File


+ 1
- 0
src/include/points.h View File

@ -19,4 +19,5 @@
float x;
float y;
} point_t;
double distance(point_t point1, point_t point2);
#endif

+ 55
- 4
src/points.c View File

@ -17,9 +17,12 @@
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include "timer.h"
#include <float.h>
#include "points.h"
#include "distance.h"
#include "timer.h"
#include "read_file.h"
#include "brute_force.h"
#define POINTS_VERSION "1.0.0"
@ -36,6 +39,25 @@ void print_usage() {
fprintf(stdout, " -v, --version mostrar la versión del programa\n");
}
/**
* Empezar los pasos antes de correr
*/
void start_algorithm(const char *message, int n) {
fprintf(stdout, "%s", message);
fflush(stdout);
start_timer();
}
/**
* Empezar los pasos después de correr
*/
void end_algorithm() {
stop_timer();
fprintf(stdout, "done\n");
print_timer();
fprintf(stdout, "\n");
}
/**
* La entrada del programa
* @param argc La cantidad de argumentos pasado al programa
@ -43,7 +65,9 @@ void print_usage() {
* @return Retorna el codigo de error o 0 por exito
*/
int main (int argc, char **argv) {
double dist = DBL_MAX;
point_t *points = NULL;
point_t *closest_points = NULL;
char *filename = NULL;
int brute = 0;
int divide = 0;
@ -116,8 +140,7 @@ int main (int argc, char **argv) {
return 2;
}
points = malloc(sizeof(point_t));
if ((opt = read_file(filename, &points, &n)) == 1) {
if ((opt = read_file(filename, &points, &n))) {
if (filename != NULL) {
free(filename);
}
@ -127,10 +150,38 @@ int main (int argc, char **argv) {
}
return 3;
}
if (filename != NULL) {
free(filename);
}
if (n < 2) {
fprintf(stderr, "Error: Se necesita un minimo de 2 puntos!\n");
return 4;
}
if (brute) {
start_algorithm("Brute force corriendo... ", n);
closest_points = brute_force(points, n, &dist);
end_algorithm();
printf("point 1: x: %f y: %f\n", closest_points[0].x, closest_points[0].y);
printf("point 2: x: %f y: %f\n", closest_points[1].x, closest_points[1].y);
printf("distance: %lf\n\n", dist);
free(closest_points);
closest_points = NULL;
}
if (divide) {
start_algorithm("Divide and conquer corriendo... ", n);
//closest_points = divide_and_conquer(points, n, &dist);
end_algorithm();
/*printf("point 1: x: %f y: %f\n", closest_points[0].x, closest_points[0].y);
printf("point 2: x: %f y: %f\n", closest_points[1].x, closest_points[1].y);
printf("distance: %lf\n", dist);
free(closest_points);
closest_points = NULL;*/
}
free(points);
return 0;
}

100.pto → test/100.pto View File


+ 6
- 0
test/5.pto View File

@ -0,0 +1,6 @@
5
2 3
7 9
1 100
1 1
5 10

+ 2
- 1
test/Makefile View File

@ -1,9 +1,10 @@
CC=gcc
CFLAGS=-Wall -I../src/include -DDEBUG -g
LDFLAGS=-lm
SRC=test.c
OBJ=$(SRC:.c=.o)
OBJ+=../src/random.o ../src/read_file.o
OBJ+=../src/read_file.o ../src/distance.o ../src/brute_force.o
all: test


+ 58
- 1
test/test.c View File

@ -17,18 +17,75 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <float.h>
#include "points.h"
#include "read_file.h"
#include "brute_force.h"
/**
* Comparar 2 double para ver si son casi igual
* @param d1 El primer double
* @param d2 El segudno double
* @return Retorna 1 si son igual o 0 sino
*/
int compare_double(double d1, double d2) {
double precision = 0.000000001;
if (((d1 - precision) < d2) && ((d1 + precision) > d2)) {
return 1;
}
else {
return 0;
}
}
/**
* El programa de test
* @param argc La cantidad de argumentos
* @param argv Los argumentos
* @return Retorna 0 por exito o 1 si falló algun test
*/
int main(int argc, char **argv) {
//int pass;
int passed = 0;
int failed = 0;
point_t *points = NULL;
unsigned int n = 0;
double dist = DBL_MAX;
fprintf(stdout, "Running tests:\n");
if (read_file("test/100.pto", &points, &n) == 0) {
fprintf(stdout, "\tread file: passed\n");
passed++;
}
else {
fprintf(stdout, "\tread file: failed\n");
failed++;
}
if (failed > 0) {
fprintf(stdout, "\tbrute force: failed\n");
failed++;
}
else {
fprintf(stdout, "\tbrute force: ");
fflush(stdout);
brute_force(points, n, &dist);
if (compare_double(dist, 0.067687840)) {
fprintf(stdout, "passed\n");
passed++;
}
else {
fprintf(stdout, "failed\n");
failed++;
}
}
fprintf(stdout, "%d tests passed\n", passed);
fprintf(stdout, "%d tests failed\n", failed);
free(points);
if (failed == 0) {
return 0;
}


Loading…
Cancel
Save