add brute force

pull/1/head
Chris Cromer 4 years ago
parent 7b0092126d
commit a595ed46ab
  1. 2
      Makefile
  2. 41
      src/brute_force.c
  3. 25
      src/distance.c
  4. 19
      src/include/brute_force.h
  5. 6
      src/include/distance.h
  6. 1
      src/include/points.h
  7. 59
      src/points.c
  8. 0
      test/100.pto
  9. 6
      test/5.pto
  10. 3
      test/Makefile
  11. 59
      test/test.c

@ -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

@ -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;
}

@ -13,24 +13,15 @@
* 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 <time.h>
#include <math.h>
#include "points.h"
/**
* Flag para inicilizar srand solo una vez
* Calcular la distancia entre 2 puntos
* @param point1 El primer punto
* @param point2 El segundo punto
* @return Retorna la distancia entre los 2 puntos
*/
static int sort_rand_initialized = 0;
/**
* Generar un númer al azar entre un mínimo y máximo
* @param min El limite míninmo
* @param max El limite máximo
* @return Retorna el número generado
*/
int gen_rand(int min, int max) {
if (sort_rand_initialized == 0) {
srand((unsigned int) time(NULL));
sort_rand_initialized = 1;
}
return rand() % (max + 1 - min) + min;
double distance(point_t point1, point_t point2) {
return sqrt(pow(point1.x - point2.x, 2) + pow(point1.y - point2.y, 2));
}

@ -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

@ -13,7 +13,7 @@
* 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_RANDOM
#define _POINTS_RANDOM
int gen_rand(int min, int max);
#ifndef _POINTS_DISTANCE
#define _POINTS_DISTANCE
double distance(point_t point1, point_t point2);
#endif

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

@ -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;
}

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

@ -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

@ -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