質問編集履歴

5

.csvファイルのスクショも追加しました.

2021/07/20 02:09

投稿

RyoBaya
RyoBaya

スコア1

test CHANGED
File without changes
test CHANGED
@@ -620,4 +620,4 @@
620
620
 
621
621
  ![sampledata.txt](079267926ed390de3d46eddb7189b23d.png)
622
622
 
623
- .csvも用意しています.
623
+ ![sampledata.csv](a37aee5f791d63c557a0f0e57ef75571.png)

4

使用しているsampledataを追加しました.

2021/07/20 02:09

投稿

RyoBaya
RyoBaya

スコア1

test CHANGED
File without changes
test CHANGED
@@ -615,3 +615,9 @@
615
615
  macbook pro macOS BigSur ver.11.4
616
616
 
617
617
  コンパイラ環境はEmacsです
618
+
619
+
620
+
621
+ ![sampledata.txt](079267926ed390de3d46eddb7189b23d.png)
622
+
623
+ .csvも用意しています.

3

エラー内容を変更しました.

2021/07/20 01:00

投稿

RyoBaya
RyoBaya

スコア1

test CHANGED
File without changes
test CHANGED
@@ -8,692 +8,596 @@
8
8
 
9
9
  ```
10
10
 
11
- In file included from flowerset.cpp:1:
12
-
13
- ./FlowerSet.h:3:20: warning: extra tokens at end of #include directive [-Wextra-tokens]
14
-
15
- #include "Flower.h"<U+0013>
16
-
17
- ^
18
-
19
- //
20
-
21
- In file included from flowerset.cpp:1:
22
-
23
- In file included from ./FlowerSet.h:3:
24
-
25
- ./Flower.h:5:23: warning: extra tokens at end of #include directive [-Wextra-tokens]
26
-
27
- #include "FlowerSet.h"<U+0013>
28
-
29
- ^
30
-
31
- //
32
-
33
- In file included from flowerset.cpp:1:
34
-
35
- In file included from ./FlowerSet.h:3:
36
-
37
- In file included from ./Flower.h:5:
38
-
39
- ./FlowerSet.h:3:20: warning: extra tokens at end of #include directive [-Wextra-tokens]
40
-
41
- #include "Flower.h"<U+0013>
42
-
43
- ^
44
-
45
- //
46
-
47
- ./FlowerSet.h:19:3: error: unknown type name 'Flower'
11
+ g++ -o bee sample04.cpp
12
+
13
+ sample04.cpp:10:25: warning: conversion from string literal to 'char *' is deprecated [-Wc++11-compat-deprecated-writable-strings]
14
+
15
+ fSet = new FlowerSet("sampledata.csv");
16
+
17
+ ^
18
+
19
+ sample04.cpp:16:17: warning: illegal character encoding in string literal [-Winvalid-source-encoding]
20
+
21
+ printf("%d<89><F1><96>ځF<8D>ŗǕ]<89><BF><92>l%f\n", i, fSet->bestValue);
22
+
23
+ ^~~~~~~~~~~~ ~~~~ ~~~~~~~~~~~~
24
+
25
+ sample04.cpp:16:7: error: use of undeclared identifier 'printf'
26
+
27
+ printf("%d<89><F1><96>ځF<8D>ŗǕ]<89><BF><92>l%f\n", i, fSet->bestValue);
28
+
29
+ ^
30
+
31
+ 2 warnings and 1 error generated.
32
+
33
+ ```
34
+
35
+
36
+
37
+ ### 該当のソースコード
38
+
39
+
40
+
41
+ ```FlowerSet
42
+
43
+ #include "FlowerSet.h"
44
+
45
+ #pragma once
46
+
47
+ #define filename data.txt
48
+
49
+ //コントラスタ
50
+
51
+ // filename: データセットのファイル名
52
+
53
+ FlowerSet::Flower(char *filename){
54
+
55
+ int i,best;
56
+
57
+
58
+
59
+ dataset=new Dataset(filename);
60
+
61
+ flower=new Flower* [EBEE_NUM];
62
+
63
+ best=0;
64
+
65
+
66
+
67
+ for(i=0;i<EBEE_NUM;i++){
68
+
69
+ flower[i]=new Flower(this);
70
+
71
+ if(flower[best]->value>flower[i]->value){
72
+
73
+ best=i;
74
+
75
+ }
76
+
77
+ }
78
+
79
+ bestPos=new double[dataset->exVarNum];
80
+
81
+ for(i=0;i<dataset->exVarNum;i++){
82
+
83
+ bestPos[i]=flower[best]->pos[i];
84
+
85
+ }
86
+
87
+ bestValue=flower[best]->value;
88
+
89
+ newFlower=new Flower(this);
90
+
91
+ trValue=new double[EBEE_NUM];
92
+
93
+ }
94
+
95
+
96
+
97
+ //デストラクタ
98
+
99
+ FlowerSet::~FlowerSet(){
100
+
101
+ int i;
102
+
103
+
104
+
105
+ for(i=0;i<EBEE_NUM;i++){
106
+
107
+ delete flower[i];
108
+
109
+ }
110
+
111
+ delete [] flower;
112
+
113
+ delete [] bestPos;
114
+
115
+ delete newFlower;
116
+
117
+ delete [] trValue;
118
+
119
+ delete dataset;
120
+
121
+
122
+
123
+ //収穫蜂フェーズを実行する
124
+
125
+ void FlowerSet::employedBeePhase()
126
+
127
+ {
128
+
129
+ int i;
130
+
131
+ Flower *tmp;
132
+
133
+
134
+
135
+ for(i=0;i<EBEE_NUM;i++){
136
+
137
+ newFlower->change(i);
138
+
139
+ if(flower[i]-> value > newFlower -> value){
140
+
141
+ tmp=newFlower;
142
+
143
+ newFlower=flower[i];
144
+
145
+ flower[i]=tmp;
146
+
147
+ }
148
+
149
+ flower[i]->visitNum++;
150
+
151
+ }
152
+
153
+ }
154
+
155
+ //追従蜂フェーズを実行する
156
+
157
+ void FlowerSet::onlookerBeePhase()
158
+
159
+ {
160
+
161
+ int i,j;
162
+
163
+ Flower *tmp;
164
+
165
+ double max,min,denom,prob,r;
166
+
167
+
168
+
169
+ for(j=0;j<OBEE_NUM;j++){
170
+
171
+ //評価値を変換する
172
+
173
+ max=DBL_MIN;
174
+
175
+ min=DBL_MAX;
176
+
177
+ for(i=0;i<EBEE_NUM;i++){
178
+
179
+ if(max<flower[i]->value){
180
+
181
+ max=flower[i]->value;
182
+
183
+ }
184
+
185
+ if(min<flower[i]->value){
186
+
187
+ min=flower[i]->value;
188
+
189
+ }
190
+
191
+ }
192
+
193
+ denom=0.0;
194
+
195
+ for(i=0;i<EBEE_NUM;i++){
196
+
197
+ trValue[i]=(max-flower[i]->value)/(max-min);
198
+
199
+ denom+=trValue[i];
200
+
201
+ }
202
+
203
+
204
+
205
+ //収穫蜂を選択する
206
+
207
+ r=RAND_01;
208
+
209
+ for(i=0;i<EBEE_NUM-1;i++){
210
+
211
+ prob=trValue[i]/denom;
212
+
213
+ if(r<=prob){
214
+
215
+ break;
216
+
217
+ }
218
+
219
+ r -=prob;
220
+
221
+ }
222
+
223
+
224
+
225
+ //収穫蜂フェーズと同様に処理する
226
+
227
+ newFlower->change(i);
228
+
229
+ if(flower[i]->value>newFlower->value){
230
+
231
+ tmp=newFlower;
232
+
233
+ newFlower=flower[i];
234
+
235
+ flower[i]=tmp;
236
+
237
+ }
238
+
239
+ flower[i]->visitNum++;
240
+
241
+ }
242
+
243
+ }
244
+
245
+
246
+
247
+ //偵察蜂フェーズを実行する
248
+
249
+ void FlowerSet::scoutBeePhase()
250
+
251
+ {
252
+
253
+ int i;
254
+
255
+ for (i=0;i<EBEE_NUM;i++){
256
+
257
+ if(VISIT_MAX<=flower[i]->visitNum){
258
+
259
+ flower[i]->renew();
260
+
261
+ }
262
+
263
+ }
264
+
265
+ }
266
+
267
+
268
+
269
+ //最良食糧源を記録する
270
+
271
+ void FlowerSet::saveBestPos()
272
+
273
+ {
274
+
275
+ int i,best;
276
+
277
+
278
+
279
+ best= -1;
280
+
281
+ for(i=0;i<EBEE_NUM;i++){
282
+
283
+ if(bestValue>flower[i]->value){
284
+
285
+ best=i;
286
+
287
+ }
288
+
289
+ }
290
+
291
+ if(best!=-1){
292
+
293
+ for(i=0;i<dataset->exVarNum;i++){
294
+
295
+ bestPos[i]=flower[best]->value;
296
+
297
+ }
298
+
299
+ bestValue=flower[best]->value;
300
+
301
+ }
302
+
303
+ }
304
+
305
+ //結果を表示する
306
+
307
+ void FlowerSet::printResult()
308
+
309
+ {
310
+
311
+ dataset->setCoef(bestPos);
312
+
313
+ dataset->printEquation();
314
+
315
+ }
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+ ```
324
+
325
+
326
+
327
+ ```Flower
328
+
329
+ //Flower.h
330
+
331
+ #ifndef INCLUDED_FLOWER
332
+
333
+ #define INCLUDED_FLOWER
334
+
335
+ #include "DataSet.h"
336
+
337
+ #include "FlowerSet.h"
338
+
339
+ #pragma once
340
+
341
+ class FlowerSet;
342
+
343
+
344
+
345
+ //定数の定義
346
+
347
+ #define REPEAT_NUM 1000 //繰り返し数
348
+
349
+ #define EBEE_NUM 100 //収穫蜂の数
350
+
351
+ #define OBEE_NUM 10 //追従蜂の数
352
+
353
+ #define VISIT_MAX 10 //蜜の採取可能回数
354
+
355
+ #define COEF_MIN -1 //標準偏回帰係数の最小値
356
+
357
+ #define COEF_MAX 1 //標準偏回帰係数の最大値
358
+
359
+
360
+
361
+ //0以上1以下の実数乱数
362
+
363
+ #define RAND_01 ((double)rand() / RAND_MAX)
364
+
365
+
366
+
367
+ class Flower{
368
+
369
+ public:
370
+
371
+ Flower(FlowerSet *argFSet);
372
+
373
+ ~Flower();
374
+
375
+ void change(int base); //近くの食糧源に変更する
376
+
377
+ void renew();//新しい食糧源に変更する.
378
+
379
+
380
+
381
+ FlowerSet *fSet; //属している食糧源集合
382
+
383
+ double *pos; //位置
384
+
385
+ double value;//評価値
386
+
387
+ int visitNum;//蜜の採取回数
388
+
389
+
390
+
391
+ private:
392
+
393
+ void evaluate();//評価値を算出する
394
+
395
+ };
396
+
397
+ #endif
398
+
399
+ ```
400
+
401
+
402
+
403
+ ```Flower
404
+
405
+ //Flower.cpp
406
+
407
+ #include "Flower.h"
408
+
409
+ #pragma once
410
+
411
+
412
+
413
+ //コントラスた
414
+
415
+ //argFSet:属している食糧源集合
416
+
417
+
418
+
419
+ Flower::Flower(FlowerSet *argFSet){
420
+
421
+ int i;
422
+
423
+
424
+
425
+ fSet=argFSet;
426
+
427
+ pos=new double[fSet->dataset->exVarNum];
428
+
429
+ for(i=0;i<fSet->dataset->exVarNum;i++){
430
+
431
+ pos[i]=COEF_MIN+(COEF_MAX-COEF_MIN)*RAND_01;
432
+
433
+ }
434
+
435
+ visitNum=0;
436
+
437
+ evaluate();
438
+
439
+ }
440
+
441
+
442
+
443
+ //デストラクタ
444
+
445
+ Flower::~Flower(){
446
+
447
+ delete[]pos;
448
+
449
+ }
450
+
451
+ //baseの近くの食糧源に変更する
452
+
453
+ //base:元にする食糧源の添字
454
+
455
+ void Flower::change(int base){
456
+
457
+ int i,j;
458
+
459
+
460
+
461
+ for(i=0;i<fSet->dataset->exVarNum;i++){
462
+
463
+ pos[i]=fSet->flower[base]->pos[i];
464
+
465
+ }
466
+
467
+ i=rand() % fSet->dataset->exVarNum;
468
+
469
+ j=(base+(rand() % (EBEE_NUM-1)+1)) % EBEE_NUM;
470
+
471
+ pos[i]=pos[i]+(rand() / (RAND_MAX/2.0)-1)*(pos[i]-fSet->flower[j]->pos[i]);
472
+
473
+ visitNum=0;
474
+
475
+ evaluate();
476
+
477
+ }
478
+
479
+
480
+
481
+ //新しい食糧源に変更する
482
+
483
+ void Flower::renew(){
484
+
485
+ int i;
486
+
487
+ for(i=0;i<fSet->dataset->exVarNum;i++){
488
+
489
+ pos[i]=COEF_MIN+(COEF_MAX - COEF_MIN)*RAND_01;
490
+
491
+ }
492
+
493
+ visitNum=0;
494
+
495
+ evaluate();
496
+
497
+ }
498
+
499
+
500
+
501
+ //評価値を算出する
502
+
503
+ void Flower::evaluate(){
504
+
505
+ int i,j;
506
+
507
+ double diff;
508
+
509
+
510
+
511
+ value=0.0;
512
+
513
+ for(i=0;i<fSet->dataset->dataNum;i++){
514
+
515
+ diff=fSet->dataset->resSData[i];
516
+
517
+ for(j=0;j<fSet->dataset->exSData[i][j];j++){
518
+
519
+ diff -= pos[j]*fSet->dataset->exSData[i][j];
520
+
521
+ }
522
+
523
+ value += pow(diff,2.0);
524
+
525
+ }
526
+
527
+ }
528
+
529
+
530
+
531
+ ```
532
+
533
+
534
+
535
+ ```FlowerSet
536
+
537
+ //FlowerSet.h
538
+
539
+ #ifndef INCLUDED_FLOWERSET
540
+
541
+ #define INCLUDE_FLOWERSET
542
+
543
+ #include "Flower.h"
544
+
545
+ #pragma once
546
+
547
+ class Flower;
548
+
549
+
550
+
551
+ class FlowerSet
552
+
553
+ {
554
+
555
+ public:
556
+
557
+ FlowerSet(char *filename);
558
+
559
+ ~FlowerSet();
560
+
561
+ void employedBeePhase(); //収穫蜂フェーズを実行する
562
+
563
+ void onlockerBeePhase();//追従蜂フェーズを実行する
564
+
565
+ void scoutBeePhase();//偵察蜂フェーズを実行する.
566
+
567
+ void saveBestPos();//最良食糧源を記録する.
568
+
569
+ void printResult();//結果を表示する.
570
+
571
+
572
+
573
+ Dataset *dataset;//データセット
48
574
 
49
575
  Flower **flower;//食糧源の集合のメンバ
50
576
 
51
- ^
577
+ double *bestPos;//最良食糧源の位置
52
-
578
+
53
- ./FlowerSet.h:24:3: error: unknown type name 'Flower'
579
+ double bestValue;//最良食糧源の評価値
580
+
581
+
582
+
583
+ private:
54
584
 
55
585
  Flower *newFlower; //新しい食糧源
56
586
 
57
- ^
58
-
59
- In file included from flowerset.cpp:1:
587
+ double *trValue; //格食糧源の変換後の評価値
60
-
61
- ./FlowerSet.h:7:7: error: redefinition of 'FlowerSet'
588
+
62
-
63
- class FlowerSet
64
-
65
- ^
589
+ };
66
-
67
- ./Flower.h:5:10: note: './FlowerSet.h' included multiple times, additional include site here
590
+
68
-
69
- #include "FlowerSet.h"<U+0013>
591
+
70
-
71
- ^
592
+
72
-
73
- flowerset.cpp:1:10: note: './FlowerSet.h' included multiple times, additional include site here
74
-
75
- #include "FlowerSet.h"
76
-
77
- ^
78
-
79
- flowerset.cpp:2:9: warning: #pragma once in main file [-Wpragma-once-outside-header]
80
-
81
- #pragma once
593
+ #endif
82
-
83
- ^
594
+
84
-
85
- flowerset.cpp:43:5: error: function definition is not allowed here
595
+
86
-
87
- {
596
+
88
-
89
- ^
597
+
90
-
91
- flowerset.cpp:59:5: error: function definition is not allowed here
92
-
93
- {
94
-
95
- ^
96
-
97
- flowerset.cpp:105:5: error: function definition is not allowed here
98
-
99
- {
100
-
101
- ^
102
-
103
- flowerset.cpp:116:5: error: function definition is not allowed here
104
-
105
- {
106
-
107
- ^
108
-
109
- flowerset.cpp:134:5: error: function definition is not allowed here
110
-
111
- {
112
-
113
- ^
114
-
115
- flowerset.cpp:139:3: error: expected '}'
116
-
117
-
118
-
119
- ^
120
-
121
- flowerset.cpp:29:24: note: to match this '{'
122
-
123
- FlowerSet::~FlowerSet(){
124
-
125
- ^
126
-
127
- 4 warnings and 9 errors generated.
128
598
 
129
599
  ```
