Skip to content
Snippets Groups Projects
Commit 7c5a31c2 authored by Jason R Wilson's avatar Jason R Wilson
Browse files

files for PSA01

parent ff0986b6
No related merge requests found
4.470802 -1.871527
-3.031774 1.549377
7.894372 2.928784
10.321351 -3.490053
0.534141 -1.425673
5.245451 2.888229
0.027076 4.315900
10.085365 -2.872810
-9.584009 4.365102
-10.437469 -5.304982
-10.459263 4.861448
7.939559 -2.733902
8.744194 1.447623
1.480710 -1.101613
-2.749660 2.498327
7.262462 3.714656
-8.293333 1.769513
7.320706 2.824994
-5.063172 4.331169
-2.904489 0.980203
-10.571174 -0.320163
0.294089 -0.043147
-5.212059 4.574710
-8.738593 -0.670101
9.374125 1.672909
-4.407383 -0.483221
9.705810 -4.398309
-1.623478 -0.740097
-8.052749 0.157121
-4.516453 -3.180130
-3.194269 0.916789
-9.707856 5.021509
5.249016 -1.522861
8.035496 1.810677
-7.345268 1.834054
-7.949954 -4.082847
-10.124908 -3.121888
-11.080439 0.154886
6.951096 -2.473320
-4.811675 -3.625790
0.070514 -5.575789
-6.066796 0.698825
-10.061157 -1.079548
10.407074 2.759780
4.698264 -4.522170
0.608994 2.590193
7.941049 -4.122449
8.184578 1.571563
-0.198618 0.247553
-8.588731 -1.558276
11.173092 3.597192
2.902615 1.271134
-10.926899 -3.346488
9.853496 2.998616
-0.646473 1.521443
-11.194432 0.452273
5.011800 -2.625334
0.250094 0.460775
10.927726 0.394153
3.098638 -0.937429
6.537779 -3.624053
9.158989 5.387815
4.741745 -2.591301
7.699449 -1.629041
9.445726 6.029175
-7.391364 4.214542
2.429028 1.782166
-4.582008 3.043505
0.234214 4.717863
-0.439387 -3.906525
-8.674616 2.373730
-1.418618 1.220522
0.851148 3.367162
-8.267115 2.672558
6.798563 1.136338
2.867453 0.118816
5.990139 -3.459365
0.242416 -3.584069
3.353172 0.564160
8.369109 4.087135
-7.979719 2.604741
8.663288 2.113997
-0.376275 0.647530
9.449945 -3.536561
-7.688408 0.326020
10.912657 -4.324923
-1.008561 2.133955
-7.461632 -3.296200
3.706442 0.974182
10.008075 4.060943
6.769445 3.636326
-3.984799 1.411626
-5.074461 2.961612
3.335316 1.043352
2.451567 -1.854812
-9.268376 1.689952
-9.534869 -0.363558
-10.400661 -3.115979
-5.686984 -3.448522
11.328903 0.718192
-7.102361 -4.577878
1.576087 -0.268046
10.341826 -3.156231
10.168671 -6.883715
9.987043 -6.092928
-7.378438 2.143717
4.886129 4.239258
7.431826 2.188098
-4.919944 6.105738
0.401401 0.886585
-7.900566 0.347154
2.820183 -0.558944
4.355351 4.419650
8.098856 3.225768
-2.937645 0.460572
-2.624444 2.006121
5.232943 -3.947831
-1.738663 -0.144068
-9.058603 -0.079247
-0.145825 -5.568225
11.116066 -3.126504
-9.932385 5.977374
10.650692 -3.538089
-8.191760 2.247480
3.919696 2.594774
2.215149 3.349125
-10.453925 -5.780264
-9.333344 4.971423
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#include <omp.h>
#define MAX_POINTS 2000
// read the file of points
int read_file (float points[], char* filename) {
int n = 0;
float next[2];
FILE* file_ptr;
file_ptr = fopen(filename,"r");
if (file_ptr == NULL) {
printf ("error : could not open file %s for reading\n",filename);
exit(1);
}
while (fscanf (file_ptr,"%f %f",next,next+1) == 2) {
if (n < MAX_POINTS) {
points[2*n] = next[0];
points[2*n+1] = next[1];
n += 1;
} else {
printf ("Too many points in file %s\n",filename);
fclose (file_ptr);
exit(1);
}
}
fclose (file_ptr);
return n;
}
// calculate the distance squared between two points
float calc_dist_sq (float u[], float v[]) {
float diff_x = u[0] - v[0];
float diff_y = u[1] - v[1];
return (diff_x*diff_x + diff_y*diff_y);
}
typedef struct centers_s {
int c[5];
} centers_type;
// compute the cost squared for centers in the centers array
float center_cost_sq (float dist_sqs[], int n, centers_type check, float min_cost_sq) {
float cost_sq = 0;
for (int i=0;i<n;i++) {
float dist_sq_1 = dist_sqs[i*n+check.c[0]];
float dist_sq_2 = dist_sqs[i*n+check.c[1]];
float dist_sq_3 = dist_sqs[i*n+check.c[2]];
float dist_sq_4 = dist_sqs[i*n+check.c[3]];
float dist_sq_5 = dist_sqs[i*n+check.c[4]];
float min_dist_sq = dist_sq_1;
if (dist_sq_2 < min_dist_sq) {
min_dist_sq = dist_sq_2;
}
if (dist_sq_3 < min_dist_sq) {
min_dist_sq = dist_sq_3;
}
if (dist_sq_4 < min_dist_sq) {
min_dist_sq = dist_sq_4;
}
if (dist_sq_5 < min_dist_sq) {
min_dist_sq = dist_sq_5;
}
// check to see if we can abort early
if (min_dist_sq > min_cost_sq) {
return min_dist_sq;
}
if (min_dist_sq > cost_sq) {
cost_sq = min_dist_sq;
}
}
return cost_sq;
}
// solve the 5-center problem exactly
// returns the minimal cost and an optimal set of 5 centers
double solve_5center (float dist_sqs[], int n, centers_type* optimal, long int* num_checked) {
double min_cost_sq = DBL_MAX;
*num_checked = 0;
for (int i=0;i<n-4;i++) {
for (int j=i+1;j<n-3;j++) {
for (int k=j+1;k<n-2;k++) {
for (int l=k+1;l<n-1;l++) {
for (int m=l+1;m<n;m++) {
*num_checked += 1;
centers_type check = { i, j, k, l, m };
double cost_sq = center_cost_sq (dist_sqs,n,check,min_cost_sq);
if (cost_sq < min_cost_sq) {
min_cost_sq = cost_sq;
*optimal = check;
}
}
}
}
}
}
// return the minimal cost
return sqrt(min_cost_sq);
}
int main (int argc, char** argv) {
// the points array
float points[2*MAX_POINTS];
// read filename and thread count from command line
if (argc < 3) {
printf ("Command usage : %s %s %s\n",argv[0],"filename","thread_count");
return 1;
}
int thread_count = atoi(argv[2]);
omp_set_num_threads(thread_count);
// read dataset
int n = read_file (points,argv[1]);
// calculate the distances squared table
float dist_sqs[n*n];
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
dist_sqs[i*n+j] = calc_dist_sq (points+2*i,points+2*j);
}
}
// print the number of points and number of threads
printf ("number of points = %d, number of threads = %d\n",n,thread_count);
// start the timer
double start_time, end_time;
start_time = omp_get_wtime();
// solve the 5-center problem exactly
centers_type optimal;
long int num_checked;
double min_cost = solve_5center (dist_sqs,n,&optimal,&num_checked);
// stop the timer
end_time = omp_get_wtime();
double elapsed = end_time-start_time;
// print out the number of 5-tuples checked
printf ("Total 5-tuples checked = %ld\n",num_checked);
// print the minimal cost for the 5-center problem
printf ("minimal cost = %g\n",min_cost);
// print an optimal solution to the 5-center problem
printf ("optimal centers : %d %d %d %d %d\n",optimal.c[0],optimal.c[1],
optimal.c[2],optimal.c[3],optimal.c[4]);
// print the elapsed time
printf ("elapsed time = %g seconds\n",elapsed);
// print the tuples checked per second
printf ("5-tuples checked per second = %.0lf\n",num_checked/elapsed);
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment