質問編集履歴
3
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,326 +1,4 @@
|
|
1
|
-
```java
|
2
|
-
|
3
|
-
package testtt;
|
4
|
-
|
5
|
-
import ilog.concert.IloException;
|
6
|
-
|
7
|
-
import ilog.concert.IloIntVar;
|
8
|
-
|
9
|
-
import ilog.concert.IloLinearNumExpr;
|
10
|
-
|
11
|
-
import ilog.cplex.IloCplex;
|
12
|
-
|
13
|
-
public class test2 {
|
14
|
-
|
15
|
-
public static void main(String[] args) {
|
16
|
-
|
17
|
-
try {
|
18
|
-
|
19
|
-
IloCplex numberlink = new IloCplex();
|
20
|
-
|
21
|
-
int row=4;
|
22
|
-
|
23
|
-
int column=4;
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
IloIntVar s[][] = new IloIntVar[row][column];
|
28
|
-
|
29
|
-
for(int i=1;i<=row-1;i++) {
|
30
|
-
|
31
|
-
for(int j=1;j<=column;j++) {
|
32
|
-
|
33
|
-
s[i][j] = numberlink.boolVar();
|
34
|
-
|
35
|
-
}
|
36
|
-
|
37
|
-
}
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
IloIntVar e[][] = new IloIntVar[row][column];
|
42
|
-
|
43
|
-
for(int i=1;i<=row;i++) {
|
44
|
-
|
45
|
-
for(int j=1;j<=column-1;j++) {
|
46
|
-
|
47
|
-
e[i][j] = numberlink.boolVar();
|
48
|
-
|
49
|
-
}
|
50
|
-
|
51
|
-
}
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
IloLinearNumExpr NumberMath = numberlink.linearNumExpr();
|
56
|
-
|
57
|
-
IloIntVar[][] x = new IloIntVar[row][column];
|
58
|
-
|
59
|
-
for(int i=1;i<=row;i++) {
|
60
|
-
|
61
|
-
for(int j=1;j<=column;j++) {
|
62
|
-
|
63
|
-
NumberMath.addTerm(1, x[i][j]);
|
64
|
-
|
65
|
-
numberlink.addLe(NumberMath,3); //数字マスは3以下(要変更)
|
66
|
-
|
67
|
-
numberlink.addGe(NumberMath, 1); //数字マスは1以上(要変更)
|
68
|
-
|
69
|
-
}
|
70
|
-
|
71
|
-
}
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
IloLinearNumExpr NumberMath1_a = numberlink.linearNumExpr();
|
76
|
-
|
77
|
-
NumberMath1_a.addTerm(1,x[1][1]);
|
78
|
-
|
79
|
-
numberlink.addEq(NumberMath1_a,1);
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
IloLinearNumExpr NumberMath1_b = numberlink.linearNumExpr();
|
84
|
-
|
85
|
-
NumberMath1_b.addTerm(1,x[2][3]);
|
86
|
-
|
87
|
-
numberlink.addEq(NumberMath1_b,1);
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
IloLinearNumExpr NumberMath2_a = numberlink.linearNumExpr();
|
94
|
-
|
95
|
-
NumberMath2_a.addTerm(1,x[1][4]);
|
96
|
-
|
97
|
-
numberlink.addEq(NumberMath2_a,2);
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
IloLinearNumExpr NumberMath2_b = numberlink.linearNumExpr();
|
102
|
-
|
103
|
-
NumberMath2_b.addTerm(1,x[3][2]);
|
104
|
-
|
105
|
-
numberlink.addEq(NumberMath2_b,2);
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
IloLinearNumExpr NumberMath3_a = numberlink.linearNumExpr();
|
112
|
-
|
113
|
-
NumberMath3_a.addTerm(1,x[2][2]);
|
114
|
-
|
115
|
-
numberlink.addEq(NumberMath3_a,3);
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
IloLinearNumExpr NumberMath3_b = numberlink.linearNumExpr();
|
120
|
-
|
121
|
-
NumberMath3_b.addTerm(1,x[4][4]);
|
122
|
-
|
123
|
-
numberlink.addEq(NumberMath3_b,3);
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
IloLinearNumExpr MathLine = numberlink.linearNumExpr();
|
132
|
-
|
133
|
-
for(int i=1;i<=row;i++) {
|
134
|
-
|
135
|
-
for(int j=1;j<=column;j++) {
|
136
|
-
|
137
|
-
MathLine.addTerm(1, s[i-1][j]);
|
138
|
-
|
139
|
-
MathLine.addTerm(1, s[i][j]);
|
140
|
-
|
141
|
-
MathLine.addTerm(1, e[i][j-1]);
|
142
|
-
|
143
|
-
MathLine.addTerm(1, e[i][j]);
|
144
|
-
|
145
|
-
if(i-1==0) {
|
146
|
-
|
147
|
-
MathLine.addTerm(-1, s[i-1][j]);
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
if(i==4) {
|
152
|
-
|
153
|
-
MathLine.addTerm(-1, s[i][j]);
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
if(j-1==0) {
|
158
|
-
|
159
|
-
MathLine.addTerm(-1, e[i][j-1]);
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
if(j==4) {
|
164
|
-
|
165
|
-
MathLine.addTerm(-1, e[i][j]);
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
if((i==1&&j==1)||(i==1&&j==4)||(i==2&&j==2)||(i==2&&j==3)||
|
170
|
-
|
171
|
-
(i==3&&j==2)||i==4&&j==4) {
|
172
|
-
|
173
|
-
numberlink.addEq(MathLine, 1);
|
174
|
-
|
175
|
-
}
|
176
|
-
|
177
|
-
else {numberlink.addGe(MathLine, 2);
|
178
|
-
|
179
|
-
numberlink.addLe(MathLine, 0);}
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
}
|
184
|
-
|
185
|
-
}
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
IloLinearNumExpr IfsEq1_1 = numberlink.linearNumExpr();
|
192
|
-
|
193
|
-
IloLinearNumExpr IfsEq1_2 = numberlink.linearNumExpr();
|
194
|
-
|
195
|
-
for(int i=1;i<=row-1;i++) {
|
196
|
-
|
197
|
-
for(int j=1;j<=column;j++) {
|
198
|
-
|
199
|
-
IfsEq1_1.addTerm(3,s[i][j]);
|
200
|
-
|
201
|
-
IfsEq1_1.addTerm(-1,x[i][j]);
|
202
|
-
|
203
|
-
IfsEq1_1.addTerm(1,x[i+1][j]);
|
204
|
-
|
205
|
-
numberlink.addLe(IfsEq1_1,3);
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
IfsEq1_2.addTerm(3,s[i][j]);
|
210
|
-
|
211
|
-
IfsEq1_2.addTerm(1,x[i][j]);
|
212
|
-
|
213
|
-
IfsEq1_2.addTerm(-1,x[i+1][j]);
|
214
|
-
|
215
|
-
numberlink.addLe(IfsEq1_1,3);
|
216
|
-
|
217
|
-
}
|
218
|
-
|
219
|
-
}
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
IloLinearNumExpr IfeEq1_1 = numberlink.linearNumExpr();
|
226
|
-
|
227
|
-
IloLinearNumExpr IfeEq1_2 = numberlink.linearNumExpr();
|
228
|
-
|
229
|
-
for(int i=1;i<=row;i++) {
|
230
|
-
|
231
|
-
for(int j=1;j<=column-1;j++) {
|
232
|
-
|
233
|
-
IfeEq1_1.addTerm(3,e[i][j]);
|
234
|
-
|
235
|
-
IfeEq1_1.addTerm(-1,x[i][j]);
|
236
|
-
|
237
|
-
IfeEq1_1.addTerm(1,x[i+1][j]);
|
238
|
-
|
239
|
-
numberlink.addLe(IfeEq1_1,3);
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
IfeEq1_2.addTerm(3,e[i][j]);
|
244
|
-
|
245
|
-
IfeEq1_2.addTerm(1,x[i][j]);
|
246
|
-
|
247
|
-
IfeEq1_2.addTerm(-1,x[i+1][j]);
|
248
|
-
|
249
|
-
numberlink.addLe(IfeEq1_1,3);
|
250
|
-
|
251
|
-
}
|
252
|
-
|
253
|
-
}
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
IloLinearNumExpr AllLine = numberlink.linearNumExpr();
|
260
|
-
|
261
|
-
for(int i=1;i<=row;i++) {
|
262
|
-
|
263
|
-
for(int j=1;j<=column-1;j++) {
|
264
|
-
|
265
|
-
AllLine.addTerm(1,e[i][j]);
|
266
|
-
|
267
|
-
}
|
268
|
-
|
269
|
-
}
|
270
|
-
|
271
|
-
for(int i=1;i<=row-1;i++) {
|
272
|
-
|
273
|
-
for(int j=1;j<=column;j++) {
|
274
|
-
|
275
|
-
AllLine.addTerm(1,s[i][j]);
|
276
|
-
|
277
|
-
}
|
278
|
-
|
279
|
-
}
|
280
|
-
|
281
|
-
numberlink.addMinimize(AllLine);
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
if(numberlink.solve()) {
|
288
|
-
|
289
|
-
System.out.println("目的関数値は"+numberlink.getObjValue());
|
290
|
-
|
291
|
-
for(int i=1;i<=row;i++) {
|
292
|
-
|
293
|
-
for(int j=1;j<=column;j++) {
|
294
|
-
|
295
|
-
System.out.println(s[i][j]+"="+numberlink.getValue(s[i][j]));
|
296
|
-
|
297
|
-
System.out.println(e[i][j]+"="+numberlink.getValue(e[i][j]));
|
298
|
-
|
299
|
-
System.out.println(x[i][j]+"="+numberlink.getValue(x[i][j]));
|
300
|
-
|
301
|
-
}
|
302
|
-
|
303
|
-
}
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
}
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
}
|
312
|
-
|
313
|
-
catch(IloException e) {
|
314
|
-
|
315
|
-
}
|
316
|
-
|
317
|
-
}
|
318
|
-
|
319
|
-
}
|
320
|
-
|
321
|
-
```
|
322
|
-
|
323
|
-
|
1
|
+
コードをEclipseで実行したところ、
|
324
2
|
|
325
3
|
|
326
4
|
|
2
削除された内容の復元を行いました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,326 @@
|
|
1
|
+
```java
|
2
|
+
|
3
|
+
package testtt;
|
4
|
+
|
5
|
+
import ilog.concert.IloException;
|
6
|
+
|
7
|
+
import ilog.concert.IloIntVar;
|
8
|
+
|
9
|
+
import ilog.concert.IloLinearNumExpr;
|
10
|
+
|
11
|
+
import ilog.cplex.IloCplex;
|
12
|
+
|
13
|
+
public class test2 {
|
14
|
+
|
15
|
+
public static void main(String[] args) {
|
16
|
+
|
17
|
+
try {
|
18
|
+
|
19
|
+
IloCplex numberlink = new IloCplex();
|
20
|
+
|
21
|
+
int row=4;
|
22
|
+
|
23
|
+
int column=4;
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
IloIntVar s[][] = new IloIntVar[row][column];
|
28
|
+
|
29
|
+
for(int i=1;i<=row-1;i++) {
|
30
|
+
|
31
|
+
for(int j=1;j<=column;j++) {
|
32
|
+
|
33
|
+
s[i][j] = numberlink.boolVar();
|
34
|
+
|
35
|
+
}
|
36
|
+
|
37
|
+
}
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
IloIntVar e[][] = new IloIntVar[row][column];
|
42
|
+
|
43
|
+
for(int i=1;i<=row;i++) {
|
44
|
+
|
45
|
+
for(int j=1;j<=column-1;j++) {
|
46
|
+
|
47
|
+
e[i][j] = numberlink.boolVar();
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
IloLinearNumExpr NumberMath = numberlink.linearNumExpr();
|
56
|
+
|
57
|
+
IloIntVar[][] x = new IloIntVar[row][column];
|
58
|
+
|
59
|
+
for(int i=1;i<=row;i++) {
|
60
|
+
|
61
|
+
for(int j=1;j<=column;j++) {
|
62
|
+
|
63
|
+
NumberMath.addTerm(1, x[i][j]);
|
64
|
+
|
65
|
+
numberlink.addLe(NumberMath,3); //数字マスは3以下(要変更)
|
66
|
+
|
67
|
+
numberlink.addGe(NumberMath, 1); //数字マスは1以上(要変更)
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
IloLinearNumExpr NumberMath1_a = numberlink.linearNumExpr();
|
76
|
+
|
77
|
+
NumberMath1_a.addTerm(1,x[1][1]);
|
78
|
+
|
79
|
+
numberlink.addEq(NumberMath1_a,1);
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
IloLinearNumExpr NumberMath1_b = numberlink.linearNumExpr();
|
84
|
+
|
85
|
+
NumberMath1_b.addTerm(1,x[2][3]);
|
86
|
+
|
87
|
+
numberlink.addEq(NumberMath1_b,1);
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
IloLinearNumExpr NumberMath2_a = numberlink.linearNumExpr();
|
94
|
+
|
95
|
+
NumberMath2_a.addTerm(1,x[1][4]);
|
96
|
+
|
97
|
+
numberlink.addEq(NumberMath2_a,2);
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
IloLinearNumExpr NumberMath2_b = numberlink.linearNumExpr();
|
102
|
+
|
103
|
+
NumberMath2_b.addTerm(1,x[3][2]);
|
104
|
+
|
105
|
+
numberlink.addEq(NumberMath2_b,2);
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
IloLinearNumExpr NumberMath3_a = numberlink.linearNumExpr();
|
112
|
+
|
113
|
+
NumberMath3_a.addTerm(1,x[2][2]);
|
114
|
+
|
115
|
+
numberlink.addEq(NumberMath3_a,3);
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
IloLinearNumExpr NumberMath3_b = numberlink.linearNumExpr();
|
120
|
+
|
121
|
+
NumberMath3_b.addTerm(1,x[4][4]);
|
122
|
+
|
123
|
+
numberlink.addEq(NumberMath3_b,3);
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
IloLinearNumExpr MathLine = numberlink.linearNumExpr();
|
132
|
+
|
133
|
+
for(int i=1;i<=row;i++) {
|
134
|
+
|
135
|
+
for(int j=1;j<=column;j++) {
|
136
|
+
|
137
|
+
MathLine.addTerm(1, s[i-1][j]);
|
138
|
+
|
139
|
+
MathLine.addTerm(1, s[i][j]);
|
140
|
+
|
141
|
+
MathLine.addTerm(1, e[i][j-1]);
|
142
|
+
|
143
|
+
MathLine.addTerm(1, e[i][j]);
|
144
|
+
|
145
|
+
if(i-1==0) {
|
146
|
+
|
147
|
+
MathLine.addTerm(-1, s[i-1][j]);
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
if(i==4) {
|
152
|
+
|
153
|
+
MathLine.addTerm(-1, s[i][j]);
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
if(j-1==0) {
|
158
|
+
|
159
|
+
MathLine.addTerm(-1, e[i][j-1]);
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
if(j==4) {
|
164
|
+
|
165
|
+
MathLine.addTerm(-1, e[i][j]);
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
if((i==1&&j==1)||(i==1&&j==4)||(i==2&&j==2)||(i==2&&j==3)||
|
170
|
+
|
171
|
+
(i==3&&j==2)||i==4&&j==4) {
|
172
|
+
|
173
|
+
numberlink.addEq(MathLine, 1);
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
else {numberlink.addGe(MathLine, 2);
|
178
|
+
|
179
|
+
numberlink.addLe(MathLine, 0);}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
IloLinearNumExpr IfsEq1_1 = numberlink.linearNumExpr();
|
192
|
+
|
193
|
+
IloLinearNumExpr IfsEq1_2 = numberlink.linearNumExpr();
|
194
|
+
|
195
|
+
for(int i=1;i<=row-1;i++) {
|
196
|
+
|
197
|
+
for(int j=1;j<=column;j++) {
|
198
|
+
|
199
|
+
IfsEq1_1.addTerm(3,s[i][j]);
|
200
|
+
|
201
|
+
IfsEq1_1.addTerm(-1,x[i][j]);
|
202
|
+
|
203
|
+
IfsEq1_1.addTerm(1,x[i+1][j]);
|
204
|
+
|
205
|
+
numberlink.addLe(IfsEq1_1,3);
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
IfsEq1_2.addTerm(3,s[i][j]);
|
210
|
+
|
211
|
+
IfsEq1_2.addTerm(1,x[i][j]);
|
212
|
+
|
213
|
+
IfsEq1_2.addTerm(-1,x[i+1][j]);
|
214
|
+
|
215
|
+
numberlink.addLe(IfsEq1_1,3);
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
IloLinearNumExpr IfeEq1_1 = numberlink.linearNumExpr();
|
226
|
+
|
227
|
+
IloLinearNumExpr IfeEq1_2 = numberlink.linearNumExpr();
|
228
|
+
|
229
|
+
for(int i=1;i<=row;i++) {
|
230
|
+
|
231
|
+
for(int j=1;j<=column-1;j++) {
|
232
|
+
|
233
|
+
IfeEq1_1.addTerm(3,e[i][j]);
|
234
|
+
|
235
|
+
IfeEq1_1.addTerm(-1,x[i][j]);
|
236
|
+
|
237
|
+
IfeEq1_1.addTerm(1,x[i+1][j]);
|
238
|
+
|
239
|
+
numberlink.addLe(IfeEq1_1,3);
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
IfeEq1_2.addTerm(3,e[i][j]);
|
244
|
+
|
245
|
+
IfeEq1_2.addTerm(1,x[i][j]);
|
246
|
+
|
247
|
+
IfeEq1_2.addTerm(-1,x[i+1][j]);
|
248
|
+
|
249
|
+
numberlink.addLe(IfeEq1_1,3);
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
IloLinearNumExpr AllLine = numberlink.linearNumExpr();
|
260
|
+
|
261
|
+
for(int i=1;i<=row;i++) {
|
262
|
+
|
263
|
+
for(int j=1;j<=column-1;j++) {
|
264
|
+
|
265
|
+
AllLine.addTerm(1,e[i][j]);
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
for(int i=1;i<=row-1;i++) {
|
272
|
+
|
273
|
+
for(int j=1;j<=column;j++) {
|
274
|
+
|
275
|
+
AllLine.addTerm(1,s[i][j]);
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
numberlink.addMinimize(AllLine);
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
if(numberlink.solve()) {
|
288
|
+
|
289
|
+
System.out.println("目的関数値は"+numberlink.getObjValue());
|
290
|
+
|
291
|
+
for(int i=1;i<=row;i++) {
|
292
|
+
|
293
|
+
for(int j=1;j<=column;j++) {
|
294
|
+
|
295
|
+
System.out.println(s[i][j]+"="+numberlink.getValue(s[i][j]));
|
296
|
+
|
297
|
+
System.out.println(e[i][j]+"="+numberlink.getValue(e[i][j]));
|
298
|
+
|
299
|
+
System.out.println(x[i][j]+"="+numberlink.getValue(x[i][j]));
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
catch(IloException e) {
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
```
|
322
|
+
|
1
|
-
Eclipseで実行したところ、
|
323
|
+
上のコードをEclipseで実行したところ、
|
2
324
|
|
3
325
|
|
4
326
|
|
1
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,336 +1,4 @@
|
|
1
|
-
```java
|
2
|
-
|
3
|
-
package testtt;
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
import ilog.concert.IloException;
|
8
|
-
|
9
|
-
import ilog.concert.IloIntVar;
|
10
|
-
|
11
|
-
import ilog.concert.IloLinearNumExpr;
|
12
|
-
|
13
|
-
import ilog.cplex.IloCplex;
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
public class test2 {
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
public static void main(String[] args) {
|
22
|
-
|
23
|
-
try {
|
24
|
-
|
25
|
-
IloCplex numberlink = new IloCplex();
|
26
|
-
|
27
|
-
int row=4;
|
28
|
-
|
29
|
-
int column=4;
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
IloIntVar s[][] = new IloIntVar[row][column];
|
34
|
-
|
35
|
-
for(int i=1;i<=row-1;i++) {
|
36
|
-
|
37
|
-
for(int j=1;j<=column;j++) {
|
38
|
-
|
39
|
-
s[i][j] = numberlink.boolVar();
|
40
|
-
|
41
|
-
}
|
42
|
-
|
43
|
-
}
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
IloIntVar e[][] = new IloIntVar[row][column];
|
48
|
-
|
49
|
-
for(int i=1;i<=row;i++) {
|
50
|
-
|
51
|
-
for(int j=1;j<=column-1;j++) {
|
52
|
-
|
53
|
-
e[i][j] = numberlink.boolVar();
|
54
|
-
|
55
|
-
}
|
56
|
-
|
57
|
-
}
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
IloLinearNumExpr NumberMath = numberlink.linearNumExpr();
|
62
|
-
|
63
|
-
IloIntVar[][] x = new IloIntVar[row][column];
|
64
|
-
|
65
|
-
for(int i=1;i<=row;i++) {
|
66
|
-
|
67
|
-
for(int j=1;j<=column;j++) {
|
68
|
-
|
69
|
-
NumberMath.addTerm(1, x[i][j]);
|
70
|
-
|
71
|
-
numberlink.addLe(NumberMath,3); //数字マスは3以下(要変更)
|
72
|
-
|
73
|
-
numberlink.addGe(NumberMath, 1); //数字マスは1以上(要変更)
|
74
|
-
|
75
|
-
}
|
76
|
-
|
77
|
-
}
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
IloLinearNumExpr NumberMath1_a = numberlink.linearNumExpr();
|
82
|
-
|
83
|
-
NumberMath1_a.addTerm(1,x[1][1]);
|
84
|
-
|
85
|
-
numberlink.addEq(NumberMath1_a,1);
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
IloLinearNumExpr NumberMath1_b = numberlink.linearNumExpr();
|
90
|
-
|
91
|
-
NumberMath1_b.addTerm(1,x[2][3]);
|
92
|
-
|
93
|
-
numberlink.addEq(NumberMath1_b,1);
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
IloLinearNumExpr NumberMath2_a = numberlink.linearNumExpr();
|
100
|
-
|
101
|
-
NumberMath2_a.addTerm(1,x[1][4]);
|
102
|
-
|
103
|
-
numberlink.addEq(NumberMath2_a,2);
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
IloLinearNumExpr NumberMath2_b = numberlink.linearNumExpr();
|
108
|
-
|
109
|
-
NumberMath2_b.addTerm(1,x[3][2]);
|
110
|
-
|
111
|
-
numberlink.addEq(NumberMath2_b,2);
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
IloLinearNumExpr NumberMath3_a = numberlink.linearNumExpr();
|
118
|
-
|
119
|
-
NumberMath3_a.addTerm(1,x[2][2]);
|
120
|
-
|
121
|
-
numberlink.addEq(NumberMath3_a,3);
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
IloLinearNumExpr NumberMath3_b = numberlink.linearNumExpr();
|
126
|
-
|
127
|
-
NumberMath3_b.addTerm(1,x[4][4]);
|
128
|
-
|
129
|
-
numberlink.addEq(NumberMath3_b,3);
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
IloLinearNumExpr MathLine = numberlink.linearNumExpr();
|
138
|
-
|
139
|
-
for(int i=1;i<=row;i++) {
|
140
|
-
|
141
|
-
for(int j=1;j<=column;j++) {
|
142
|
-
|
143
|
-
MathLine.addTerm(1, s[i-1][j]);
|
144
|
-
|
145
|
-
MathLine.addTerm(1, s[i][j]);
|
146
|
-
|
147
|
-
MathLine.addTerm(1, e[i][j-1]);
|
148
|
-
|
149
|
-
MathLine.addTerm(1, e[i][j]);
|
150
|
-
|
151
|
-
if(i-1==0) {
|
152
|
-
|
153
|
-
MathLine.addTerm(-1, s[i-1][j]);
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
if(i==4) {
|
158
|
-
|
159
|
-
MathLine.addTerm(-1, s[i][j]);
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
if(j-1==0) {
|
164
|
-
|
165
|
-
MathLine.addTerm(-1, e[i][j-1]);
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
if(j==4) {
|
170
|
-
|
171
|
-
MathLine.addTerm(-1, e[i][j]);
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
if((i==1&&j==1)||(i==1&&j==4)||(i==2&&j==2)||(i==2&&j==3)||
|
176
|
-
|
177
|
-
(i==3&&j==2)||i==4&&j==4) {
|
178
|
-
|
179
|
-
numberlink.addEq(MathLine, 1);
|
180
|
-
|
181
|
-
}
|
182
|
-
|
183
|
-
else {numberlink.addGe(MathLine, 2);
|
184
|
-
|
185
|
-
numberlink.addLe(MathLine, 0);}
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
}
|
190
|
-
|
191
|
-
}
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
IloLinearNumExpr IfsEq1_1 = numberlink.linearNumExpr();
|
198
|
-
|
199
|
-
IloLinearNumExpr IfsEq1_2 = numberlink.linearNumExpr();
|
200
|
-
|
201
|
-
for(int i=1;i<=row-1;i++) {
|
202
|
-
|
203
|
-
for(int j=1;j<=column;j++) {
|
204
|
-
|
205
|
-
IfsEq1_1.addTerm(3,s[i][j]);
|
206
|
-
|
207
|
-
IfsEq1_1.addTerm(-1,x[i][j]);
|
208
|
-
|
209
|
-
IfsEq1_1.addTerm(1,x[i+1][j]);
|
210
|
-
|
211
|
-
numberlink.addLe(IfsEq1_1,3);
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
IfsEq1_2.addTerm(3,s[i][j]);
|
216
|
-
|
217
|
-
IfsEq1_2.addTerm(1,x[i][j]);
|
218
|
-
|
219
|
-
IfsEq1_2.addTerm(-1,x[i+1][j]);
|
220
|
-
|
221
|
-
numberlink.addLe(IfsEq1_1,3);
|
222
|
-
|
223
|
-
}
|
224
|
-
|
225
|
-
}
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
IloLinearNumExpr IfeEq1_1 = numberlink.linearNumExpr();
|
232
|
-
|
233
|
-
IloLinearNumExpr IfeEq1_2 = numberlink.linearNumExpr();
|
234
|
-
|
235
|
-
for(int i=1;i<=row;i++) {
|
236
|
-
|
237
|
-
for(int j=1;j<=column-1;j++) {
|
238
|
-
|
239
|
-
IfeEq1_1.addTerm(3,e[i][j]);
|
240
|
-
|
241
|
-
IfeEq1_1.addTerm(-1,x[i][j]);
|
242
|
-
|
243
|
-
IfeEq1_1.addTerm(1,x[i+1][j]);
|
244
|
-
|
245
|
-
numberlink.addLe(IfeEq1_1,3);
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
IfeEq1_2.addTerm(3,e[i][j]);
|
250
|
-
|
251
|
-
IfeEq1_2.addTerm(1,x[i][j]);
|
252
|
-
|
253
|
-
IfeEq1_2.addTerm(-1,x[i+1][j]);
|
254
|
-
|
255
|
-
numberlink.addLe(IfeEq1_1,3);
|
256
|
-
|
257
|
-
}
|
258
|
-
|
259
|
-
}
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
IloLinearNumExpr AllLine = numberlink.linearNumExpr();
|
266
|
-
|
267
|
-
for(int i=1;i<=row;i++) {
|
268
|
-
|
269
|
-
for(int j=1;j<=column-1;j++) {
|
270
|
-
|
271
|
-
AllLine.addTerm(1,e[i][j]);
|
272
|
-
|
273
|
-
}
|
274
|
-
|
275
|
-
}
|
276
|
-
|
277
|
-
for(int i=1;i<=row-1;i++) {
|
278
|
-
|
279
|
-
for(int j=1;j<=column;j++) {
|
280
|
-
|
281
|
-
AllLine.addTerm(1,s[i][j]);
|
282
|
-
|
283
|
-
}
|
284
|
-
|
285
|
-
}
|
286
|
-
|
287
|
-
numberlink.addMinimize(AllLine);
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
if(numberlink.solve()) {
|
296
|
-
|
297
|
-
System.out.println("目的関数値は"+numberlink.getObjValue());
|
298
|
-
|
299
|
-
for(int i=1;i<=row;i++) {
|
300
|
-
|
301
|
-
for(int j=1;j<=column;j++) {
|
302
|
-
|
303
|
-
System.out.println(s[i][j]+"="+numberlink.getValue(s[i][j]));
|
304
|
-
|
305
|
-
System.out.println(e[i][j]+"="+numberlink.getValue(e[i][j]));
|
306
|
-
|
307
|
-
System.out.println(x[i][j]+"="+numberlink.getValue(x[i][j]));
|
308
|
-
|
309
|
-
}
|
310
|
-
|
311
|
-
}
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
}
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
}
|
320
|
-
|
321
|
-
catch(IloException e) {
|
322
|
-
|
323
|
-
}
|
324
|
-
|
325
|
-
}
|
326
|
-
|
327
|
-
}
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
```
|
332
|
-
|
333
|
-
|
1
|
+
Eclipseで実行したところ、
|
334
2
|
|
335
3
|
|
336
4
|
|