質問編集履歴
2
文法の修正とprintfの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -44,109 +44,225 @@
|
|
44
44
|
|
45
45
|
|
46
46
|
|
47
|
+
#include "sub.h"
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
double pheromone_0;
|
52
|
+
|
53
|
+
double alpha;
|
54
|
+
|
55
|
+
double beta;
|
56
|
+
|
57
|
+
double rho;
|
58
|
+
|
59
|
+
double q_0;
|
60
|
+
|
61
|
+
int n;
|
62
|
+
|
63
|
+
int nant;
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
void erace(void){
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
alpha=1.0;
|
72
|
+
|
73
|
+
beta=1.0; //ヒューリスティック情報の優先度
|
74
|
+
|
75
|
+
rho=0.5; //フェロモン蒸発率
|
76
|
+
|
77
|
+
q_0=0;
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
pheromone_0=1.0;
|
82
|
+
|
83
|
+
printf("pheromone_0;%f\n",pheromone_0);
|
84
|
+
|
85
|
+
initial_pheromone( pheromone_0);
|
86
|
+
|
87
|
+
printf("%f\n",pheromone[0][0]);
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
int main(int argc, char *argv[]){
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
int i;
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
n=51;
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
nant=10; //アリの数
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
start_program();
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
erace();
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
for(i=0;i<n;i++){
|
130
|
+
|
131
|
+
free(pheromone[i]);
|
132
|
+
|
133
|
+
free(total[i]);
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
free(total);
|
138
|
+
|
139
|
+
free(pheromone);
|
140
|
+
|
141
|
+
return(0);
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
```
|
146
|
+
|
147
|
+
```
|
148
|
+
|
149
|
+
sub.c
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
#include <stdio.h>
|
154
|
+
|
155
|
+
#include <stdlib.h>
|
156
|
+
|
157
|
+
#include <math.h>
|
158
|
+
|
159
|
+
#include <time.h>
|
160
|
+
|
161
|
+
#include <limits.h>
|
162
|
+
|
163
|
+
|
164
|
+
|
47
165
|
#include "tesb.h"
|
48
166
|
|
49
167
|
|
50
168
|
|
169
|
+
ant_struct *ant;
|
170
|
+
|
171
|
+
ant_struct *bestant;
|
172
|
+
|
173
|
+
|
174
|
+
|
51
|
-
double pheromone
|
175
|
+
double **pheromone;
|
52
|
-
|
53
|
-
|
176
|
+
|
54
|
-
|
55
|
-
|
177
|
+
double **total;
|
56
|
-
|
57
|
-
|
178
|
+
|
58
|
-
|
59
|
-
|
179
|
+
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
180
|
+
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
alpha=1.0;
|
68
|
-
|
69
|
-
beta=1.0; //ヒューリスティック情報の優先度
|
70
|
-
|
71
|
-
r
|
181
|
+
void initial_pheromone(double initial_trail) //フェロモンの初期化
|
182
|
+
|
183
|
+
|
184
|
+
|
72
|
-
|
185
|
+
{
|
186
|
+
|
73
|
-
|
187
|
+
long int i, j;
|
188
|
+
|
189
|
+
|
190
|
+
|
74
|
-
|
191
|
+
for ( i = 0 ; i < n ; i++ ) {
|
192
|
+
|
75
|
-
|
193
|
+
for ( j =0 ; j <=i ; j++ ) {
|
76
|
-
|
194
|
+
|
77
|
-
pheromone_
|
195
|
+
pheromone[i][j] = initial_trail;
|
78
|
-
|
79
|
-
|
196
|
+
|
80
|
-
|
81
|
-
|
197
|
+
pheromone[j][i]=pheromone[i][j];
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
82
204
|
|
83
205
|
}
|
84
206
|
|
85
207
|
|
86
208
|
|
209
|
+
|
210
|
+
|
87
|
-
i
|
211
|
+
void start_program(void)
|
212
|
+
|
88
|
-
|
213
|
+
{
|
89
|
-
|
90
|
-
|
214
|
+
|
91
|
-
int i;
|
215
|
+
int i,j;
|
92
|
-
|
216
|
+
|
93
|
-
int n
|
217
|
+
if((ant=malloc(sizeof(ant_struct)*nant+sizeof(ant_struct *)*nant))==NULL){
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
218
|
+
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
n
|
219
|
+
printf("out of memory,exit");
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
220
|
+
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
e
|
221
|
+
exit(1);
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
free(bestant->tour);
|
124
|
-
|
125
|
-
free(bestant->visited);
|
126
|
-
|
127
|
-
for(i=0;i<nant;i++){
|
128
|
-
|
129
|
-
free(ant[i].tour);
|
130
|
-
|
131
|
-
free(ant[i].visited);
|
132
222
|
|
133
223
|
}
|
134
224
|
|
225
|
+
|
226
|
+
|
135
|
-
for(i=0;i<n;i++){
|
227
|
+
for(i=0;i<nant;i++){
|
228
|
+
|
136
|
-
|
229
|
+
ant[i].tour=calloc(n+1,sizeof(int));
|
230
|
+
|
231
|
+
ant[i].visited=calloc(n,sizeof(char));
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
if((bestant=malloc(sizeof(ant_struct)))==NULL){
|
236
|
+
|
137
|
-
f
|
237
|
+
printf("out of memory,exit");
|
138
|
-
|
238
|
+
|
139
|
-
|
239
|
+
exit(1);
|
140
240
|
|
141
241
|
}
|
142
242
|
|
243
|
+
bestant->tour=calloc(n+1,sizeof(int));
|
244
|
+
|
245
|
+
bestant->visited=calloc(n,sizeof(char));
|
246
|
+
|
247
|
+
|
248
|
+
|
143
|
-
|
249
|
+
pheromone=malloc(sizeof(double *)*n);
|
144
|
-
|
250
|
+
|
145
|
-
fr
|
251
|
+
for(i=0;i<n;i++){
|
252
|
+
|
146
|
-
|
253
|
+
pheromone[i]=malloc(sizeof(double*)*n);
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
total=malloc(sizeof(double *)*n);
|
260
|
+
|
147
|
-
|
261
|
+
for(i=0;i<n;i++){
|
148
|
-
|
262
|
+
|
149
|
-
|
263
|
+
total[i]=malloc(sizeof(double*)*n);
|
264
|
+
|
265
|
+
}
|
150
266
|
|
151
267
|
}
|
152
268
|
|
@@ -154,130 +270,6 @@
|
|
154
270
|
|
155
271
|
```
|
156
272
|
|
157
|
-
sub.c
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
#include <stdio.h>
|
162
|
-
|
163
|
-
#include <stdlib.h>
|
164
|
-
|
165
|
-
#include <math.h>
|
166
|
-
|
167
|
-
#include <time.h>
|
168
|
-
|
169
|
-
#include <limits.h>
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
#include "tesb.h"
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
ant_struct *ant;
|
178
|
-
|
179
|
-
ant_struct *bestant;
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
double **pheromone;
|
184
|
-
|
185
|
-
double **total;
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
void initial_pheromone(double initial_trail) //フェロモンの初期化
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
{
|
194
|
-
|
195
|
-
long int i, j;
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
for ( i = 0 ; i < n ; i++ ) {
|
200
|
-
|
201
|
-
for ( j =0 ; j <=i ; j++ ) {
|
202
|
-
|
203
|
-
pheromone[i][j] = initial_trail;
|
204
|
-
|
205
|
-
pheromone[j][i]=pheromone[i][j];
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
}
|
210
|
-
|
211
|
-
}
|
212
|
-
|
213
|
-
}
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
void start_program(void)
|
220
|
-
|
221
|
-
{
|
222
|
-
|
223
|
-
int i,j;
|
224
|
-
|
225
|
-
if((ant=malloc(sizeof(ant_struct)*nant+sizeof(ant_struct *)*nant))==NULL){
|
226
|
-
|
227
|
-
printf("out of memory,exit");
|
228
|
-
|
229
|
-
exit(1);
|
230
|
-
|
231
|
-
}
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
for(i=0;i<nant;i++){
|
236
|
-
|
237
|
-
ant[i].tour=calloc(n+1,sizeof(int));
|
238
|
-
|
239
|
-
ant[i].visited=calloc(n,sizeof(char));
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
if((bestant=malloc(sizeof(ant_struct)))==NULL){
|
244
|
-
|
245
|
-
printf("out of memory,exit");
|
246
|
-
|
247
|
-
exit(1);
|
248
|
-
|
249
|
-
}
|
250
|
-
|
251
|
-
bestant->tour=calloc(n+1,sizeof(int));
|
252
|
-
|
253
|
-
bestant->visited=calloc(n,sizeof(char));
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
pheromone=malloc(sizeof(double *)*n);
|
258
|
-
|
259
|
-
for(i=0;i<n;i++){
|
260
|
-
|
261
|
-
pheromone[i]=malloc(sizeof(double*)*n);
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
total=malloc(sizeof(double *)*n);
|
268
|
-
|
269
|
-
for(i=0;i<n;i++){
|
270
|
-
|
271
|
-
total[i]=malloc(sizeof(double*)*n);
|
272
|
-
|
273
|
-
}
|
274
|
-
|
275
|
-
}
|
276
|
-
|
277
|
-
```
|
278
|
-
|
279
|
-
```
|
280
|
-
|
281
273
|
ファイル sub.h
|
282
274
|
|
283
275
|
typedef struct {
|
1
誤字などの修正、intをdoubleに変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -28,13 +28,11 @@
|
|
28
28
|
|
29
29
|
ファイル as.c
|
30
30
|
|
31
|
-
|
32
|
-
|
33
31
|
#include <stdio.h>
|
34
32
|
|
35
33
|
#include <math.h>
|
36
34
|
|
37
|
-
#include <limits.h>
|
35
|
+
#include <limits.h>
|
38
36
|
|
39
37
|
#include <assert.h>
|
40
38
|
|
@@ -46,7 +44,7 @@
|
|
46
44
|
|
47
45
|
|
48
46
|
|
49
|
-
#include "s
|
47
|
+
#include "tesb.h"
|
50
48
|
|
51
49
|
|
52
50
|
|
@@ -74,9 +72,9 @@
|
|
74
72
|
|
75
73
|
q_0=0;
|
76
74
|
|
77
|
-
|
75
|
+
|
78
|
-
|
76
|
+
|
79
|
-
pheromone_0=1
|
77
|
+
pheromone_0=1.0;
|
80
78
|
|
81
79
|
printf("%f\n",pheromone_0);
|
82
80
|
|
@@ -86,7 +84,7 @@
|
|
86
84
|
|
87
85
|
|
88
86
|
|
89
|
-
i
|
87
|
+
int main(int argc, char *argv[]){
|
90
88
|
|
91
89
|
|
92
90
|
|
@@ -106,8 +104,6 @@
|
|
106
104
|
|
107
105
|
|
108
106
|
|
109
|
-
scanf("%\n",&optimal);
|
110
|
-
|
111
107
|
start_program();
|
112
108
|
|
113
109
|
|
@@ -116,9 +112,9 @@
|
|
116
112
|
|
117
113
|
|
118
114
|
|
119
|
-
|
120
|
-
|
121
|
-
|
115
|
+
|
116
|
+
|
117
|
+
|
122
118
|
|
123
119
|
|
124
120
|
|
@@ -174,7 +170,7 @@
|
|
174
170
|
|
175
171
|
|
176
172
|
|
177
|
-
#include "s
|
173
|
+
#include "tesb.h"
|
178
174
|
|
179
175
|
|
180
176
|
|
@@ -198,27 +194,25 @@
|
|
198
194
|
|
199
195
|
long int i, j;
|
200
196
|
|
201
|
-
|
197
|
+
|
202
198
|
|
203
199
|
for ( i = 0 ; i < n ; i++ ) {
|
204
200
|
|
205
|
-
|
201
|
+
for ( j =0 ; j <=i ; j++ ) {
|
206
|
-
|
202
|
+
|
207
|
-
|
203
|
+
pheromone[i][j] = initial_trail;
|
208
|
-
|
204
|
+
|
209
|
-
|
205
|
+
pheromone[j][i]=pheromone[i][j];
|
210
|
-
|
211
|
-
|
206
|
+
|
212
|
-
|
213
|
-
|
207
|
+
|
214
|
-
|
215
|
-
}
|
216
208
|
|
217
209
|
}
|
218
210
|
|
211
|
+
}
|
212
|
+
|
219
213
|
}
|
220
214
|
|
221
|
-
|
215
|
+
|
222
216
|
|
223
217
|
|
224
218
|
|
@@ -236,7 +230,7 @@
|
|
236
230
|
|
237
231
|
}
|
238
232
|
|
239
|
-
|
233
|
+
|
240
234
|
|
241
235
|
for(i=0;i<nant;i++){
|
242
236
|
|
@@ -258,23 +252,23 @@
|
|
258
252
|
|
259
253
|
bestant->visited=calloc(n,sizeof(char));
|
260
254
|
|
261
|
-
|
262
|
-
|
255
|
+
|
256
|
+
|
263
|
-
pheromone=malloc(sizeof(
|
257
|
+
pheromone=malloc(sizeof(double *)*n);
|
264
258
|
|
265
259
|
for(i=0;i<n;i++){
|
266
260
|
|
267
|
-
pheromone[i]=malloc(sizeof(
|
261
|
+
pheromone[i]=malloc(sizeof(double*)*n);
|
268
262
|
|
269
263
|
}
|
270
264
|
|
271
265
|
|
272
266
|
|
273
|
-
total=malloc(sizeof(
|
267
|
+
total=malloc(sizeof(double *)*n);
|
274
268
|
|
275
269
|
for(i=0;i<n;i++){
|
276
270
|
|
277
|
-
total[i]=malloc(sizeof(
|
271
|
+
total[i]=malloc(sizeof(double*)*n);
|
278
272
|
|
279
273
|
}
|
280
274
|
|
@@ -284,7 +278,7 @@
|
|
284
278
|
|
285
279
|
```
|
286
280
|
|
287
|
-
ファイル
|
281
|
+
ファイル sub.h
|
288
282
|
|
289
283
|
typedef struct {
|
290
284
|
|
@@ -358,9 +352,7 @@
|
|
358
352
|
|
359
353
|
extern double **pheromone;
|
360
354
|
|
361
|
-
|
355
|
+
extern double **total;
|
362
|
-
|
363
|
-
|
364
356
|
|
365
357
|
|
366
358
|
|
@@ -369,3 +361,5 @@
|
|
369
361
|
### 試したこと
|
370
362
|
|
371
363
|
配列が問題なのかと思って配列を表示してみたりしたのですが配列のメモリもしっかり用意できていました。
|
364
|
+
|
365
|
+
修正してみたのですがまだエラーがでてしまいます。明日またmallocを使わない方法でやってみようと思います
|