130
600
 
131
-
132
-
133
- ### 該当のソースコード
134
-
135
-
136
-
137
- ```FlowerSet
138
-
139
- #include "FlowerSet.h"
140
-
141
- #pragma once
142
-
143
- #define filename data.txt
144
-
145
- //コントラスタ
146
-
147
- // filename: データセットのファイル名
148
-
149
- FlowerSet::Flower(char *filename){
150
-
151
- int i,best;
152
-
153
-
154
-
155
- dataset=new Dataset(filename);
156
-
157
- flower=new Flower* [EBEE_NUM];
158
-
159
- best=0;
160
-
161
-
162
-
163
- for(i=0;i<EBEE_NUM;i++){
164
-
165
- flower[i]=new Flower(this);
166
-
167
- if(flower[best]->value>flower[i]->value){
168
-
169
- best=i;
170
-
171
- }
172
-
173
- }
174
-
175
- bestPos=new double[dataset->exVarNum];
176
-
177
- for(i=0;i<dataset->exVarNum;i++){
178
-
179
- bestPos[i]=flower[best]->pos[i];
180
-
181
- }
182
-
183
- bestValue=flower[best]->value;
184
-
185
- newFlower=new Flower(this);
186
-
187
- trValue=new double[EBEE_NUM];
188
-
189
- }
190
-
191
-
192
-
193
- //デストラクタ
194
-
195
- FlowerSet::~FlowerSet(){
196
-
197
- int i;
198
-
199
-
200
-
201
- for(i=0;i<EBEE_NUM;i++){
202
-
203
- delete flower[i];
204
-
205
- }
206
-
207
- delete [] flower;
208
-
209
- delete [] bestPos;
210
-
211
- delete newFlower;
212
-
213
- delete [] trValue;
214
-
215
- delete dataset;
216
-
217
-
218
-
219
- //収穫蜂フェーズを実行する
220
-
221
- void FlowerSet::employedBeePhase()
222
-
223
- {
224
-
225
- int i;
226
-
227
- Flower *tmp;
228
-
229
-
230
-
231
- for(i=0;i<EBEE_NUM;i++){
232
-
233
- newFlower->change(i);
234
-
235
- if(flower[i]-> value > newFlower -> value){
236
-
237
- tmp=newFlower;
238
-
239
- newFlower=flower[i];
240
-
241
- flower[i]=tmp;
242
-
243
- }
244
-
245
- flower[i]->visitNum++;
246
-
247
- }
248
-
249
- }
250
-
251
- //追従蜂フェーズを実行する
252
-
253
- void FlowerSet::onlookerBeePhase()
254
-
255
- {
256
-
257
- int i,j;
258
-
259
- Flower *tmp;
260
-
261
- double max,min,denom,prob,r;
262
-
263
-
264
-
265
- for(j=0;j<OBEE_NUM;j++){
266
-
267
- //評価値を変換する
268
-
269
- max=DBL_MIN;
270
-
271
- min=DBL_MAX;
272
-
273
- for(i=0;i<EBEE_NUM;i++){
274
-
275
- if(max<flower[i]->value){
276
-
277
- max=flower[i]->value;
278
-
279
- }
280
-
281
- if(min<flower[i]->value){
282
-
283
- min=flower[i]->value;
284
-
285
- }
286
-
287
- }
288
-
289
- denom=0.0;
290
-
291
- for(i=0;i<EBEE_NUM;i++){
292
-
293
- trValue[i]=(max-flower[i]->value)/(max-min);
294
-
295
- denom+=trValue[i];
296
-
297
- }
298
-
299
-
300
-
301
- //収穫蜂を選択する
302
-
303
- r=RAND_01;
304
-
305
- for(i=0;i<EBEE_NUM-1;i++){
306
-
307
- prob=trValue[i]/denom;
308
-
309
- if(r<=prob){
310
-
311
- break;
312
-
313
- }
314
-
315
- r -=prob;
316
-
317
- }
318
-
319
-
320
-
321
- //収穫蜂フェーズと同様に処理する
322
-
323
- newFlower->change(i);
324
-
325
- if(flower[i]->value>newFlower->value){
326
-
327
- tmp=newFlower;
328
-
329
- newFlower=flower[i];
330
-
331
- flower[i]=tmp;
332
-
333
- }
334
-
335
- flower[i]->visitNum++;
336
-
337
- }
338
-
339
- }
340
-
341
-
342
-
343
- //偵察蜂フェーズを実行する
344
-
345
- void FlowerSet::scoutBeePhase()
346
-
347
- {
348
-
349
- int i;
350
-
351
- for (i=0;i<EBEE_NUM;i++){
352
-
353
- if(VISIT_MAX<=flower[i]->visitNum){
354
-
355
- flower[i]->renew();
356
-
357
- }
358
-
359
- }
360
-
361
- }
362
-
363
-
364
-
365
- //最良食糧源を記録する
366
-
367
- void FlowerSet::saveBestPos()
368
-
369
- {
370
-
371
- int i,best;
372
-
373
-
374
-
375
- best= -1;
376
-
377
- for(i=0;i<EBEE_NUM;i++){
378
-
379
- if(bestValue>flower[i]->value){
380
-
381
- best=i;
382
-
383
- }
384
-
385
- }
386
-
387
- if(best!=-1){
388
-
389
- for(i=0;i<dataset->exVarNum;i++){
390
-
391
- bestPos[i]=flower[best]->value;
392
-
393
- }
394
-
395
- bestValue=flower[best]->value;
396
-
397
- }
398
-
399
- }
400
-
401
- //結果を表示する
402
-
403
- void FlowerSet::printResult()
404
-
405
- {
406
-
407
- dataset->setCoef(bestPos);
408
-
409
- dataset->printEquation();
410
-
411
- }
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
- ```
420
-
421
-
422
-
423
- ```Flower
424
-
425
- //Flower.h
426
-
427
- #ifndef INCLUDED_FLOWER
428
-
429
- #define INCLUDED_FLOWER
430
-
431
- #include "DataSet.h"
432
-
433
- #include "FlowerSet.h"
434
-
435
- #pragma once
436
-
437
- class FlowerSet;
438
-
439
-
440
-
441
- //定数の定義
442
-
443
- #define REPEAT_NUM 1000 //繰り返し数
444
-
445
- #define EBEE_NUM 100 //収穫蜂の数
446
-
447
- #define OBEE_NUM 10 //追従蜂の数
448
-
449
- #define VISIT_MAX 10 //蜜の採取可能回数
450
-
451
- #define COEF_MIN -1 //標準偏回帰係数の最小値
452
-
453
- #define COEF_MAX 1 //標準偏回帰係数の最大値
454
-
455
-
456
-
457
- //0以上1以下の実数乱数
458
-
459
- #define RAND_01 ((double)rand() / RAND_MAX)
460
-
461
-
462
-
463
- class Flower{
464
-
465
- public:
466
-
467
- Flower(FlowerSet *argFSet);
468
-
469
- ~Flower();
470
-
471
- void change(int base); //近くの食糧源に変更する
472
-
473
- void renew();//新しい食糧源に変更する.
474
-
475
-
476
-
477
- FlowerSet *fSet; //属している食糧源集合
478
-
479
- double *pos; //位置
480
-
481
- double value;//評価値
482
-
483
- int visitNum;//蜜の採取回数
484
-
485
-
486
-
487
- private:
488
-
489
- void evaluate();//評価値を算出する
490
-
491
- };
492
-
493
- #endif
494
-
495
- ```
496
-
497
-
498
-
499
- ```Flower
500
-
501
- //Flower.cpp
502
-
503
- #include "Flower.h"
504
-
505
- #pragma once
506
-
507
-
508
-
509
- //コントラスた
510
-
511
- //argFSet:属している食糧源集合
512
-
513
-
514
-
515
- Flower::Flower(FlowerSet *argFSet){
516
-
517
- int i;
518
-
519
-
520
-
521
- fSet=argFSet;
522
-
523
- pos=new double[fSet->dataset->exVarNum];
524
-
525
- for(i=0;i<fSet->dataset->exVarNum;i++){
526
-
527
- pos[i]=COEF_MIN+(COEF_MAX-COEF_MIN)*RAND_01;
528
-
529
- }
530
-
531
- visitNum=0;
532
-
533
- evaluate();
534
-
535
- }
536
-
537
-
538
-
539
- //デストラクタ
540
-
541
- Flower::~Flower(){
542
-
543
- delete[]pos;
544
-
545
- }
546
-
547
- //baseの近くの食糧源に変更する
548
-
549
- //base:元にする食糧源の添字
550
-
551
- void Flower::change(int base){
552
-
553
- int i,j;
554
-
555
-
556
-
557
- for(i=0;i<fSet->dataset->exVarNum;i++){
558
-
559
- pos[i]=fSet->flower[base]->pos[i];
560
-
561
- }
562
-
563
- i=rand() % fSet->dataset->exVarNum;
564
-
565
- j=(base+(rand() % (EBEE_NUM-1)+1)) % EBEE_NUM;
566
-
567
- pos[i]=pos[i]+(rand() / (RAND_MAX/2.0)-1)*(pos[i]-fSet->flower[j]->pos[i]);
568
-
569
- visitNum=0;
570
-
571
- evaluate();
572
-
573
- }
574
-
575
-
576
-
577
- //新しい食糧源に変更する
578
-
579
- void Flower::renew(){
580
-
581
- int i;
582
-
583
- for(i=0;i<fSet->dataset->exVarNum;i++){
584
-
585
- pos[i]=COEF_MIN+(COEF_MAX - COEF_MIN)*RAND_01;
586
-
587
- }
588
-
589
- visitNum=0;
590
-
591
- evaluate();
592
-
593
- }
594
-
595
-
596
-
597
- //評価値を算出する
598
-
599
- void Flower::evaluate(){
600
-
601
- int i,j;
602
-
603
- double diff;
604
-
605
-
606
-
607
- value=0.0;
608
-
609
- for(i=0;i<fSet->dataset->dataNum;i++){
610
-
611
- diff=fSet->dataset->resSData[i];
612
-
613
- for(j=0;j<fSet->dataset->exSData[i][j];j++){
614
-
615
- diff -= pos[j]*fSet->dataset->exSData[i][j];
616
-
617
- }
618
-
619
- value += pow(diff,2.0);
620
-
621
- }
622
-
623
- }
624
-
625
-
626
-
627
- ```
628
-
629
-
630
-
631
- ```FlowerSet
632
-
633
- //FlowerSet.h
634
-
635
- #ifndef INCLUDED_FLOWERSET
636
-
637
- #define INCLUDE_FLOWERSET
638
-
639
- #include "Flower.h"
640
-
641
- #pragma once
642
-
643
- class Flower;
644
-
645
-
646
-
647
- class FlowerSet
648
-
649
- {
650
-
651
- public:
652
-
653
- FlowerSet(char *filename);
654
-
655
- ~FlowerSet();
656
-
657
- void employedBeePhase(); //収穫蜂フェーズを実行する
658
-
659
- void onlockerBeePhase();//追従蜂フェーズを実行する
660
-
661
- void scoutBeePhase();//偵察蜂フェーズを実行する.
662
-
663
- void saveBestPos();//最良食糧源を記録する.
664
-
665
- void printResult();//結果を表示する.
666
-
667
-
668
-
669
- Dataset *dataset;//データセット
670
-
671
- Flower **flower;//食糧源の集合のメンバ
672
-
673
- double *bestPos;//最良食糧源の位置
674
-
675
- double bestValue;//最良食糧源の評価値
676
-
677
-
678
-
679
- private:
680
-
681
- Flower *newFlower; //新しい食糧源
682
-
683
- double *trValue; //格食糧源の変換後の評価値
684
-
685
- };
686
-
687
-
688
-
689
- #endif
690
-
691
-
692
-
693
-
694
-
695
- ```
696
-
697
601
  ### 試したこと
