- 帖子
- 1
- 主題
- 1
- 精華
- 0
- 積分
- 2
- 點名
- 0
- 作業系統
- win7
- 軟體版本
- sp3
- 閱讀權限
- 10
- 性別
- 男
- 註冊時間
- 2014-7-16
- 最後登錄
- 2014-8-3
|
大家好,我是初學者,目前需要使用基因演算法完成論文,在打程式時候有遇到以下問題,請求,感謝。
問題如下:
1 0 0 1
0 1 1 0
1 1 1 1 A陣列
0 0 0 0
1 0 0 0
0 1 0 0
0 0 1 1 A1陣列
0 0 0 0
0 0 0 1
0 0 1 0
1 1 0 0 A2陣列
0 0 0 0
A = A1+A2
主要是 A陣列中的值加起來會是A1與A2中的所有值加起來
陣列的關係限制如下
任一行或列中,加起來的值, A1 = A2 = A/2,也就是說A1與A2的邊際值會等於A/2的邊際值。
例如說 第一列中,A總值為2,A1與A2分別為1
A陣列已知,想要轉成A1與A2 但是有點卡關
我主要想要從A陣列中 求出A1與A2
範例中A1與A2是我自己算出來的
而A1與A2 的值介於0或1之間,位置隨機,只要合乎限制式
我的程式碼如下,請求大大幫忙看一下或是有更好的想法,感謝
Sub 交換_1()
Dim i As Integer, j As Integer, K As Integer, FF As Integer
Dim A As Single, b As Integer
Dim L() As Integer, M() As Integer, n1() As Integer
Dim n As Integer, s As Integer, d As Integer, num1 As Integer, num2 As Integer
n = 3 ' 產品數
d = 8 '需求點
s = 4 '供幾點
num1 = s
num2 = (n * d) + 1
ReDim L(num1), M(num2), n1(num2) 'L 列數 ,M 行數 ,N new列數
For i = 1 To num1
Sheets(5).Cells(i, "Z") = Application.Sum(Range(Sheets(5).Cells(i, 1), Sheets(5).Cells(i, num2)))
Next i
For j = 1 To num2
Sheets(5).Cells(5, j) = Application.Sum(Range(Sheets(5).Cells(1, j), Sheets(5).Cells(num1, j)))
Next j
For i = 1 To num1
L(i) = Sheets(5).Cells(i, "Z") / 2
Next i
For j = 1 To num2
M(j) = Sheets(5).Cells(5, j) / 2
Next j
For i = 1 To num1
For j = 1 To num2
If Sheets(5).Cells(i, j) = 1 Then
If L(i) <> 0 Then
If M(j) <> 0 Then
Randomize
A = Rnd
If A > 0.5 Then
Sheets(5).Cells(i + 9, j) = 1
L(i) = L(i) - 1
M(j) = M(j) - 1
If L(i) = 0 Then
Exit For
End If
End If
End If
End If
End If
If j = num2 Then 'J為最後一行
For FF = 1 To num2 '總行數
n1(i) = Application.CountIf(Range(Sheets(5).Cells(i + 9, j + 1), Sheets(5).Cells(i + 9, j)), 1)
If n1(i) <> Sheets(5).Cells(i, "Z") / 2 Then
If L(i) <> 0 Then
If M(j) <> 0 Then
Sheets(5).Cells(i + 9, j) = 1
L(i) = L(i) - 1
M(j) = M(j) - 1
If L(i) = 0 Then
Exit For
End If
Else
j = j - FF
End If
End If
End If
Next FF
End If
Next j
Next i
End Sub |
|