-
Notifications
You must be signed in to change notification settings - Fork 7
/
cmultiknapsack.cpp
1685 lines (1477 loc) · 35.8 KB
/
cmultiknapsack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Genetic Algorithm - 0/1 Multi-Constraint Knapsack Problem (Multi-Dimensional)
-----------------------------------------------------------------------------
A genetic algorithm implementation for the multi-constraint knapsack problem.
The multi-constraint knapsack problem is a generalization of the 0/1 knapsack problem. The 0/1
knapsack problem has one weight constraint. The multi-constraint knapsack problem has m constraints
and one objective function to be maximized while all the m constraints are satisfied.
This makes it much harder to solve as compared to the 0/1 knapsack problem.
The genetic algorithm is an adaptive greedy genetic algorithm which uses the following parameters:
Selection: Random Selection.
Crossover: Adaptive crossover combination of the greedy and one point crossovers.
Mutation: The mutation rate is adaptively changed from 1/10N to 10/N in steps of
1/100N depending on the progress of the algorithm in improving upon its population.
Replacement: Generational. All offspring replace their parent individuals.
Change the parameters of the genetic algorithm by changing the Globals.
Author: Shalin Shah
Email: [email protected]
** Requirements **
The program requires an ORLIB or WEING formatted files passed in as arguments.
Usage: ./a.out <filename> <fileformat>
(The file format is either weing or orlib)
Example: ./a.out data.DAT weing
*/
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <time.h>
#include <string.h>
#include <iostream>
#include <cstring>
using namespace std;
/* Class used by local Improvement */
class GreedyObject
{
public:
double ratio;
int index;
public:
GreedyObject(){}
GreedyObject(double r, int in)
{
ratio = r;
index = in;
}
GreedyObject(const GreedyObject & copy)
{
this->ratio = copy.ratio;
this->index = copy.index;
}
bool operator < (const GreedyObject &right) const
{
return this->ratio > right.ratio;
}
bool operator > (const GreedyObject &right) const
{
return this->ratio < right.ratio;
}
bool operator == (const GreedyObject &right) const
{
return this->index == right.index;
}
bool operator != (const GreedyObject &right) const
{
return this->index != right.index;
}
GreedyObject & operator = (const GreedyObject ©)
{
this->ratio = copy.ratio;
this->index = copy.index;
return *this;
}
};
/* Globals */
int NUMBER_OBJECTS; // populated automatically by processDataORLIB
int NUMBER_CONSTRAINTS; // populated automatically by processDataORLIB
int OPTIMUM; // populated automatically by processDataORLIB
int * CAPACITIES; // populated automatically by processDataORLIB
int ** CONSTRAINTS; // populated automatically by processDataORLIB
int * VALUES; // populated automatically by processDataORLIB
const int POPULATION = 10; // size of the population
const int LOCAL_IMPROVEMENT = 10; // number of local improvements
const double INITIAL_POPULATION_PROB = 0.9; // the probability with which the initial population is generated
const int GENERATIONS = 100; // number of generations to run the algorithm
const int GREEDY_CROSSOVER = 1; // a constant to identify greedy crossover in adaptive crossover
const int ONE_POINT_CROSSOVER = 0; // a constant to identify one point crossover in adaptive crossover
const int NEGATIVE_FITNESS = -100; // the fitness of an invalid individual (violating constraints)
const int SHUFFLE_TOLERANCE = 100000; // the number of attempts to identify unique parents before shuffling
double MUTATION_PROBABILITY; // populated in main()
double MUTATION_INCREMENT; // populated in main()
double SHUFFLE_PROBABILITY; // populated in main()
double MAX_MUTATION_PROBABILITY; // populated in main()
int runtimeCrossoverType; // populated by adaptive crossover to identify which crossover operation was performed
const int MAX_UNIQUE_ITERATIONS = 10; // maximum iterations to spend on finding objects in local improvement
/* A list of objects sorted in non-increasing order of the lagrangian psuedo-utility ratio.
Used by localImprovement() */
vector<GreedyObject> GREEDY_OBJECTS;
/* Lagrangian Relaxation Paremeters */
const double INITIAL_LAMBDA = 0; // the initial value of lambdas
const double INITIAL_INCREMENT = 0.01; // the value of the increment with which the lambdas are increased or decreased
const double LAMBDA_TOLERANCE = 0.00000001; // the value of the increment at which the calculation terminates
const int COUNT_TOLERANCE = 100; // termination criteria constant for the inner loop of calculateLagrangianMultipliers
const int DIFF_TOLERANCE = 2; // termination criteria constant for the inner loop of calculateLagrangianMultipliers
double * LAMBDAS; // the lagrangian multipliers - populated by calculateLagrangianMultipliers
double * SOLUTION; // the lagrangian dual solution - used by calculateLagrangianMultipliers
/* Generate a random number in [min, max] */
int generateRandomNumber(int min, int max)
{
int r;
double randd = ((double)rand() / ((double)(RAND_MAX)+(double)(1)));
r = (int) (randd * (double)(max-min+1)) + min;
return r;
}
/* Generate a random number in [0, 1) */
double generateDoubleRandomNumber()
{
return ((double)rand() / ((double)(RAND_MAX)+(double)(1)));
}
/* Return a random crossover type */
int randomCrossover()
{
int cc = (int)((double)generateDoubleRandomNumber() * (double)2);
return cc;
}
/* Calculate the weights of this knapsack that is passed in */
int * calculateWeights(int * knapsack)
{
int * weights = new int[NUMBER_CONSTRAINTS];
for(int i=0; i<NUMBER_CONSTRAINTS; i++)
{
weights[i] = 0;
for(int j=0; j<NUMBER_OBJECTS; j++)
{
if(knapsack[j] == 1)
{
weights[i]+=CONSTRAINTS[i][j];
}
}
}
return weights;
}
/* Calculate the value of the currently calculated Lagrangian Solution */
double calculateValue()
{
double value = 0;
for(int i=0; i<NUMBER_CONSTRAINTS; i++)
{
double ll = LAMBDAS[i] * (double)CAPACITIES[i];
value+=ll;
}
for(int i=0; i<NUMBER_OBJECTS; i++)
{
if(SOLUTION[i] > 0)
{
value+=SOLUTION[i];
}
}
return value;
}
/* Calculate the lagrangian dual solution */
int * calculateSolution()
{
double * solution = new double[NUMBER_OBJECTS];
int * knapsack = new int[NUMBER_OBJECTS];
for(int i=0; i<NUMBER_OBJECTS; i++)
{
knapsack[i] = 0;
}
for(int i=0; i<NUMBER_OBJECTS; i++)
{
double w = 0;
for(int j=0; j<NUMBER_CONSTRAINTS; j++)
{
double ww = (double)CONSTRAINTS[j][i];
ww*=LAMBDAS[j];
w+=ww;
}
double v = (double)VALUES[i] - w;
solution[i] = v;
}
if(SOLUTION != NULL)
delete(SOLUTION);
SOLUTION = solution;
for(int i=0; i<NUMBER_OBJECTS; i++)
{
if(solution[i] < 0)
{
knapsack[i] = 0;
}
else
{
knapsack[i] = 1;
}
}
return knapsack;
}
/* Initialize lagrangian multipliers */
void initializeLagrangianMultipliers()
{
if(LAMBDAS != NULL)
delete [] LAMBDAS;
if(SOLUTION != NULL)
delete [] SOLUTION;
LAMBDAS = new double[NUMBER_CONSTRAINTS];
SOLUTION = new double[NUMBER_OBJECTS];
for(int i=0; i<NUMBER_CONSTRAINTS; i++)
{
LAMBDAS[i] = 0;
}
for(int i=0; i<NUMBER_OBJECTS; i++)
{
SOLUTION[i] = 0;
}
}
/* Calculate the lagrangian multipliers */
void calculateLagrangianMultipliers()
{
double increment = INITIAL_INCREMENT;
double tolerance = LAMBDA_TOLERANCE;
LAMBDAS = NULL;
SOLUTION = NULL;
initializeLagrangianMultipliers();
while(true)
{
int count = 0;
double prevValue = -1;
while(true)
{
int * solution = calculateSolution();
int * weights = calculateWeights(solution);
bool flag = true;
double value = calculateValue();
if(prevValue == -1)
{
prevValue = value;
}
else
{
double diff = prevValue - value;
if(diff < 0)
{
diff*=-1;
}
if(diff < DIFF_TOLERANCE)
{
count++;
if(count >= COUNT_TOLERANCE)
{
count = 0;
break;
}
}
else
{
count = 0;
}
prevValue = value;
}
flag = true;
for(int i=0; i<NUMBER_CONSTRAINTS; i++)
{
if(weights[i] < CAPACITIES[i])
{
if(LAMBDAS[i] > 0)
{
LAMBDAS[i]-=increment;
if(LAMBDAS[i] < 0)
{
LAMBDAS[i] = 0;
}
}
}
else if(weights[i] > CAPACITIES[i])
{
flag = false;
LAMBDAS[i]+=increment;
}
}
if(flag)
{
break;
}
}
if(increment <= tolerance)
{
break;
}
increment/=2;
}
}
/* A genome in the genetic algorithm */
class KNode
{
private:
int * knapsack;
int value;
int * weights;
public:
int crossoverType;
public:
/* Constructor */
KNode()
{
knapsack = NULL;
weights = NULL;
}
/* Constructor */
KNode(int * knap)
{
/* Make a deep copy of the passed in array */
knapsack = new int [NUMBER_OBJECTS];
for(int i=0; i<NUMBER_OBJECTS; i++)
{
if(knap[i] == 1)
{
knapsack[i] = 1;
}
else if(knap[i] == 0)
{
knapsack[i] = 0;
}
else
{
printf("Invalid Knapsack passed to constructor!");
printf("i = %d, value = %d", i, knap[i]);
exit(1);
}
}
calculateWeights();
calculateValue();
crossoverType = randomCrossover();
}
/* Copy Constructor */
KNode(const KNode & copy)
{
int * knap = copy.knapsack;
knapsack = new int [NUMBER_OBJECTS];
for(int i=0; i<NUMBER_OBJECTS; i++)
{
if(knap[i] == 1)
{
knapsack[i] = 1;
}
else if(knap[i] == 0)
{
knapsack[i] = 0;
}
else
{
printf("Invalid Knapsack passed to copy constructor!");
exit(1);
}
}
crossoverType = copy.crossoverType;
calculateWeights();
calculateValue();
}
/* Destructor */
~KNode()
{
//printf("Inside Destructor");
if(knapsack != NULL)
delete [] knapsack;
if(weights != NULL)
delete [] weights;
knapsack = NULL;
weights = NULL;
}
/* Assignment Operator */
KNode & operator = (const KNode & copy)
{
if(knapsack != NULL)
delete knapsack;
if(weights != NULL)
delete weights;
//printf("Inside operator =\n");
int * knap = copy.knapsack;
knapsack = new int [NUMBER_OBJECTS];
for(int i=0; i<NUMBER_OBJECTS; i++)
{
if(knap[i] == 1)
{
knapsack[i] = 1;
}
else if(knap[i] == 0)
{
knapsack[i] = 0;
}
else
{
printf("Invalid Knapsack passed to copy constructor!");
exit(1);
}
}
crossoverType = copy.crossoverType;
calculateWeights();
calculateValue();
return *this;
}
bool operator < (const KNode &right) const
{
int af = this->nodeFitness();
int bf = right.nodeFitness();
return af > bf;
}
bool operator > (const KNode & right) const
{
int af = this->nodeFitness();
int bf = right.nodeFitness();
return af < bf;
}
bool operator == (const KNode & right) const
{
int af = this->nodeFitness();
int bf = right.nodeFitness();
return af == bf;
}
bool operator != (const KNode & right) const
{
int af = this->nodeValue();
int bf = right.nodeValue();
return af != bf;
}
/* Verify that the object of this class is a valid knapsack */
void checkKNode() const
{
for(int i=0; i<NUMBER_OBJECTS; i++)
{
if(knapsack[i] == 1 || knapsack[i] == 0)
{
}
else
{
// Should Never Happen
printf("Bad Knapsack");
exit(1);
}
}
}
/* Calculate the weights of this knapsack object */
void calculateWeights()
{
weights = new int[NUMBER_CONSTRAINTS];
for(int i=0; i<NUMBER_CONSTRAINTS; i++)
{
weights[i] = 0;
for(int j=0; j<NUMBER_OBJECTS; j++)
{
if(knapsack[j] == 1)
{
weights[i]+=CONSTRAINTS[i][j];
}
}
}
}
/* Calculate the value of this knapsack object */
void calculateValue()
{
value = 0;
for(int i=0; i<NUMBER_OBJECTS; i++)
{
if(knapsack[i] == 1)
{
value+=VALUES[i];
}
}
}
/* Does this knapsack object violate any of the constraints? */
bool violatesConstraints(void) const
{
for(int i=0; i<NUMBER_CONSTRAINTS; i++)
{
if(this->weights[i] > CAPACITIES[i])
{
return true;
}
}
return false;
}
/* The fitness of this object - This is different from the value. If the
object violates any of the constraints, a negative value is returned */
int nodeFitness() const
{
if(violatesConstraints())
{
return NEGATIVE_FITNESS;
}
return nodeValue();
}
/* Return the value of this knapsack object */
int nodeValue() const
{
return value;
}
/* Replace the value at the passed in index to 1 */
void addOne(int index)
{
if(knapsack[index]==1)
{
return;
}
value+=VALUES[index];
for(int i=0; i<NUMBER_CONSTRAINTS; i++)
{
weights[i]+=CONSTRAINTS[i][index];
}
knapsack[index] = 1;
}
/* Replace the value at the passed in index to 0 */
void resetBit(int index)
{
if(knapsack[index] == 0)
{
return;
}
value-=VALUES[index];
for(int i=0; i<NUMBER_CONSTRAINTS; i++)
{
weights[i]-=CONSTRAINTS[i][index];
}
knapsack[index] = 0;
}
/* Set the value at the passed in index to 0 */
void addZero(int index)
{
knapsack[index] = 0;
}
/* Set the value represented by this knapsack */
void setValue(int v)
{
value = v;
}
/* Set the weights of this knapsack object */
void setWeights(int * w)
{
weights = w;
}
/* Set the array that represents the chosen and not chosen objects in order */
void setKnapsack(int * knap)
{
knapsack = knap;
}
/* Is the object represented by the index chosen? If yes, return 1. If no, return 0 */
int getValueOfIndex(int index)
{
return knapsack[index];
}
/* Return the array that represents the chosen and not chosen objects in order */
int * getKnapsack()
{
return knapsack;
}
/* Return a clone of this object - All member variables are deep copied */
KNode clone()
{
int * kk = new int[NUMBER_OBJECTS];
int ct = crossoverType;
int vv = value;
for(int i=0; i<NUMBER_OBJECTS; i++)
{
kk[i] = knapsack[i];
}
KNode clone(kk);
delete [] kk;
clone.crossoverType = ct;
return clone;
}
};
/* Generate a random initial population */
vector<KNode> generateRandomPopulation()
{
vector<KNode> population;
for(int i=0; i<POPULATION; i++)
{
int * knapsack = new int[NUMBER_OBJECTS];
for(int j=0; j<NUMBER_OBJECTS; j++)
{
double rand = generateDoubleRandomNumber();
if(rand < INITIAL_POPULATION_PROB)
{
knapsack[j] = 1;
}
else
{
knapsack[j] = 0;
}
}
KNode node(knapsack);
delete [] knapsack;
population.push_back(node);
}
return population;
}
/* Process the data from the ORLIB file */
void processDataORLIB(char * filename)
{
FILE * file;
file = fopen(filename, "r");
if(file == NULL)
{
printf("orlib1.txt File Not Found in Current Directory.");
exit(1);
}
char * line = new char [1000];
fgets(line, 1000, file);
char * tok = strtok(line, " ");
NUMBER_OBJECTS = atoi(tok);
tok = strtok(NULL, " ");
NUMBER_CONSTRAINTS = atoi(tok);
tok = strtok(NULL, " ");
OPTIMUM = atoi(tok);
//printf("%d\n", NUMBER_OBJECTS);
//printf("%d\n", NUMBER_CONSTRAINTS);
//printf("%d\n", OPTIMUM);
int i = 0;
/* VALUES (objective function) */
VALUES = new int[NUMBER_OBJECTS];
fgets(line, 1000, file);
tok = strtok(line, " ");
while(true)
{
while(tok != NULL && i < NUMBER_OBJECTS)
{
int vv = atoi(tok);
VALUES[i] = vv;
i++;
tok = strtok(NULL, " ");
if(tok == NULL)
{
break;
}
if(strcmp(tok, " ") == 0 || strcmp(tok, "\n") == 0)
{
break;
}
}
if(i < NUMBER_OBJECTS)
{
fgets(line, 1000, file);
tok = strtok(line, " ");
int vv = atoi(tok);
VALUES[i] = vv;
tok = strtok(NULL, " ");
i++;
}
else
{
break;
}
}
/* CONSTRAINTS */
CONSTRAINTS = new int*[NUMBER_CONSTRAINTS];
for(int n=0; n<NUMBER_CONSTRAINTS; n++)
{
i=0;
fgets(line, 1000, file);
CONSTRAINTS[n] = new int[NUMBER_OBJECTS];
tok = strtok(line, " ");
int vv = atoi(tok);
CONSTRAINTS[n][i] = vv;
i++;
while(true)
{
tok = strtok(NULL, " ");
while(tok != NULL && i < NUMBER_OBJECTS)
{
vv = atoi(tok);
CONSTRAINTS[n][i] = vv;
tok = strtok(NULL, " ");
i++;
if(tok == NULL)
{
break;
}
if(strcmp(tok, " ") == 0 || strcmp(tok, "\n") == 0)
{
break;
}
}
if(i < NUMBER_OBJECTS)
{
fgets(line, 1000, file);
tok = strtok(line, " ");
int vv = atoi(tok);
CONSTRAINTS[n][i] = vv;
i++;
}
else
{
break;
}
}
}
/* CAPACITIES */
CAPACITIES = new int[NUMBER_CONSTRAINTS];
i=0;
fgets(line, 1000, file);
tok = strtok(line, " ");
while(true)
{
while(tok != NULL && i < NUMBER_CONSTRAINTS)
{
int vv = atoi(tok);
CAPACITIES[i]= vv;
i++;
tok = strtok(NULL, " ");
if(tok == NULL)
{
break;
}
if(strcmp(tok, " ") == 0 || strcmp(tok, "\n") == 0)
{
break;
}
}
if(i < NUMBER_CONSTRAINTS)
{
fgets(line, 1000, file);
tok = strtok(line, " ");
int vv = atoi(tok);
CAPACITIES[i] = vv;
i++;
tok = strtok(NULL, " ");
}
else
{
break;
}
}
delete[](line);
}
/* Process the data from the ORLIB file */
void processDataWEING(char * filename)
{
FILE * file;
file = fopen(filename, "r");
if(file == NULL)
{
printf("Data File Not Found in Current Directory.");
exit(1);
}
char * line = new char [1000];
fgets(line, 1000, file);
char * tok = strtok(line, " \t");
NUMBER_CONSTRAINTS = atoi(tok);
tok = strtok(NULL, " \t");
NUMBER_OBJECTS = atoi(tok);
int i = 0;
/* VALUES (objective function) */
VALUES = new int[NUMBER_OBJECTS];
fgets(line, 1000, file);
tok = strtok(line, " \t");
while(true)
{
while(tok != NULL && i < NUMBER_OBJECTS)
{
int vv = atoi(tok);
VALUES[i] = vv;
i++;
tok = strtok(NULL, " \t");
if(tok == NULL)
{
break;
}
if(strcmp(tok, " ") == 0 || strcmp(tok, "") == 0)
{
continue;
}
if(strcmp(tok, "\n") == 0)
{
break;
}
}
if(i < NUMBER_OBJECTS)
{
fgets(line, 1000, file);
tok = strtok(line, " \t");
int vv = atoi(tok);
VALUES[i] = vv;
tok = strtok(NULL, " \t");
i++;
}
else
{
break;
}
}
/* CAPACITIES */
CAPACITIES = new int[NUMBER_CONSTRAINTS];
i=0;
fgets(line, 1000, file);
tok = strtok(line, " \t");
while(true)
{
while(tok != NULL && i < NUMBER_CONSTRAINTS)
{
int vv = atoi(tok);
CAPACITIES[i]= vv;
i++;
tok = strtok(NULL, " \t");
if(tok == NULL)
{
break;
}
if(strcmp(tok, " ") == 0 || strcmp(tok, "") == 0)
{
continue;
}
if(strcmp(tok, "\n") == 0)
{
break;
}
}
if(i < NUMBER_CONSTRAINTS)
{
fgets(line, 1000, file);
tok = strtok(line, " \t");
int vv = atoi(tok);
CAPACITIES[i] = vv;
i++;
tok = strtok(NULL, " \t");
}
else
{
break;
}
}
/* CONSTRAINTS */
CONSTRAINTS = new int*[NUMBER_CONSTRAINTS];
for(int n=0; n<NUMBER_CONSTRAINTS; n++)
{
i=0;
fgets(line, 1000, file);
CONSTRAINTS[n] = new int[NUMBER_OBJECTS];
tok = strtok(line, " \t");
int vv = atoi(tok);
CONSTRAINTS[n][i] = vv;
i++;
while(true)
{
tok = strtok(NULL, " \t");
while(tok != NULL && i < NUMBER_OBJECTS)
{
vv = atoi(tok);
CONSTRAINTS[n][i] = vv;
tok = strtok(NULL, " \t");
i++;
if(tok == NULL)
{
break;
}
if(strcmp(tok, " ") == 0 || strcmp(tok, "") == 0)
{
continue;
}
if(strcmp(tok, "\n") == 0)
{
break;
}
}
if(i < NUMBER_OBJECTS)
{
fgets(line, 1000, file);
tok = strtok(line, " \t");
int vv = atoi(tok);
CONSTRAINTS[n][i] = vv;
i++;
}
else
{
break;
}
}
}
fgets(line, 1000, file);
tok = strtok(line, " \t");
while(strcmp(tok, "") == 0 || strcmp(tok, " ") == 0 || strcmp(tok, "\n") == 0)
{
fgets(line, 1000, file);
tok = strtok(line, " \t");
}
OPTIMUM = atoi(tok);
delete[](line);
}
/* Mutation */
inline void mutate(KNode &node, double prob)