698
602
 
699
603
 

2

現在発生しているエラーです.

2021/07/19 06:25

投稿

RyoBaya
RyoBaya

スコア1

test CHANGED
File without changes
test CHANGED
@@ -8,696 +8,692 @@
8
8
 
9
9
  ```
10
10
 
11
+ In file included from flowerset.cpp:1:
12
+
13
+ ./FlowerSet.h:3:20: warning: extra tokens at end of #include directive [-Wextra-tokens]
14
+
15
+ #include "Flower.h"<U+0013>
16
+
17
+ ^
18
+
19
+ //
20
+
21
+ In file included from flowerset.cpp:1:
22
+
23
+ In file included from ./FlowerSet.h:3:
24
+
25
+ ./Flower.h:5:23: warning: extra tokens at end of #include directive [-Wextra-tokens]
26
+
27
+ #include "FlowerSet.h"<U+0013>
28
+
29
+ ^
30
+
31
+ //
32
+
33
+ In file included from flowerset.cpp:1:
34
+
35
+ In file included from ./FlowerSet.h:3:
36
+
37
+ In file included from ./Flower.h:5:
38
+
39
+ ./FlowerSet.h:3:20: warning: extra tokens at end of #include directive [-Wextra-tokens]
40
+
41
+ #include "Flower.h"<U+0013>
42
+
43
+ ^
44
+
45
+ //
46
+
47
+ ./FlowerSet.h:19:3: error: unknown type name 'Flower'
48
+
49
+ Flower **flower;//食糧源の集合のメンバ
50
+
51
+ ^
52
+
53
+ ./FlowerSet.h:24:3: error: unknown type name 'Flower'
54
+
55
+ Flower *newFlower; //新しい食糧源
56
+
57
+ ^
58
+
59
+ In file included from flowerset.cpp:1:
60
+
11
- error: redefinition of 'FlowerSet'
61
+ ./FlowerSet.h:7:7: error: redefinition of 'FlowerSet'
12
62
 
13
63
  class FlowerSet
14
64
 
15
65
  ^
16
66
 
67
+ ./Flower.h:5:10: note: './FlowerSet.h' included multiple times, additional include site here
68
+
69
+ #include "FlowerSet.h"<U+0013>
70
+
71
+ ^
72
+
73
+ flowerset.cpp:1:10: note: './FlowerSet.h' included multiple times, additional include site here
74
+
75
+ #include "FlowerSet.h"
76
+
77
+ ^
78
+
79
+ flowerset.cpp:2:9: warning: #pragma once in main file [-Wpragma-once-outside-header]
80
+
81
+ #pragma once
82
+
83
+ ^
84
+
17
- flowerset.cpp:7:12: error: C++ requires a type specifier for all declarations
85
+ flowerset.cpp:43:5: error: function definition is not allowed here
86
+
87
+ {
88
+
89
+ ^
90
+
91
+ flowerset.cpp:59:5: error: function definition is not allowed here
92
+
93
+ {
94
+
95
+ ^
96
+
97
+ flowerset.cpp:105:5: error: function definition is not allowed here
98
+
99
+ {
100
+
101
+ ^
102
+
103
+ flowerset.cpp:116:5: error: function definition is not allowed here
104
+
105
+ {
106
+
107
+ ^
108
+
109
+ flowerset.cpp:134:5: error: function definition is not allowed here
110
+
111
+ {
112
+
113
+ ^
114
+
115
+ flowerset.cpp:139:3: error: expected '}'
116
+
117
+
118
+
119
+ ^
120
+
121
+ flowerset.cpp:29:24: note: to match this '{'
122
+
123
+ FlowerSet::~FlowerSet(){
124
+
125
+ ^
126
+
127
+ 4 warnings and 9 errors generated.
128
+
129
+ ```
130
+
131
+
132
+
133
+ ### 該当のソースコード
134
+
135
+
136
+
137
+ ```FlowerSet
138
+
139
+ #include "FlowerSet.h"
140
+
141
+ #pragma once
142
+
143
+ #define filename data.txt
144
+
145
+ //コントラスタ
146
+
147
+ // filename: データセットのファイル名
18
148
 
19
149
  FlowerSet::Flower(char *filename){
20
150
 
151
+ int i,best;
152
+
153
+
154
+
21
- flowerset.cpp:11:14: error: must use 'class' tag to refer to type 'Flower' in this scope
155
+ dataset=new Dataset(filename);
22
-
156
+
23
- flower=new Flower *[EBEE_NUM];
157
+ flower=new Flower* [EBEE_NUM];
24
-
25
- ^
158
+
26
-
27
- class
159
+ best=0;
28
-
29
- flowerset.cpp:7:12: note: class 'Flower' is hidden by a non-type declaration of 'Flower' here
160
+
30
-
161
+
162
+
31
- FlowerSet::Flower(char *filename){
163
+ for(i=0;i<EBEE_NUM;i++){
32
-
33
- ^
34
-
35
- flowerset.cpp:15:19: error: must use 'class' tag to refer to type 'Flower' in this scope
36
164
 
37
165
  flower[i]=new Flower(this);
38
166
 
39
- ^
40
-
41
- class
42
-
43
- flowerset.cpp:7:12: note: class 'Flower' is hidden by a non-type declaration of 'Flower' here
44
-
45
- FlowerSet::Flower(char *filename){
167
+ if(flower[best]->value>flower[i]->value){
168
+
46
-
169
+ best=i;
170
+
47
- ^
171
+ }
172
+
48
-
173
+ }
174
+
49
- flowerset.cpp:22:30: error: no member named 'post' in 'Flower'; did you mean 'pos'?
175
+ bestPos=new double[dataset->exVarNum];
176
+
50
-
177
+ for(i=0;i<dataset->exVarNum;i++){
178
+
51
- bestPos[i]=flower[best]->post[i];
179
+ bestPos[i]=flower[best]->pos[i];
52
-
53
- ^~~~
180
+
54
-
55
- pos
181
+ }
56
-
57
- ./Flower.h:28:13: note: 'pos' declared here
182
+
58
-
59
- double *pos; //位置
183
+ bestValue=flower[best]->value;
60
-
61
- ^
62
-
63
- flowerset.cpp:25:17: error: must use 'class' tag to refer to type 'Flower' in this scope
64
184
 
65
185
  newFlower=new Flower(this);
66
186
 
67
- ^
68
-
69
- class
70
-
71
- flowerset.cpp:7:12: note: class 'Flower' is hidden by a non-type declaration of 'Flower' here
72
-
73
- FlowerSet::Flower(char *filename){
74
-
75
- ^
76
-
77
- flowerset.cpp:26:3: error: use of undeclared identifier 'trvlue'; did you mean 'trValue'?
78
-
79
- trvlue=new double[EBEE_NUM];
80
-
81
- ^~~~~~
82
-
83
- trValue
84
-
85
- ./Flowerset.h:25:11: note: 'trValue' declared here
187
+ trValue=new double[EBEE_NUM];
188
+
189
+ }
190
+
191
+
192
+
193
+ //デストラクタ
194
+
195
+ FlowerSet::~FlowerSet(){
196
+
197
+ int i;
198
+
199
+
200
+
201
+ for(i=0;i<EBEE_NUM;i++){
202
+
203
+ delete flower[i];
204
+
205
+ }
206
+
207
+ delete [] flower;
208
+
209
+ delete [] bestPos;
210
+
211
+ delete newFlower;
212
+
213
+ delete [] trValue;
214
+
215
+ delete dataset;
216
+
217
+
218
+
219
+ //収穫蜂フェーズを実行する
220
+
221
+ void FlowerSet::employedBeePhase()
222
+
223
+ {
224
+
225
+ int i;
226
+
227
+ Flower *tmp;
228
+
229
+
230
+
231
+ for(i=0;i<EBEE_NUM;i++){
232
+
233
+ newFlower->change(i);
234
+
235
+ if(flower[i]-> value > newFlower -> value){
236
+
237
+ tmp=newFlower;
238
+
239
+ newFlower=flower[i];
240
+
241
+ flower[i]=tmp;
242
+
243
+ }
244
+
245
+ flower[i]->visitNum++;
246
+
247
+ }
248
+
249
+ }
250
+
251
+ //追従蜂フェーズを実行する
252
+
253
+ void FlowerSet::onlookerBeePhase()
254
+
255
+ {
256
+
257
+ int i,j;
258
+
259
+ Flower *tmp;
260
+
261
+ double max,min,denom,prob,r;
262
+
263
+
264
+
265
+ for(j=0;j<OBEE_NUM;j++){
266
+
267
+ //評価値を変換する
268
+
269
+ max=DBL_MIN;
270
+
271
+ min=DBL_MAX;
272
+
273
+ for(i=0;i<EBEE_NUM;i++){
274
+
275
+ if(max<flower[i]->value){
276
+
277
+ max=flower[i]->value;
278
+
279
+ }
280
+
281
+ if(min<flower[i]->value){
282
+
283
+ min=flower[i]->value;
284
+
285
+ }
286
+
287
+ }
288
+
289
+ denom=0.0;
290
+
291
+ for(i=0;i<EBEE_NUM;i++){
292
+
293
+ trValue[i]=(max-flower[i]->value)/(max-min);
294
+
295
+ denom+=trValue[i];
296
+
297
+ }
298
+
299
+
300
+
301
+ //収穫蜂を選択する
302
+
303
+ r=RAND_01;
304
+
305
+ for(i=0;i<EBEE_NUM-1;i++){
306
+
307
+ prob=trValue[i]/denom;
308
+
309
+ if(r<=prob){
310
+
311
+ break;
312
+
313
+ }
314
+
315
+ r -=prob;
316
+
317
+ }
318
+
319
+
320
+
321
+ //収穫蜂フェーズと同様に処理する
322
+
323
+ newFlower->change(i);
324
+
325
+ if(flower[i]->value>newFlower->value){
326
+
327
+ tmp=newFlower;
328
+
329
+ newFlower=flower[i];
330
+
331
+ flower[i]=tmp;
332
+
333
+ }
334
+
335
+ flower[i]->visitNum++;
336
+
337
+ }
338
+
339
+ }
340
+
341
+
342
+
343
+ //偵察蜂フェーズを実行する
344
+
345
+ void FlowerSet::scoutBeePhase()
346
+
347
+ {
348
+
349
+ int i;
350
+
351
+ for (i=0;i<EBEE_NUM;i++){
352
+
353
+ if(VISIT_MAX<=flower[i]->visitNum){
354
+
355
+ flower[i]->renew();
356
+
357
+ }
358
+
359
+ }
360
+
361
+ }
362
+
363
+
364
+
365
+ //最良食糧源を記録する
366
+
367
+ void FlowerSet::saveBestPos()
368
+
369
+ {
370
+
371
+ int i,best;
372
+
373
+
374
+
375
+ best= -1;
376
+
377
+ for(i=0;i<EBEE_NUM;i++){
378
+
379
+ if(bestValue>flower[i]->value){
380
+
381
+ best=i;
382
+
383
+ }
384
+
385
+ }
386
+
387
+ if(best!=-1){
388
+
389
+ for(i=0;i<dataset->exVarNum;i++){
390
+
391
+ bestPos[i]=flower[best]->value;
392
+
393
+ }
394
+
395
+ bestValue=flower[best]->value;
396
+
397
+ }
398
+
399
+ }
400
+
401
+ //結果を表示する
402
+
403
+ void FlowerSet::printResult()
404
+
405
+ {
406
+
407
+ dataset->setCoef(bestPos);
408
+
409
+ dataset->printEquation();
410
+
411
+ }
412
+
413
+
414
+
415
+
416
+
417
+
418
+
419
+ ```
420
+
421
+
422
+
423
+ ```Flower
424
+
425
+ //Flower.h
426
+
427
+ #ifndef INCLUDED_FLOWER
428
+
429
+ #define INCLUDED_FLOWER
430
+
431
+ #include "DataSet.h"
432
+
433
+ #include "FlowerSet.h"
434
+
435
+ #pragma once
436
+
437
+ class FlowerSet;
438
+
439
+
440
+
441
+ //定数の定義
442
+
443
+ #define REPEAT_NUM 1000 //繰り返し数
444
+
445
+ #define EBEE_NUM 100 //収穫蜂の数
446
+
447
+ #define OBEE_NUM 10 //追従蜂の数
448
+
449
+ #define VISIT_MAX 10 //蜜の採取可能回数
450
+
451
+ #define COEF_MIN -1 //標準偏回帰係数の最小値
452
+
453
+ #define COEF_MAX 1 //標準偏回帰係数の最大値
454
+
455
+
456
+
457
+ //0以上1以下の実数乱数
458
+
459
+ #define RAND_01 ((double)rand() / RAND_MAX)
460
+
461
+
462
+
463
+ class Flower{
464
+
465
+ public:
466
+
467
+ Flower(FlowerSet *argFSet);
468
+
469
+ ~Flower();
470
+
471
+ void change(int base); //近くの食糧源に変更する
472
+
473
+ void renew();//新しい食糧源に変更する.
474
+
475
+
476
+
477
+ FlowerSet *fSet; //属している食糧源集合
478
+
479
+ double *pos; //位置
480
+
481
+ double value;//評価値
482
+
483
+ int visitNum;//蜜の採取回数
484
+
485
+
486
+
487
+ private:
488
+
489
+ void evaluate();//評価値を算出する
490
+
491
+ };
492
+
493
+ #endif
494
+
495
+ ```
496
+
497
+
498
+
499
+ ```Flower
500
+
501
+ //Flower.cpp
502
+
503
+ #include "Flower.h"
504
+
505
+ #pragma once
506
+
507
+
508
+
509
+ //コントラスた
510
+
511
+ //argFSet:属している食糧源集合
512
+
513
+
514
+
515
+ Flower::Flower(FlowerSet *argFSet){
516
+
517
+ int i;
518
+
519
+
520
+
521
+ fSet=argFSet;
522
+
523
+ pos=new double[fSet->dataset->exVarNum];
524
+
525
+ for(i=0;i<fSet->dataset->exVarNum;i++){
526
+
527
+ pos[i]=COEF_MIN+(COEF_MAX-COEF_MIN)*RAND_01;
528
+
529
+ }
530
+
531
+ visitNum=0;
532
+
533
+ evaluate();
534
+
535
+ }
536
+
537
+
538
+
539
+ //デストラクタ
540
+
541
+ Flower::~Flower(){
542
+
543
+ delete[]pos;
544
+
545
+ }
546
+
547
+ //baseの近くの食糧源に変更する
548
+
549
+ //base:元にする食糧源の添字
550
+
551
+ void Flower::change(int base){
552
+
553
+ int i,j;
554
+
555
+
556
+
557
+ for(i=0;i<fSet->dataset->exVarNum;i++){
558
+
559
+ pos[i]=fSet->flower[base]->pos[i];
560
+
561
+ }
562
+
563
+ i=rand() % fSet->dataset->exVarNum;
564
+
565
+ j=(base+(rand() % (EBEE_NUM-1)+1)) % EBEE_NUM;
566
+
567
+ pos[i]=pos[i]+(rand() / (RAND_MAX/2.0)-1)*(pos[i]-fSet->flower[j]->pos[i]);
568
+
569
+ visitNum=0;
570
+
571
+ evaluate();
572
+
573
+ }
574
+
575
+
576
+
577
+ //新しい食糧源に変更する
578
+
579
+ void Flower::renew(){
580
+
581
+ int i;
582
+
583
+ for(i=0;i<fSet->dataset->exVarNum;i++){
584
+
585
+ pos[i]=COEF_MIN+(COEF_MAX - COEF_MIN)*RAND_01;
586
+
587
+ }
588
+
589
+ visitNum=0;
590
+
591
+ evaluate();
592
+
593
+ }
594
+
595
+
596
+
597
+ //評価値を算出する
598
+
599
+ void Flower::evaluate(){
600
+
601
+ int i,j;
602
+
603
+ double diff;
604
+
605
+
606
+
607
+ value=0.0;
608
+
609
+ for(i=0;i<fSet->dataset->dataNum;i++){
610
+
611
+ diff=fSet->dataset->resSData[i];
612
+
613
+ for(j=0;j<fSet->dataset->exSData[i][j];j++){
614
+
615
+ diff -= pos[j]*fSet->dataset->exSData[i][j];
616
+
617
+ }
618
+
619
+ value += pow(diff,2.0);
620
+
621
+ }
622
+
623
+ }
624
+
625
+
626
+
627
+ ```
628
+
629
+
630
+
631
+ ```FlowerSet
632
+
633
+ //FlowerSet.h
634
+
635
+ #ifndef INCLUDED_FLOWERSET
636
+
637
+ #define INCLUDE_FLOWERSET
638
+
639
+ #include "Flower.h"
640
+
641
+ #pragma once
642
+
643
+ class Flower;
644
+
645
+
646
+
647
+ class FlowerSet
648
+
649
+ {
650
+
651
+ public:
652
+
653
+ FlowerSet(char *filename);
654
+
655
+ ~FlowerSet();
656
+
657
+ void employedBeePhase(); //収穫蜂フェーズを実行する
658
+
659
+ void onlockerBeePhase();//追従蜂フェーズを実行する
660
+
661
+ void scoutBeePhase();//偵察蜂フェーズを実行する.
662
+
663
+ void saveBestPos();//最良食糧源を記録する.
664
+
665
+ void printResult();//結果を表示する.
666
+
667
+
668
+
669
+ Dataset *dataset;//データセット
670
+
671
+ Flower **flower;//食糧源の集合のメンバ
672
+
673
+ double *bestPos;//最良食糧源の位置
674
+
675
+ double bestValue;//最良食糧源の評価値
676
+
677
+
678
+
679
+ private:
680
+
681
+ Flower *newFlower; //新しい食糧源
86
682
 
87
683
  double *trValue; //格食糧源の変換後の評価値
88
684
 
89
- ^
685
+ };
90
-
91
- flowerset.cpp:43:37: error: function definition is not allowed here
686
+
92
-
93
- void FlowerSet::employedBeePhase(){
687
+
94
-
95
- ^
688
+
96
-
97
- flowerset.cpp:58:37: error: function definition is not allowed here
98
-
99
- void FlowerSet::onlookerBeePhase(){
100
-
101
- ^
102
-
103
- flowerset.cpp:103:34: error: function definition is not allowed here
104
-
105
- void FlowerSet::scoutBeePhase(){
106
-
107
- ^
108
-
109
- flowerset.cpp:113:32: error: function definition is not allowed here
110
-
111
- void FlowerSet::saveBestPos(){
112
-
113
- ^
114
-
115
- flowerset.cpp:130:32: error: function definition is not allowed here
116
-
117
- void FlowerSet::printResult(){
118
-
119
- ^
120
-
121
- flowerset.cpp:135:3: error: expected '}'
122
-
123
-
124
-
125
- ^
126
-
127
- ./FlowerSet.h:7:7: error: redefinition of 'FlowerSet'
128
-
129
- class FlowerSet
689
+ #endif
690
+
691
+
130
692
 
131
693
 
132
694
 
133
695
  ```
