Browse files

long shifts healing mutators

  • Loading branch information...
1 parent 85ea360 commit 359d089ca6bf92281c099cc338014153bf4c7ec4 @txemagon committed Feb 3, 2012
View
BIN bin/plan-shifts
Binary file not shown.
View
14 etc/shift-planning.yml
@@ -12,19 +12,19 @@ work_load: 375 # In tenths of houur per week.
shift_length: 70 # Daily tenths of hour.
# Penalty points
-shift_week_extra_penalty: 2 # Punish working a lot of days together.
+shift_week_extra_penalty: 3 # Punish working a lot of days together.
shift_week_defect_penalty: 1 # Punish interleaving holidays and work
halving_weekend_penalty: 7 # Punish not giving the whole weekend
-weekend_injustice_penalty: 2 # Punish different number of weekends
-consecutive_weekend_injustice: 2 # Punish working one weekend after another
+weekend_injustice_penalty: 15 # Punish different number of weekends
+consecutive_weekend_injustice: 0 # Punish working one weekend after another
bad_free_days_penalty: 1 # Ensure the same amount of free days
few_people_penalty: 3 # Ensure the correct crew
extra_people_penalty: 4 # Ensure the correct crew
-work_load_penalty: 4 # Go to work_load
+work_load_penalty: 1 # Go to work_load
# Inner Working
generations: 200000 # Number of generations
-population: 400 # Population in each generation
-mutation_length: 2 # Verb. Number of maximum mutators applied together
-cross_rate: 25 # Probability of crossing part of a chromosome in a copy
+population: 90 # Population in each generation
+mutation_length: 1 # Verb. Number of maximum mutators applied together
+cross_rate: 100 # Probability of crossing part of a chromosome in a copy
# P = 1 / cross_rate
View
94 src/allele.cpp
@@ -15,6 +15,7 @@
*
* =====================================================================================
*/
+#include <stdio.h>
#include <stdlib.h>
#include <vector>
@@ -135,10 +136,25 @@ vector < TimeInterval > &get_bad_shifts (Chromosome chromo, unsigned worker,
interval = false;
}
-
return list;
} /* ----- end of function get_bad_shifts ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: get_free_working_days
+ * Description: Fills a list with the weekends of a given worker.
+ * =====================================================================================
+ */
+ vector<unsigned> &
+get_free_working_days ( Chromosome chromo, unsigned worker, vector<unsigned> &list )
+{
+ for (unsigned day=0; day<chromo.length; day++)
+ if (!is_working(chromo.gene[day], worker) && !is_weekend(day))
+ list.push_back(day);
+ return list;
+} /* ----- end of function get_free_working_days ----- */
+
/*
* === FUNCTION ======================================================================
* Name: is_working_the_weekend
@@ -235,18 +251,7 @@ mutation_gene (Chromosome chromo)
chromo.gene[position] =
random_gene (chromo.width, goals[staff_number_ix].value);
- /*
- if (position % WEEK == SATURDAY + 1)
- position--;
- if (position % WEEK == SATURDAY)
- {
- unsigned random_shift = random_gene (chromo.width, SNW);
- chromo.gene[position] = random_shift;
- chromo.gene[position + 1] = random_shift;
- }
- else
- chromo.gene[position] = random_gene (chromo.width, SN);
- */
+
} /* ----- end of function mutation_gene ----- */
@@ -346,3 +351,66 @@ bubble_gene (Chromosome chromo)
chromo.gene[pos1] = chromo.gene[pos2];
chromo.gene[pos2] = buffer;
} /* ----- end of function bubble ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: lower_shift_stress
+ * Description: Use one of the free working days to lower long shift penalty points.
+ * =====================================================================================
+ */
+ void
+lower_shift_stress ( Chromosome chromo )
+{
+ vector<unsigned> list;
+ vector<TimeInterval> interval;
+ unsigned insize;
+ unsigned freesize;
+ unsigned bad_interval;
+ unsigned freeday;
+ unsigned aux;
+ unsigned init, end, mid;
+ unsigned w= rand() % chromo.width;
+
+ get_bad_shifts (chromo, w, interval);
+ insize = interval.size();
+ if (insize){
+ get_free_working_days(chromo, w, list);
+ freesize = list.size();
+ if (freesize){
+ bad_interval = rand() % insize;
+ init = interval[bad_interval].day_start;
+ end = interval[bad_interval].day_end;
+ do{
+ mid = init + rand() % (1 + end - init );
+ }while(mid % WEEK >= SATURDAY);
+ freeday = rand() % freesize;
+ aux = chromo.gene[list[freeday]];
+ chromo.gene[list[freeday]] = chromo.gene[mid];
+ chromo.gene[mid] = aux;
+ }
+ }
+} /* ----- end of function lower_shift_stress ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: exch_weekend
+ * Description: Exchanges two weekends
+ * =====================================================================================
+ */
+ void
+exch_weekend ( Chromosome chromo )
+{
+ unsigned weekend1, weekend2;
+ unsigned aux;
+
+ weekend1 = rand() % chromo.length / WEEK;
+ do{
+ weekend2 = rand() % chromo.length / WEEK;
+ }while(weekend2 == weekend1);
+
+ for (unsigned off=0; off<2; off++){
+ aux = chromo.gene[weekend1*WEEK+off];
+ chromo.gene[weekend1*WEEK+off] = chromo.gene[weekend2*WEEK+off];
+ chromo.gene[weekend2*WEEK+off] = aux;
+ }
+} /* ----- end of function exch_weekend ----- */
View
3 src/allele.h
@@ -37,5 +37,8 @@ unsigned get_shift_base (Chromosome chromo);
unsigned get_consweekend_base (Chromosome chromo);
vector < TimeInterval > &get_bad_shifts (Chromosome chromo, unsigned worker,
vector < TimeInterval > &list);
+vector<unsigned> & get_free_working_days ( Chromosome chromo, unsigned worker, vector<unsigned> &list );
+void lower_shift_stress ( Chromosome chromo );
+void exch_weekend ( Chromosome chromo );
#endif /* ----- #ifndef ALLELE_H ----- */
View
47 src/chromosomal.cpp
@@ -58,10 +58,45 @@ show_chromosome (Chromosome chromosome)
free (shift);
} /* ----- end of function show_chromosome ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: hash_chromosome
+ * Description: Prints the hex chromosome
+ * =====================================================================================
+ */
+ void
+hash_chromosome ( Chromosome chromosome )
+{
+ for (unsigned day = 0; day < chromosome.length; day++)
+ printf("%X", chromosome.gene[day]);
+
+ printf("\n");
+} /* ----- end of function hash_chromosome ----- */
+/*
+ * === FUNCTION ======================================================================
+ * Name: display_working_free_days
+ * Description: Shows those free days outside the weekend
+ * =====================================================================================
+ */
+void
+display_working_free_days (Chromosome chromo, unsigned worker)
+{
+ vector < unsigned > list;
+ get_free_working_days (chromo, worker, list );
+ vector < unsigned >::const_iterator fd;
+
+ printf("Free days outside weekend: ");
+ for (fd = list.begin (); fd != list.end (); fd++)
+ printf ("%2u ", *fd);
+ printf ("\n");
+} /* ----- end of function display_working_free_days ----- */
+
+
/*
* === FUNCTION ======================================================================
* Name: display_long_shifts
- * Description: Looks for long shifts
+ * Description: Show long shifts
* =====================================================================================
*/
void
@@ -70,6 +105,8 @@ display_long_shifts (Chromosome chromo, unsigned worker)
vector < TimeInterval > &list =
get_bad_shifts (chromo, worker, chromo.summary.wrong_shifts[worker]);
vector < TimeInterval >::const_iterator shift;
+
+ printf("Long Shifts: ");
for (shift = list.begin (); shift != list.end (); shift++)
printf ("[%2u, %2u] ", shift->day_start, shift->day_end);
printf ("\n");
@@ -103,7 +140,9 @@ display_summaries (Chromosome chromo)
chromo.summary.weekends_halved[worker]);
printf ("Free weekends: %u\n", chromo.summary.weekends[worker]);
printf ("Free days: %u\n", chromo.summary.freedays[worker]);
+ printf ("Working Ratio: %.2lf\n", 1. * (chromo.length - chromo.summary.freedays[worker]) * problem[shift_length_idx]. value / goals[work_load_idx].value * WEEK / chromo.length );
display_long_shifts (chromo, worker);
+ display_working_free_days (chromo, worker);
}
} /* ----- end of function display_summaries() ----- */
@@ -127,7 +166,7 @@ display_penalties (Chromosome chromo)
printf ("Different free days: %u\n", chromo.penalty.different_free_days);
printf ("Long Shifts: %u\n", chromo.penalty.long_shifts);
printf ("Weekends Halved: %u\n", chromo.penalty.weekends_halved);
- printf ("Consecutive Weekends: %u\n", chromo.penalty.consecutive_weekends);
+ // printf ("Consecutive Weekends: %u\n", chromo.penalty.consecutive_weekends);
} /* ----- end of function display_penalties ----- */
@@ -343,10 +382,12 @@ cross (Chromosome chromo1, Chromosome chromo2)
start2 = rand () % chromo2.length / WEEK;
start1 *= WEEK;
start2 *= WEEK;
+ /*
length =
rand () % (unsigned) fmin (chromo1.length - start1,
chromo2.length - start2);
- length = WEEK;
+ */
+ length = WEEK - 2;
buffer = (unsigned *) malloc (length * sizeof (unsigned));
memcpy (buffer, &(chromo1.gene[start1]), length);
View
1 src/chromosomal.h
@@ -29,5 +29,6 @@ void fix_staff (Population population);
void cross (Chromosome chromo1, Chromosome chromo2);
void display_penalties (Chromosome chromo);
void display_summaries (Chromosome chromo);
+void hash_chromosome ( Chromosome chromosome );
#endif
View
1 src/main.cpp
@@ -245,6 +245,7 @@ main (int argc, char *argv[])
mutate_generation (population));
analyze_aptitude (&(population.person[0]));
+ hash_chromosome(population.person[0]);
show_chromosome (population.person[0]);
display_summaries (population.person[0]);
display_penalties (population.person[0]);
View
12 src/manage_time.cpp
@@ -49,3 +49,15 @@ the_next_wekend_as_of (unsigned day, unsigned max)
day = -1;
return day;
} /* ----- end of function the_next_wekend_as_of ----- */
+
+/*
+ * === FUNCTION ======================================================================
+ * Name: is_weekend
+ * Description: Returns true if the given day is a weekend
+ * =====================================================================================
+ */
+bool
+is_weekend ( unsigned day )
+{
+ return day % WEEK >= SATURDAY;
+} /* ----- end of function is_weekend ----- */
View
1 src/manage_time.h
@@ -35,5 +35,6 @@ extern enum WeekDay first_day_of_week;
int get_day (int day_of_week);
int the_next_wekend_as_of (unsigned day, unsigned max);
+bool is_weekend ( unsigned day );
#endif /* ----- #ifndef MANAGE_TIME_H ----- */
View
2 src/mutation_controller.cpp
@@ -31,7 +31,7 @@
void (*mutator[]) (Chromosome chromo) =
{ // random_wknd_gene,
NULL,
- mutation_gene, random_rotate_gene, random_shift, interchain, bubble_gene};
+ mutation_gene, random_rotate_gene, random_shift, interchain, bubble_gene, lower_shift_stress, exch_weekend };
unsigned mutator_elements = sizeof (mutator) / sizeof (void (*)(Chromosome));
View
1 src/mutators.cpp
@@ -73,6 +73,7 @@ mutator_exec (unsigned verb, Chromosome chromo)
unsigned
random_verb ()
{
+ static unsigned long long times = 0;
int nb_of_mutators = mutator_elements;
int base = nb_of_mutators;
int max = power (base, inner_working[mutation_length_idx].value);
View
8 src/screen.cpp
@@ -377,7 +377,7 @@ analyze_aptitude (Chromosome * chromo)
for (int w = 0; w < chromo->width; w++)
{
chromo->summary.extra_shifts[w] = evaluate_extra_shifts (*chromo, w);
- // chromo->summary.weekends_halved[w] = evaluate_weekends_halved (*chromo, w);
+ chromo->summary.weekends_halved[w] = evaluate_weekends_halved (*chromo, w);
// chromo->summary.consecutive_weekends[w] = evaluate_consecutive_weekends (*chromo, w);
chromo->summary.weekends[w] = get_total_weekends (*chromo, w);
chromo->summary.freedays[w] = get_total_freedays (*chromo, w);
@@ -409,23 +409,21 @@ check_aptitude (Population population)
population.person[i].penalty_sum +=
population.person[i].penalty.bad_staff_number =
punish_bad_staff_number (population.person[i]);
-/*
+
population.person[i].penalty_sum +=
population.person[i].penalty.weekends_halved =
punish_halving_weekends (population.person[i]);
+ /*
population.person[i].penalty_sum +=
population.person[i].penalty.consecutive_weekends =
punish_consecutive_weekends (population.person[i]);
-
*/
-
population.person[i].penalty_sum +=
population.person[i].penalty.different_weekend_number =
punish_different_weekend_number (population.person[i]);
-
population.person[i].penalty_sum +=
population.person[i].penalty.work_load =
punish_work_load (population.person[i]);
View
64 src/tags
@@ -4,51 +4,75 @@
!_TAG_PROGRAM_NAME Exuberant Ctags //
!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
!_TAG_PROGRAM_VERSION 5.9~svn20110310 //
-CONS_WE_PENAL chromosomal.cpp 64;" d file:
-FREEDAY_PENAL chromosomal.cpp 66;" d file:
-INDIV_PENAL chromosomal.cpp 61;" d file:
-LONG_SH_PENAL chromosomal.cpp 62;" d file:
-WEEKEND_PENAL chromosomal.cpp 65;" d file:
-WEEK_HA_PENAL chromosomal.cpp 63;" d file:
+MAX_TRANSLATION chromosomal.cpp 34;" d file:
analyze_aptitude screen.cpp /^analyze_aptitude (Chromosome * chromo)$/;" f
-bubble_gene chromosomal.cpp /^bubble_gene ( Chromosome chromo )$/;" f
+bubble_gene allele.cpp /^bubble_gene (Chromosome chromo)$/;" f
check_aptitude screen.cpp /^check_aptitude (Population population)$/;" f
-clear_penalties screen.cpp /^clear_penalties (Population population)$/;" f
+clear_summary screen.cpp /^clear_summary (Population population)$/;" f
copy chromosomal.cpp /^copy (Population population, unsigned dest, unsigned src)$/;" f
create_initial_population chromosomal.cpp /^create_initial_population (unsigned workers, unsigned period,$/;" f
cross chromosomal.cpp /^cross (Chromosome chromo1, Chromosome chromo2)$/;" f
deallocate_pop chromosomal.cpp /^deallocate_pop (Population population)$/;" f
+display_amounts gene.cpp /^display_amounts (char *buffer, unsigned value, unsigned base)$/;" f
+display_long_shifts chromosomal.cpp /^display_long_shifts (Chromosome chromo, unsigned worker)$/;" f
display_penalties chromosomal.cpp /^display_penalties (Chromosome chromo)$/;" f
+display_summaries chromosomal.cpp /^display_summaries (Chromosome chromo)$/;" f
+display_working_free_days chromosomal.cpp /^display_working_free_days (Chromosome chromo, unsigned worker)$/;" f
+evaluate_consecutive_weekends screen.cpp /^evaluate_consecutive_weekends (Chromosome chromo, unsigned worker)$/;" f
+evaluate_defect_staff_number screen.cpp /^evaluate_defect_staff_number (Chromosome chromo)$/;" f
+evaluate_extra_shifts screen.cpp /^evaluate_extra_shifts (Chromosome chromo, unsigned worker)$/;" f
+evaluate_extra_staff_number screen.cpp /^evaluate_extra_staff_number (Chromosome chromo)$/;" f
+evaluate_weekends_halved screen.cpp /^evaluate_weekends_halved (Chromosome chromo, unsigned worker)$/;" f
ex_program main.cpp /^ex_program (int sig)$/;" f
exit_request main.cpp /^bool exit_request = false;$/;" v
first_day_of_week manage_time.cpp /^enum WeekDay first_day_of_week = monday; \/* Default value *\/$/;" v typeref:enum:WeekDay
fix_staff chromosomal.cpp /^fix_staff (Population population)$/;" f
+get_bad_shifts allele.cpp /^vector < TimeInterval > &get_bad_shifts (Chromosome chromo, unsigned worker,$/;" f
+get_consweekend_base allele.cpp /^get_consweekend_base (Chromosome chromo)$/;" f
get_day manage_time.cpp /^get_day (int day_of_week)$/;" f
-get_first_free_wknd_since allele.cpp /^get_first_free_wknd_since ( Chromosome chromo, unsigned worker, unsigned since_day )$/;" f
+get_first_free_wknd_since allele.cpp /^get_first_free_wknd_since (Chromosome chromo, unsigned worker,$/;" f
+get_free_working_days allele.cpp /^get_free_working_days ( Chromosome chromo, unsigned worker, vector<unsigned> &list )$/;" f
+get_one_normal_day allele.cpp /^get_one_normal_day (unsigned length)$/;" f
+get_shift_base allele.cpp /^get_shift_base (Chromosome chromo)$/;" f
get_total_freedays allele.cpp /^get_total_freedays (Chromosome chromo, unsigned worker)$/;" f
get_total_weekends allele.cpp /^get_total_weekends (Chromosome chromo, unsigned worker)$/;" f
+goals main.cpp /^ConfigIntVar goals[] = {$/;" v
init_chromosome chromosomal.cpp /^init_chromosome (unsigned workers, unsigned period)$/;" f
-interchain chromosomal.cpp /^interchain (Chromosome chromo)$/;" f
+init_weekends chromosomal.cpp /^init_weekends (Chromosome chromo)$/;" f
+inner_working main.cpp /^ConfigIntVar inner_working[] = {$/;" v
+inspect_gene gene.cpp /^inspect_gene (char *output, unsigned gene, unsigned width)$/;" f
+interchain allele.cpp /^interchain (Chromosome chromo)$/;" f
+is_weekend manage_time.cpp /^is_weekend ( unsigned day )$/;" f
is_working gene.cpp /^is_working (unsigned gene, unsigned worker)$/;" f
is_working_the_weekend allele.cpp /^is_working_the_weekend (Chromosome chromo, unsigned worker, unsigned week)$/;" f
+load_config_file main.cpp /^load_config_file ()$/;" f
+lower_shift_stress allele.cpp /^lower_shift_stress ( Chromosome chromo )$/;" f
main main.cpp /^main (int argc, char *argv[])$/;" f
-mutate_chromo mutation_controller.cpp /^mutate_chromo ( Chromosome chromo )$/;" f
-mutate_generation mutation_controller.cpp /^mutate_generation ( Population population )$/;" f
-mutation_gene allele.cpp /^mutation_gene (Chromosome chromo, unsigned position)$/;" f
+mutate_chromo mutation_controller.cpp /^mutate_chromo (Chromosome * chromo)$/;" f
+mutate_generation mutation_controller.cpp /^mutate_generation (Population population)$/;" f
+mutation_gene allele.cpp /^mutation_gene (Chromosome chromo)$/;" f
+mutator mutation_controller.cpp /^void (*mutator[]) (Chromosome chromo) =$/;" v
+mutator_elements mutation_controller.cpp /^unsigned mutator_elements = sizeof (mutator) \/ sizeof (void (*)(Chromosome));$/;" v
+mutator_exec mutators.cpp /^mutator_exec (unsigned verb, Chromosome chromo)$/;" f
+penalty_points main.cpp /^ConfigIntVar penalty_points[] = {$/;" v
people_working gene.cpp /^people_working (unsigned gene)$/;" f
population main.cpp /^Population population;$/;" v
+power mutators.cpp /^power (int base, int exponent)$/;" f
+problem main.cpp /^ConfigIntVar problem[] = {$/;" v
punish_bad_staff_number screen.cpp /^punish_bad_staff_number (Chromosome chromo)$/;" f
-punish_consecutive_weekends screen.cpp /^punish_consecutive_weekends (Chromosome chromo, unsigned worker)$/;" f
+punish_consecutive_weekends screen.cpp /^punish_consecutive_weekends (Chromosome chromo)$/;" f
punish_different_free_days screen.cpp /^punish_different_free_days (Chromosome chromo)$/;" f
punish_different_weekend_number screen.cpp /^punish_different_weekend_number (Chromosome chromo)$/;" f
-punish_halving_weekends screen.cpp /^punish_halving_weekends (Chromosome chromo, unsigned worker)$/;" f
-punish_long_shifts screen.cpp /^punish_long_shifts (Chromosome chromo, unsigned worker)$/;" f
-punish_work_load screen.cpp /^punish_work_load (Chromosome chromo, unsigned worker)$/;" f
+punish_halving_weekends screen.cpp /^punish_halving_weekends (Chromosome chromo)$/;" f
+punish_long_shifts screen.cpp /^punish_long_shifts (Chromosome chromo)$/;" f
+punish_work_load screen.cpp /^punish_work_load (Chromosome chromo)$/;" f
random_gene gene.cpp /^random_gene (unsigned workers, unsigned ones)$/;" f
-random_rotate_gene allele.cpp /^random_rotate_gene (Chromosome chromo, unsigned position)$/;" f
-random_shift chromosomal.cpp /^random_shift (Chromosome chromo)$/;" f
-random_wknd_gene allele.cpp /^random_wknd_gene (Chromosome chromo, unsigned position )$/;" f
+random_rotate_gene allele.cpp /^random_rotate_gene (Chromosome chromo)$/;" f
+random_shift allele.cpp /^random_shift (Chromosome chromo)$/;" f
+random_verb mutators.cpp /^random_verb ()$/;" f
+random_wknd_gene allele.cpp /^random_wknd_gene (Chromosome chromo)$/;" f
rotate_gene gene.cpp /^rotate_gene (unsigned gene, int places, unsigned width)$/;" f
+set_global main.cpp /^set_global (const char *key, const char *value)$/;" f
set_worker gene.cpp /^set_worker (unsigned gene, unsigned worker, enum TWorking value)$/;" f
show_chromosome chromosomal.cpp /^show_chromosome (Chromosome chromosome)$/;" f
sort_by_penalty chromosomal.cpp /^sort_by_penalty (Population population)$/;" f

0 comments on commit 359d089

Please sign in to comment.