/* File: fi.C */ #include "fi.h" #include "task.h" int number_of_variables = 3; #define NUMBER_OF_FCALC 1 #define NUMBER_OF_OBJECTS 500 static int number_of_objects = NUMBER_OF_OBJECTS; static int object [NUMBER_OF_OBJECTS]; static double object_cost [NUMBER_OF_OBJECTS]; static double object_weight [NUMBER_OF_OBJECTS]; static double ratio [NUMBER_OF_OBJECTS]; static double total_weight; static double normx [3]; static int init = 0; static void normalize (const double *xx, int dim, double *x); static double fif (const double *xx, int dim); double fi (const double *xx, int dim) { if (init == 0) { for (int i = 0; i < NUMBER_OF_OBJECTS; i++) { object_cost [i] = ats () * ats () * ats (); object_weight [i] = ats () * ats () * ats (); ratio [i] = object_cost [i] / object_weight [i]; total_weight += object_weight [i]; } total_weight *= ats () * ats () * ats (); init = 1; } normalize (xx, dim, normx); double fmax = -r1mach (2); for (int k = 0; k < NUMBER_OF_FCALC; k++) { double s = fif (normx, dim); fmax = MAX (s, fmax); } return -fmax; } static double fif (const double *x, int dim) { for (int i = 0; i < number_of_objects; i++) object [i] = 0; double weight = 0.; double cost = 0.; for (int n = number_of_objects; n > 0; n--) { double maxh = - r1mach (2); int k; for (int i = 0; i < number_of_objects; i++) if ((int) object [i] == 0) if (maxh < ratio [i]) { maxh = ratio [i]; k = i; } double a2 = 0.; for (i = 0; i < number_of_objects; i++) if (object [i] == 0) a2 += ratio [i]; a2 = 1. / a2; double eps = ats (); double r = 0.; for (i = 0; i < number_of_objects; i++) { if (object [i] == 0) r += 1. / n * x [0] + a2 * x [1] * ratio [i] + x [2] * (i == k ? 1 : 0); if (r > eps) { weight += object_weight [i]; if (weight > total_weight) return cost; cost += object_cost [i]; object [i] = 1; break; } } } return cost; } static void normalize (const double *xx, int dim, double *x) { double sum = 0.; for (int i = 0; i < dim; i++) sum += xx [i]; for (i = 0; i < dim; i++) x [i] = xx [i] / sum; } void constr (const double *x, int n, double *g, int ng) { /* ng > 0 - calculates all constaints ng < 0 - calculates only constraint with number (-ng) */ if (ng > 0) { g [0] = x [0] + 0.625; g [1] = 0.125 - x [0]; g [2] = x [1] + 0.5; g [3] = 0.25 - x [1]; } else { ng = -ng; switch (ng) { case 1: g [0] = x [0] + 0.625; break; case 2: g [1] = 0.125 - x [0]; break; case 3: g [2] = x [1] + 0.5; break; case 4: g [3] = 0.25 - x [1]; break; } } }