134
696
 
135
-
136
-
137
- ### 該当のソースコード
138
-
139
-
140
-
141
- ```FlowerSet
142
-
143
- #include "FlowerSet.h"
144
-
145
- #pragma once
146
-
147
- #define filename data.txt
148
-
149
- //コントラスタ
150
-
151
- // filename: データセットのファイル名
152
-
153
- FlowerSet::Flower(char *filename){
154
-
155
- int i,best;
156
-
157
-
158
-
159
- dataset=new Dataset(filename);
160
-
161
- flower=new Flower* [EBEE_NUM];
162
-
163
- best=0;
164
-
165
-
166
-
167
- for(i=0;i<EBEE_NUM;i++){
168
-
169
- flower[i]=new Flower(this);
170
-
171
- if(flower[best]->value>flower[i]->value){
172
-
173
- best=i;
174
-
175
- }
176
-
177
- }
178
-
179
- bestPos=new double[dataset->exVarNum];
180
-
181
- for(i=0;i<dataset->exVarNum;i++){
182
-
183
- bestPos[i]=flower[best]->pos[i];
184
-
185
- }
186
-
187
- bestValue=flower[best]->value;
188
-
189
- newFlower=new Flower(this);
190
-
191
- trValue=new double[EBEE_NUM];
192
-
193
- }
194
-
195
-
196
-
197
- //デストラクタ
198
-
199
- FlowerSet::~FlowerSet(){
200
-
201
- int i;
202
-
203
-
204
-
205
- for(i=0;i<EBEE_NUM;i++){
206
-
207
- delete flower[i];
208
-
209
- }
210
-
211
- delete [] flower;
212
-
213
- delete [] bestPos;
214
-
215
- delete newFlower;
216
-
217
- delete [] trValue;
218
-
219
- delete dataset;
220
-
221
-
222
-
223
- //収穫蜂フェーズを実行する
224
-
225
- void FlowerSet::employedBeePhase()
226
-
227
- {
228
-
229
- int i;
230
-
231
- Flower *tmp;
232
-
233
-
234
-
235
- for(i=0;i<EBEE_NUM;i++){
236
-
237
- newFlower->change(i);
238
-
239
- if(flower[i]-> value > newFlower -> value){
240
-
241
- tmp=newFlower;
242
-
243
- newFlower=flower[i];
244
-
245
- flower[i]=tmp;
246
-
247
- }
248
-
249
- flower[i]->visitNum++;
250
-
251
- }
252
-
253
- }
254
-
255
- //追従蜂フェーズを実行する
256
-
257
- void FlowerSet::onlookerBeePhase()
258
-
259
- {
260
-
261
- int i,j;
262
-
263
- Flower *tmp;
264
-
265
- double max,min,denom,prob,r;
266
-
267
-
268
-
269
- for(j=0;j<OBEE_NUM;j++){
270
-
271
- //評価値を変換する
272
-
273
- max=DBL_MIN;
274
-
275
- min=DBL_MAX;
276
-
277
- for(i=0;i<EBEE_NUM;i++){
278
-
279
- if(max<flower[i]->value){
280
-
281
- max=flower[i]->value;
282
-
283
- }
284
-
285
- if(min<flower[i]->value){
286
-
287
- min=flower[i]->value;
288
-
289
- }
290
-
291
- }
292
-
293
- denom=0.0;
294
-
295
- for(i=0;i<EBEE_NUM;i++){
296
-
297
- trValue[i]=(max-flower[i]->value)/(max-min);
298
-
299
- denom+=trValue[i];
300
-
301
- }
302
-
303
-
304
-
305
- //収穫蜂を選択する
306
-
307
- r=RAND_01;
308
-
309
- for(i=0;i<EBEE_NUM-1;i++){
310
-
311
- prob=trValue[i]/denom;
312
-
313
- if(r<=prob){
314
-
315
- break;
316
-
317
- }
318
-
319
- r -=prob;
320
-
321
- }
322
-
323
-
324
-
325
- //収穫蜂フェーズと同様に処理する
326
-
327
- newFlower->change(i);
328
-
329
- if(flower[i]->value>newFlower->value){
330
-
331
- tmp=newFlower;
332
-
333
- newFlower=flower[i];
334
-
335
- flower[i]=tmp;
336
-
337
- }
338
-
339
- flower[i]->visitNum++;
340
-
341
- }
342
-
343
- }
344
-
345
-
346
-
347
- //偵察蜂フェーズを実行する
348
-
349
- void FlowerSet::scoutBeePhase()
350
-
351
- {
352
-
353
- int i;
354
-
355
- for (i=0;i<EBEE_NUM;i++){
356
-
357
- if(VISIT_MAX<=flower[i]->visitNum){
358
-
359
- flower[i]->renew();
360
-
361
- }
362
-
363
- }
364
-
365
- }
366
-
367
-
368
-
369
- //最良食糧源を記録する
370
-
371
- void FlowerSet::saveBestPos()
372
-
373
- {
374
-
375
- int i,best;
376
-
377
-
378
-
379
- best= -1;
380
-
381
- for(i=0;i<EBEE_NUM;i++){
382
-
383
- if(bestValue>flower[i]->value){
384
-
385
- best=i;
386
-
387
- }
388
-
389
- }
390
-
391
- if(best!=-1){
392
-
393
- for(i=0;i<dataset->exVarNum;i++){
394
-
395
- bestPos[i]=flower[best]->value;
396
-
397
- }
398
-
399
- bestValue=flower[best]->value;
400
-
401
- }
402
-
403
- }
404
-
405
- //結果を表示する
406
-
407
- void FlowerSet::printResult()
408
-
409
- {
410
-
411
- dataset->setCoef(bestPos);
412
-
413
- dataset->printEquation();
414
-
415
- }
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
- ```
424
-
425
-
426
-
427
- ```Flower
428
-
429
- //Flower.h
430
-
431
- #ifndef INCLUDED_FLOWER
432
-
433
- #define INCLUDED_FLOWER
434
-
435
- #include "DataSet.h"
436
-
437
- #include "FlowerSet.h"
438
-
439
- #pragma once
440
-
441
- class FlowerSet;
442
-
443
-
444
-
445
- //定数の定義
446
-
447
- #define REPEAT_NUM 1000 //繰り返し数
448
-
449
- #define EBEE_NUM 100 //収穫蜂の数
450
-
451
- #define OBEE_NUM 10 //追従蜂の数
452
-
453
- #define VISIT_MAX 10 //蜜の採取可能回数
454
-
455
- #define COEF_MIN -1 //標準偏回帰係数の最小値
456
-
457
- #define COEF_MAX 1 //標準偏回帰係数の最大値
458
-
459
-
460
-
461
- //0以上1以下の実数乱数
462
-
463
- #define RAND_01 ((double)rand() / RAND_MAX)
464
-
465
-
466
-
467
- class Flower{
468
-
469
- public:
470
-
471
- Flower(FlowerSet *argFSet);
472
-
473
- ~Flower();
474
-
475
- void change(int base); //近くの食糧源に変更する
476
-
477
- void renew();//新しい食糧源に変更する.
478
-
479
-
480
-
481
- FlowerSet *fSet; //属している食糧源集合
482
-
483
- double *pos; //位置
484
-
485
- double value;//評価値
486
-
487
- int visitNum;//蜜の採取回数
488
-
489
-
490
-
491
- private:
492
-
493
- void evaluate();//評価値を算出する
494
-
495
- };
496
-
497
- #endif
498
-
499
- ```
500
-
501
-
502
-
503
- ```Flower
504
-
505
- //Flower.cpp
506
-
507
- #include "Flower.h"
508
-
509
- #pragma once
510
-
511
-
512
-
513
- //コントラスた
514
-
515
- //argFSet:属している食糧源集合
516
-
517
-
518
-
519
- Flower::Flower(FlowerSet *argFSet){
520
-
521
- int i;
522
-
523
-
524
-
525
- fSet=argFSet;
526
-
527
- pos=new double[fSet->dataset->exVarNum];
528
-
529
- for(i=0;i<fSet->dataset->exVarNum;i++){
530
-
531
- pos[i]=COEF_MIN+(COEF_MAX-COEF_MIN)*RAND_01;
532
-
533
- }
534
-
535
- visitNum=0;
536
-
537
- evaluate();
538
-
539
- }
540
-
541
-
542
-
543
- //デストラクタ
544
-
545
- Flower::~Flower(){
546
-
547
- delete[]pos;
548
-
549
- }
550
-
551
- //baseの近くの食糧源に変更する
552
-
553
- //base:元にする食糧源の添字
554
-
555
- void Flower::change(int base){
556
-
557
- int i,j;
558
-
559
-
560
-
561
- for(i=0;i<fSet->dataset->exVarNum;i++){
562
-
563
- pos[i]=fSet->flower[base]->pos[i];
564
-
565
- }
566
-
567
- i=rand() % fSet->dataset->exVarNum;
568
-
569
- j=(base+(rand() % (EBEE_NUM-1)+1)) % EBEE_NUM;
570
-
571
- pos[i]=pos[i]+(rand() / (RAND_MAX/2.0)-1)*(pos[i]-fSet->flower[j]->pos[i]);
572
-
573
- visitNum=0;
574
-
575
- evaluate();
576
-
577
- }
578
-
579
-
580
-
581
- //新しい食糧源に変更する
582
-
583
- void Flower::renew(){
584
-
585
- int i;
586
-
587
- for(i=0;i<fSet->dataset->exVarNum;i++){
588
-
589
- pos[i]=COEF_MIN+(COEF_MAX - COEF_MIN)*RAND_01;
590
-
591
- }
592
-
593
- visitNum=0;
594
-
595
- evaluate();
596
-
597
- }
598
-
599
-
600
-
601
- //評価値を算出する
602
-
603
- void Flower::evaluate(){
604
-
605
- int i,j;
606
-
607
- double diff;
608
-
609
-
610
-
611
- value=0.0;
612
-
613
- for(i=0;i<fSet->dataset->dataNum;i++){
614
-
615
- diff=fSet->dataset->resSData[i];
616
-
617
- for(j=0;j<fSet->dataset->exSData[i][j];j++){
618
-
619
- diff -= pos[j]*fSet->dataset->exSData[i][j];
620
-
621
- }
622
-
623
- value += pow(diff,2.0);
624
-
625
- }
626
-
627
- }
628
-
629
-
630
-
631
- ```
632
-
633
-
634
-
635
- ```FlowerSet
636
-
637
- //FlowerSet.h
638
-
639
- #ifndef INCLUDED_FLOWERSET
640
-
641
- #define INCLUDE_FLOWERSET
642
-
643
- #include "Flower.h"
644
-
645
- #pragma once
646
-
647
- class Flower;
648
-
649
-
650
-
651
- class FlowerSet
652
-
653
- {
654
-
655
- public:
656
-
657
- FlowerSet(char *filename);
658
-
659
- ~FlowerSet();
660
-
661
- void employedBeePhase(); //収穫蜂フェーズを実行する
662
-
663
- void onlockerBeePhase();//追従蜂フェーズを実行する
664
-
665
- void scoutBeePhase();//偵察蜂フェーズを実行する.
666
-
667
- void saveBestPos();//最良食糧源を記録する.
668
-
669
- void printResult();//結果を表示する.
670
-
671
-
672
-
673
- Dataset *dataset;//データセット
674
-
675
- Flower **flower;//食糧源の集合のメンバ
676
-
677
- double *bestPos;//最良食糧源の位置
678
-
679
- double bestValue;//最良食糧源の評価値
680
-
681
-
682
-
683
- private:
684
-
685
- Flower *newFlower; //新しい食糧源
686
-
687
- double *trValue; //格食糧源の変換後の評価値
688
-
689
- };
690
-
691
-
692
-
693
- #endif
694
-
695
-
696
-
697
-
698
-
699
- ```
700
-
701
697
  ### 試したこと
