#include #include #include #include #include "task.h" #define DIM 3 #define NUMBER_OF_JOBS 7 #define NUMBER_OF_MACHINES 40 #define NUMBER_OF_FCALC 100 #define FALSE 0 #define TRUE 1 int number_of_variables = DIM; class step { private: double time; public: step (void) {}; void operator = (const step B) {time = B .time;}; step (double t) {time = t;}; double Time (void) {return time;}; }; class job { private: step stp [128]; int number_of_steps; int used; public: void Use (void) {used = TRUE;} job (void) {number_of_steps = 0; used = FALSE;}; void Add (const step s) {stp [number_of_steps++] = s;}; int IsFinished (void) {return used;}; void Clear (void) {number_of_steps = 0; used = FALSE;}; void Init (void) {used = FALSE;}; step operator [] (int i) {return stp [i];}; double Length (void) { double sum = 0.; for (int i = 0; i < number_of_steps; i++) sum += stp [i] .Time (); return sum; }; }; static void normalize (const double *xx, int dim, double *x); static void do_selection (job *j, int selection, int number_of_jobs, double *machine_time, double *job_time); static double fif (const double *xx, int dim); static void calc_heuristics (int n, double *h, job *j); double fi (const double *xx, int dim) { double x [DIM]; normalize (xx, dim, x); double fmin = r1mach (2); for (int k = 0; k < NUMBER_OF_FCALC; k++) { double s = fif (x, dim); fmin = MIN (s, fmin); } printf ("%g %g %g %g\n", fmin, x[0], x[1], x[2]) ; return fmin; } static double fif (const double *xx, int dim) { static job j [NUMBER_OF_JOBS]; static int init = FALSE; if (init == FALSE) { for (int l = 0; l < NUMBER_OF_JOBS; l++) { j [l] .Clear (); for (int i = 0; i < NUMBER_OF_MACHINES; i++) { j [l] .Add (step ((double)((int)(100. * ats ())))); } } init = TRUE; } double machine_time [NUMBER_OF_MACHINES]; double job_time [NUMBER_OF_JOBS]; for (int i = 0; i < NUMBER_OF_JOBS; i++) { j [i] .Init (); job_time [i] = 0.; } for (i = 0; i < NUMBER_OF_MACHINES; i++) machine_time [i] = 0.; for (;;) { int n = 0; int selection; for (int i = 0; i < NUMBER_OF_JOBS; i++) { if (j [i] .IsFinished () == TRUE) n++; else selection = i; } if (n == NUMBER_OF_JOBS) break; if (n == NUMBER_OF_JOBS - 1) { do_selection (j, selection, NUMBER_OF_JOBS, machine_time, job_time); break; } double h [NUMBER_OF_JOBS]; calc_heuristics (NUMBER_OF_JOBS, h, j); double a2 = 0.; double a3 = 0.; for (i = 0; i < NUMBER_OF_JOBS; i++) { if (j [i] .IsFinished () == TRUE) continue; a2 += h [i]; a3 += h [i] * h [i]; } a2 = 1. / a2; a3 = 1. / a3; double eps = ats (); double r = 0.; for (i = 0; i < NUMBER_OF_JOBS; i++) { if (j [i] .IsFinished () == TRUE) continue; r += 1. / (NUMBER_OF_JOBS - n) * xx [0] + a2 * xx [1] * h [i] + a3 * xx [2] * h [i] * h [i]; if (r > eps) { do_selection (j, i, NUMBER_OF_JOBS, machine_time, job_time); break; } } } double max = 0.; for (i = 0; i < NUMBER_OF_JOBS; i++) max = MAX (max, job_time [i]); return max; } static void do_selection (job *j, int selection, int number_of_jobs, double *machine_time, double *job_time) { for (int i = 0; i < NUMBER_OF_MACHINES; i++) { job_time [selection] = machine_time [i] = MAX (job_time [selection], machine_time [i]) + j [selection] [i] .Time (); } j [selection] .Use (); } 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; } /*static void calc_heuristics (int n, double *h, job *j) { double minA = r1mach (2); double maxA = -r1mach (2); for (int jj = 0; jj < n; jj++) { if (j [jj] .IsFinished () == TRUE) continue; minA = MIN (minA, j [jj] .Length ()); maxA = MAX (maxA, j [jj] .Length ()); } if (minA == maxA) for (jj = 0; jj < n; jj++) h [jj] = 1.; else { for (jj = 0; jj < n; jj++) { if (j [jj] .IsFinished () == TRUE) continue; h [jj] = (j [jj] .Length () - minA) / (maxA - minA); } } }*/ static void calc_heuristics (int n, double *h, job *j) { for (int jj = 0; jj < n; jj++) { if (j [jj] .IsFinished () == TRUE) continue; double ej = (j [jj] [0] .Time () < j [jj] [NUMBER_OF_MACHINES - 1] .Time ()) ? 1. : -1.; double minj = r1mach (2); for (int k = 0; k < NUMBER_OF_MACHINES - 1; k++) if (minj > j [jj] [k] .Time () + j [jj] [k + 1] .Time ()) minj = j [jj] [k] .Time () + j [jj] [k + 1] .Time (); h [jj] = ej / minj; } double minA = r1mach (2); double maxA = -r1mach (2); for (jj = 0; jj < n; jj++) { if (j [jj] .IsFinished () == TRUE) continue; minA = MIN (minA, h [jj]); maxA = MAX (maxA, h [jj]); } if (minA == maxA) for (jj = 0; jj < n; jj++) h [jj] = 1.; else { for (jj = 0; jj < n; jj++) { if (j [jj] .IsFinished () == TRUE) continue; h [jj] = (h [jj] - minA) / (maxA - minA); } } } /* Definition of constraints */ void constr (const double *x, int n, double *g, int ng) { /* ng > 0 - calculates all constaints ng < 0 - calculates only constraint with number (-ng) */ double x1 = x [0]; double x2 = x [1]; double x3 = x1 * x1; double x4 = x2 * x2; if (ng > 0) { g [0] = 25. - x3 - x4; g [1] = 10. * x1 - x3 + 10. * x2 - x4 - 34.; g [2] = x1; g [3] = x2; } else { ng = -ng; switch (ng) { case 1: g [0] = 25. - x3 - x4; break; case 2: g [1] = 10. * x1 - x3 + 10. * x2 - x4 - 34.; break; case 3: g [2] = x1; break; case 4: g [3] = x2; break; } } }