-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgene.h
693 lines (542 loc) · 19.5 KB
/
gene.h
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
// Declarations for shape, coord, edge_node, placement, shape_l, shape_g_l, gene
// chromosome, LDA_c, LDA_e, environment
#ifndef _gene_h
#define _gene_h
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <sys/timeb.h>
#include <strstream>
#include <math.h>
#include "define_vars.h"
#include "uulist.h"
#include "sort.h"
//#include "bitstream.h"
#include "Random Number Generator\randlib.h"
#ifndef random_done
#define random_done
#define random() (ignlgi()-1)
#define MAXRANDOM 2147483561L
#endif
void output_angle(CDumpContext& output_stream, const angle output_ang);
void str_out_angle(char *output, const int string_size, const angle output_ang);
class shape : virtual public CObject
{
DECLARE_DYNAMIC(shape);
protected:
x_size_t x_size;
y_size_t y_size;
array_index id;
bool rotation_ok;
public:
// ...default constructor
shape();
// ...parameterized constructors
shape(const x_size_t x_value, const y_size_t y_value);
// ...destructor
virtual ~shape();
// ...copy constructor
shape(const shape &source);
// ...overloaded = operator
shape& operator=(const shape &source);
// ...Overloaded comparison operators
friend int operator==(const shape &first_shape, const shape &second_shape);
// Input and output stream operators
friend std::ostream& operator<<(std::ostream& output_stream, const shape shape_to_output);
friend std::istream& operator>>(std::istream& input_stream, shape& shape_to_input);
inline y_size_t height();
inline x_size_t width();
inline array_index get_id();
inline void set_id(array_index &new_id);
bool is_rotation_ok();
inline void prevent_rotation();
#ifdef _DEBUG
virtual void Dump( CDumpContext& dc ) const;
#endif
};
class shape_group : virtual public CObject
{
DECLARE_DYNAMIC(shape_group);
public:
sequence_size length;
shape *shape_list;
xy_size_t total_area;
shape_group();
virtual ~shape_group();
shape_group(const shape_group &source);
shape_group& operator=(const shape_group &source);
shape& operator[](const array_index &i);
friend std::istream& operator>>(std::istream& input_stream, shape_group& LDA_e_to_input);
void set_rotation_validities(xy_size_t width, xy_size_t height);
#ifdef _DEBUG
virtual void Dump( CDumpContext& dc ) const;
#endif
};
class coord : virtual public CObject
{
DECLARE_DYNAMIC(coord);
public:
x_pos_t x_pos;
y_pos_t y_pos;
public:
// ...default constructor
coord();
// ...parameterized constructors
coord(const x_pos_t x_value, const y_pos_t y_value);
// ...destructor
virtual ~coord();
// ...copy constructor
coord(const coord &source);
// ...overloaded = operator
coord& operator=(const coord &source);
friend coord operator+(const coord a, const coord b); // Overloaded addition
friend coord operator-(const coord a, const coord b); // Overloaded subtraction
// ...Overloaded comparison operators
friend int operator==(const coord &first_coord, const coord &second_coord);
// Input and output stream operators
friend std::ostream& operator<<(std::ostream& output_stream, const coord coord_to_output);
friend std::istream& operator>>(std::istream& input_stream, coord& coord_to_input);
};
class edge_node : virtual public CObject
{
DECLARE_DYNAMIC(edge_node);
private:
coord hotspot; // Indexing object
y_size_t length; // Data object
edge_node *next_edge_node; // Used to form list
public:
static coord undefined_index; // Holds definition of an undefined index
static compare (edge_node::*compare_method)(const coord &hotspot1, const coord &hotspot2);
edge_node(); // Default Constructor
virtual ~edge_node();
edge_node(const coord &new_hotspot, const y_size_t &new_length);
edge_node(const edge_node &source); // Copy Constructor
edge_node& operator=(const edge_node &source); // Overloaded = operator
friend int operator>(const edge_node &edge1, const edge_node &edge2);
coord get_index(); // Return value of index field
status set_index(const coord new_hotspot); // Set value of index field
void set_x_pos(const x_pos_t &new_x);
void set_y_pos(const y_pos_t &new_y);
x_pos_t get_x_pos();
y_pos_t get_y_pos();
void set_to_first_index(); // Return first index value
status set_to_next_index(const coord ref); // Return next index value
compare compare_index(const coord &hotspot1, const coord &hotspot2); // Compare two index objs
compare compare_index_y(const coord &hotspot1, const coord &hotspot2); // Compare two index objs
compare compare_index_x(const coord &hotspot1, const coord &hotspot2); // Compare two index objs
y_size_t get_data(); // Return value of data field
status set_data(const y_size_t &new_length); // Set value of data field
compare compare_data(const y_size_t &length1, // Return comparison between
const y_size_t &length2); // two data objects
void set_pointer_to(edge_node *p); // Set pointer field
edge_node* get_pointer(); // Return value of pointer field
void print_node(); // Print Node info
// Overloaded output operator for name_object
friend std::ostream& operator<<(std::ostream& output_stream, const edge_node &a);
#ifdef _DEBUG
virtual void Dump( CDumpContext& dc ) const;
#endif
};
const sequence_size max_coord_list=200; // should be static in coord_group class
class coord_group : virtual public CObject
{
DECLARE_DYNAMIC(coord_group);
public:
// static sequence_size max_coord_list;
sequence_size length;
coord coord_list[max_coord_list];
coord& operator[](const array_index &i);
coord_group();
virtual ~coord_group();
coord_group(const coord_group &source);
#ifdef _DEBUG
virtual void Dump( CDumpContext& dc ) const;
#endif
};
class test_set : virtual public CObject
{
DECLARE_DYNAMIC(test_set);
public:
static const int id_size = 10;
char id[id_size];
shape_group shapes;
static const int description_size = 1000;
char description[description_size];
placement_rule rule;
x_size_t sheet_width;
y_size_t sheet_height;
virtual ~test_set();
test_set& operator=(const test_set &source);
// Input and output stream operators
friend std::ostream& operator<<(std::ostream& output_stream, const test_set shape_l_to_output);
friend std::istream& operator>>(std::istream& input_stream, test_set& shape_l_to_input);
};
class test_set_group : virtual public CObject
{
DECLARE_DYNAMIC(test_set_group);
public:
sequence_size length;
array_index current;
test_set *test_list;
test_set_group();
virtual ~test_set_group();
test_set_group(const test_set_group &source);
test_set_group& operator=(const test_set_group& source);
test_set& next_current();
test_set& prev_current();
test_set& get_current();
test_set& set_current(const array_index setting);
test_set& operator[](const array_index &i);
friend std::istream& operator>>(std::istream& input_stream,
test_set_group& LDA_e_to_input);
#ifdef _DEBUG
virtual void Dump( CDumpContext& dc ) const;
#endif
};
class shape_l : public shape, public coord
{
DECLARE_DYNAMIC(shape_l);
public:
shape_l();
virtual ~shape_l();
shape_l(const shape_l &source);
// Input and output stream operators
friend std::ostream& operator<<(std::ostream& output_stream, const shape_l shape_l_to_output);
friend std::istream& operator>>(std::istream& input_stream, shape_l& shape_l_to_input);
y_pos_t top();
y_pos_t bot();
x_pos_t left();
x_pos_t right();
#ifdef _DEBUG
virtual void Dump( CDumpContext& dc ) const;
#endif
};
class gene : virtual public CObject
{
DECLARE_DYNAMIC(gene);
public:
virtual gene* clone() const = 0;
virtual gene* create() const = 0;
gene();
virtual ~gene();
virtual array_index get_id() = 0;
virtual void copy(gene *source) = 0;
virtual void copy_feature_1(gene *source) = 0;
virtual void copy_feature_2(gene *source) = 0;
virtual void copy_all_features(gene *source) = 0;
virtual void randomize_feature_1() = 0;
virtual void randomize_feature_2() = 0;
virtual void randomize_all_features() = 0;
// Input and output stream operators
friend std::ostream& operator<<(std::ostream& output_stream, const gene gene_to_output);
friend std::istream& operator>>(std::istream& input_stream, gene& gene_to_input);
};
class shape_g_l;
class shape_g : public gene
{
DECLARE_DYNAMIC(shape_g);
protected:
shape *p_piece;
angle orientation;
placement_rule heuristic;
public:
shape* get_tied_shape() const;
void set_tied_shape(shape* new_shape);
virtual array_index get_id();
angle get_orientation() const;
void set_orientation(angle new_orientation);
placement_rule get_heuristic() const;
void set_heuristic(const placement_rule &h);
virtual gene* clone() const;
virtual gene* create() const;
virtual void copy(gene *source);
virtual void copy_feature_1(gene *source);
virtual void copy_feature_2(gene *source);
virtual void copy_all_features(gene *source);
virtual void randomize_feature_1();
virtual void randomize_feature_2();
virtual void randomize_all_features();
// ...default constructor
shape_g();
// ...destructor
virtual ~shape_g();
// ...copy constructor
shape_g(const shape_g &source);
// ...overloaded = operator
shape_g& operator=(const shape_g &source);
shape_g& operator=(const shape_g_l &source);
// ...Overloaded comparison operators
friend int operator==(const shape_g &first_shape_g, const shape_g &second_shape_g);
// Input and output stream operators
friend std::ostream& operator<<(std::ostream& output_stream, const shape_g shape_g_to_output);
friend std::ostream& operator<<(std::ostream& output_stream, const shape_g *shape_g_to_output);
friend std::istream& operator>>(std::istream& input_stream, shape_g& shape_g_to_input);
y_size_t height();
x_size_t width();
};
class shape_g_l : public shape_g, public coord
{
DECLARE_DYNAMIC(shape_g_l);
public:
shape_g_l();
shape_g_l(shape_g &source);
shape_g_l(shape_g &source_shape_g, coord &source_coord);
// ...destructor
virtual ~shape_g_l();
// ...copy constructor
shape_g_l(const shape_g_l &source);
// ...overloaded = operator
shape_g_l& operator=(const shape_g_l &source);
shape_g_l& operator=(const shape_g &source);
// Input and output stream operators
friend std::ostream& operator<<(std::ostream& output_stream, shape_g_l &s);
friend std::istream& operator>>(std::istream& input_stream, shape_g_l& shape_g_l_to_input);
y_pos_t top();
y_pos_t bot();
x_pos_t left();
x_pos_t right();
#ifdef _DEBUG
virtual void Dump( CDumpContext& dc );
#endif
};
class environment : virtual public CObject
{
public:
virtual ~environment();
};
class chromosome : virtual public CObject
{
DECLARE_DYNAMIC(chromosome);
protected:
gene** sequence;
void generate_place_holders(const sequence_size &length);
bool fitness_valid;
public:
chromosome();
virtual ~chromosome();
chromosome(const chromosome &source);
virtual chromosome* clone() const = 0;
virtual chromosome* create() const = 0;
virtual chromosome* create_array(sequence_size sz) const = 0;
virtual chromosome* create_loaded_array(sequence_size sz, chromosome *first_element) = 0;
virtual void copy(chromosome* source) = 0;
virtual int size_of() = 0;
virtual gene*& operator[](const array_index &i);
virtual fitness_score fitness() = 0;
virtual void invalidate_fitness() = 0;
virtual void init_chromosome() = 0;
virtual void random_chromosome() = 0;
virtual void ordered_chromosome(array_index* gene_order) = 0;
virtual void get_permutation(array_index* test) = 0;
virtual void get_permutation_char(char* test) = 0;
virtual sequence_size get_length() = 0;
virtual sequence_size distance(chromosome &c)=0;
// ...overloaded = operator
// virtual chromosome& operator=(const chromosome &source);
// ...Overloaded comparison operators
friend int operator==(const chromosome &first_chromosome, const chromosome &second_chromosome);
// Input and output stream operators
friend std::ostream& operator<<(std::ostream& output_stream, const chromosome chromosome_to_output);
friend std::istream& operator>>(std::istream& input_stream, chromosome& chromosome_to_input);
};
typedef chromosome candidate;
class LDA_e;
class LDA_c : public chromosome
{
DECLARE_DYNAMIC(LDA_c);
private:
fitness_score fitness_value;
public:
static sequence_size LDA_length;
static LDA_e *_environment;
LDA_c();
virtual ~LDA_c();
LDA_c(const LDA_c &source);
chromosome* clone() const;
chromosome* create() const;
chromosome* create_loaded_array(sequence_size sz, chromosome* first_element);
chromosome* create_array(sequence_size sz) const;
void copy(chromosome* source);
virtual int size_of();
virtual gene*& operator[](const array_index &i);
virtual fitness_score fitness();
virtual void invalidate_fitness();
virtual void init_chromosome();
virtual void random_chromosome();
virtual void ordered_chromosome(array_index* gene_order);
virtual void get_permutation(array_index* test);
virtual void get_permutation_char(char* test);
virtual sequence_size get_length();
virtual sequence_size distance(chromosome &c);
// ...overloaded = operator
LDA_c& operator=(const LDA_c &source);
// ...Overloaded comparison operators
friend int operator==(const LDA_c &first_LDA_c, const LDA_c &second_LDA_c);
// Input and output stream operators
friend std::ostream& operator<<(std::ostream& output_stream, const LDA_c &LDA_c_to_output);
friend std::istream& operator>>(std::istream& input_stream, LDA_c& LDA_c_to_input);
#ifdef _DEBUG
virtual void Dump( CDumpContext& dc ) const;
#endif
};
class layout_piece : public CObject
{
public:
shape_g_l piece;
sheetflag_t new_sheet_needed;
layout_piece()
{ new_sheet_needed=s_NO; }
friend std::ostream& operator<<(std::ostream& output_stream, layout_piece &l);
#ifdef _DEBUG
virtual void Dump( CDumpContext& dc ) const;
#endif
};
struct area_t {xy_size_t x_span; xy_size_t y_span;};
class fitness_report
{
public:
array_index sheet_count;
array_index next_index;
static const int sheet_size = 100;
area_t sheet_fitness[sheet_size];
fitness_report()
{
sheet_count=next_index=0;
for (int i=0; i < sheet_size; i++)
sheet_fitness[i].x_span = sheet_fitness[i].y_span = 0;
}
void store_fitness(area_t fit)
{
sheet_fitness[next_index++]=fit;
sheet_count++;
}
status get_fitness(area_t &fit)
{
if (next_index<sheet_count)
{
fit=sheet_fitness[next_index++];
return SUCCESS;
}
else
return FAILURE;
}
void reset_index()
{ next_index=0; }
void reset_count()
{ sheet_count=0; next_index=0; }
//fitness_score overall_fitness();
};
class LDA_e : public environment
{
DECLARE_DYNAMIC(LDA_e);
private:
bool master_LDA;
array_index max_width_index;
public:
static array_index max_layout_items;
layout_piece *layout;
array_index next_index;
char _permutation[10000];
fitness_score _fitness;
fitness_report *_ftable;
protected:
x_size_t macro_width;
y_size_t macro_height;
shape_group *shapes;
virtual void clear_layout();
void clear_layout(x_size_t macro_wide, y_size_t macro_high);
virtual status add_to_layout(shape_g &item);
virtual area_t sheet_usage();
virtual fitness_score overall_usage1();
void fitness_r(LDA_c &LDA_test, const array_index &offset=0);
public:
bool mutate_orientation;
bool mutate_heuristic;
virtual ~LDA_e();
LDA_e(x_size_t macro_wide, y_size_t macro_high, layout_piece *lay,
fitness_report *table);
LDA_e(x_size_t macro_wide, y_size_t macro_high);
LDA_e();
virtual LDA_e* create(x_size_t macro_wide, y_size_t macro_high,
layout_piece *lay, fitness_report *table) const;
void set_size(x_size_t macro_wide, y_size_t macro_high);
x_size_t get_x_size();
y_size_t get_y_size();
fitness_score fitness(LDA_c &LDA_test);
void init_chromosome(LDA_c &LDA_test);
void random_chromosome(LDA_c &LDA_test);
void ordered_chromosome(LDA_c &LDA_test, array_index* gene_order);
void get_permutation(LDA_c &to_analyse, array_index* test);
void get_permutation_char(LDA_c &to_analyse, char* test);
void provide_shapes(shape_group *shape_list);
friend std::ostream& operator<<(std::ostream& output_stream, LDA_e &a);
virtual void set_rule_regime(const placement_rule &rule);
virtual placement_rule get_rule_regime();
virtual placement_rule get_current_rule();
virtual void process_current_rule(shape_g &item);
};
class LDA_new : public LDA_e
{
DECLARE_DYNAMIC(LDA_new);
private:
coord_group left_places;
ulist<edge_node> left_profile;
coord_group top_places;
ulist<edge_node> top_profile;
placement_rule current_rule;
placement_rule rule_regime;
status build_left_profile_places(shape_g &item);
coord choose_leftmost_place(const status &left_list_valid,
const status &top_list_valid);
coord choose_ileftmost_place(const status &left_list_valid,
const status &top_list_valid);
void add_shape_at_place_6(shape_g_l &chosen_gene, const profile_t adjust_profile);
status build_top_profile_places(shape_g &item);
coord choose_topmost_place(const status &top_list_valid,
const status &left_list_valid);
coord choose_itopmost_place(const status &top_list_valid,
const status &left_list_valid);
virtual status add_to_layout(shape_g &item);
virtual void clear_layout();
void flip_placement_rule();
void restore_current_rule();
virtual area_t sheet_usage();
virtual fitness_score overall_usage1();
public:
virtual ~LDA_new();
LDA_new(x_size_t macro_wide, y_size_t macro_high);
LDA_new(x_size_t macro_wide, y_size_t macro_high, layout_piece *lay,
fitness_report *table);
LDA_new();
virtual LDA_e* create(x_size_t macro_wide, y_size_t macro_high,
layout_piece *lay, fitness_report *table) const;
virtual void set_rule_regime(const placement_rule &rule);
virtual placement_rule get_rule_regime();
virtual placement_rule get_current_rule();
virtual void process_current_rule(shape_g &item);
friend std::ostream& operator<<(std::ostream& output_stream, LDA_new &a);
};
class population : virtual public CObject
{
public:
int candidate_size;
sequence_size size;
candidate *sequence;
population();
population(candidate *type, array_index size);
virtual ~population();
population(const population &source);
candidate* get_candidate(const array_index &i);
candidate* new_candidate();
void replace_candidate(array_index removal, candidate *new_candidate);
void randomize();
void standardize();
friend std::ostream& operator<<(std::ostream& output_stream, const population &a);
};
void permutation(sequence_size length, sequence_size permut_length,
array_index *order);
status check_permutation(sequence_size length, array_index* test);
void seed_random();
void seed_random_new();
#endif