702
698
 
703
699
 

1

FlowerSet.h Flower.h Flower.cppを追加しました

2021/07/19 04:39

投稿

RyoBaya
RyoBaya

スコア1

test CHANGED
File without changes
test CHANGED
@@ -424,6 +424,280 @@
424
424
 
425
425
 
426
426
 
427
+ ```Flower
428
+
429
+ //Flower.h
430
+
431
+ #ifndef INCLUDED_FLOWER
432
+
433
+ #define INCLUDED_FLOWER
434
+
435
+ #include "DataSet.h"
436
+
437
+ #include "FlowerSet.h"
438
+
439
+ #pragma once
440
+
441
+ class FlowerSet;
442
+
443
+
444
+
445
+ //定数の定義
446
+
447
+ #define REPEAT_NUM 1000 //繰り返し数
448
+
449
+ #define EBEE_NUM 100 //収穫蜂の数
450
+
451
+ #define OBEE_NUM 10 //追従蜂の数
452
+
453
+ #define VISIT_MAX 10 //蜜の採取可能回数
454
+
455
+ #define COEF_MIN -1 //標準偏回帰係数の最小値
456
+
457
+ #define COEF_MAX 1 //標準偏回帰係数の最大値
458
+
459
+
460
+
461
+ //0以上1以下の実数乱数
462
+
463
+ #define RAND_01 ((double)rand() / RAND_MAX)
464
+
465
+
466
+
467
+ class Flower{
468
+
469
+ public:
470
+
471
+ Flower(FlowerSet *argFSet);
472
+
473
+ ~Flower();
474
+
475
+ void change(int base); //近くの食糧源に変更する
476
+
477
+ void renew();//新しい食糧源に変更する.
478
+
479
+
480
+
481
+ FlowerSet *fSet; //属している食糧源集合
482
+
483
+ double *pos; //位置
484
+
485
+ double value;//評価値
486
+
487
+ int visitNum;//蜜の採取回数
488
+
489
+
490
+
491
+ private:
492
+
493
+ void evaluate();//評価値を算出する
494
+
495
+ };
496
+
497
+ #endif
498
+
499
+ ```
500
+
501
+
502
+
503
+ ```Flower
504
+
505
+ //Flower.cpp
506
+
507
+ #include "Flower.h"
508
+
509
+ #pragma once
510
+
511
+
512
+
513
+ //コントラスた
514
+
515
+ //argFSet:属している食糧源集合
516
+
517
+
518
+
519
+ Flower::Flower(FlowerSet *argFSet){
520
+
521
+ int i;
522
+
523
+
524
+
525
+ fSet=argFSet;
526
+
527
+ pos=new double[fSet->dataset->exVarNum];
528
+
529
+ for(i=0;i<fSet->dataset->exVarNum;i++){
530
+
531
+ pos[i]=COEF_MIN+(COEF_MAX-COEF_MIN)*RAND_01;
532
+
533
+ }
534
+
535
+ visitNum=0;
536
+
537
+ evaluate();
538
+
539
+ }
540
+
541
+
542
+
543
+ //デストラクタ
544
+
545
+ Flower::~Flower(){
546
+
547
+ delete[]pos;
548
+
549
+ }
550
+
551
+ //baseの近くの食糧源に変更する
552
+
553
+ //base:元にする食糧源の添字
554
+
555
+ void Flower::change(int base){
556
+
557
+ int i,j;
558
+
559
+
560
+
561
+ for(i=0;i<fSet->dataset->exVarNum;i++){
562
+
563
+ pos[i]=fSet->flower[base]->pos[i];
564
+
565
+ }
566
+
567
+ i=rand() % fSet->dataset->exVarNum;
568
+
569
+ j=(base+(rand() % (EBEE_NUM-1)+1)) % EBEE_NUM;
570
+
571
+ pos[i]=pos[i]+(rand() / (RAND_MAX/2.0)-1)*(pos[i]-fSet->flower[j]->pos[i]);
572
+
573
+ visitNum=0;
574
+
575
+ evaluate();
576
+
577
+ }
578
+
579
+
580
+
581
+ //新しい食糧源に変更する
582
+
583
+ void Flower::renew(){
584
+
585
+ int i;
586
+
587
+ for(i=0;i<fSet->dataset->exVarNum;i++){
588
+
589
+ pos[i]=COEF_MIN+(COEF_MAX - COEF_MIN)*RAND_01;
590
+
591
+ }
592
+
593
+ visitNum=0;
594
+
595
+ evaluate();
596
+
597
+ }
598
+
599
+
600
+
601
+ //評価値を算出する
602
+
603
+ void Flower::evaluate(){
604
+
605
+ int i,j;
606
+
607
+ double diff;
608
+
609
+
610
+
611
+ value=0.0;
612
+
613
+ for(i=0;i<fSet->dataset->dataNum;i++){
614
+
615
+ diff=fSet->dataset->resSData[i];
616
+
617
+ for(j=0;j<fSet->dataset->exSData[i][j];j++){
618
+
619
+ diff -= pos[j]*fSet->dataset->exSData[i][j];
620
+
621
+ }
622
+
623
+ value += pow(diff,2.0);
624
+
625
+ }
626
+
627
+ }
628
+
629
+
630
+
631
+ ```
632
+
633
+
634
+
635
+ ```FlowerSet
636
+
637
+ //FlowerSet.h
638
+
639
+ #ifndef INCLUDED_FLOWERSET
640
+
641
+ #define INCLUDE_FLOWERSET
642
+
643
+ #include "Flower.h"
644
+
645
+ #pragma once
646
+
647
+ class Flower;
648
+
649
+
650
+
651
+ class FlowerSet
652
+
653
+ {
654
+
655
+ public:
656
+
657
+ FlowerSet(char *filename);
658
+
659
+ ~FlowerSet();
660
+
661
+ void employedBeePhase(); //収穫蜂フェーズを実行する
662
+
663
+ void onlockerBeePhase();//追従蜂フェーズを実行する
664
+
665
+ void scoutBeePhase();//偵察蜂フェーズを実行する.
666
+
667
+ void saveBestPos();//最良食糧源を記録する.
668
+
669
+ void printResult();//結果を表示する.
670
+
671
+
672
+
673
+ Dataset *dataset;//データセット
674
+
675
+ Flower **flower;//食糧源の集合のメンバ
676
+
677
+ double *bestPos;//最良食糧源の位置
678
+
679
+ double bestValue;//最良食糧源の評価値
680
+
681
+
682
+
683
+ private:
684
+
685
+ Flower *newFlower; //新しい食糧源
686
+
687
+ double *trValue; //格食糧源の変換後の評価値
688
+
689
+ };
690
+
691
+
692
+
693
+ #endif
694
+
695
+
696
+
697
+
698
+
699
+ ```
700
+
427
701
  ### 試したこと
428
702
 
429
703