可以点样最佳化VBA列阵
时间:2013-09-21
来源:互联网

有冇快D既搜寻法可以用系VBA?(好似EXCEL Solver)
Sub opt_target()
Dim p1 as integer
Dim p2 as integer
Dim m as integer
Dim n as integer
Dim parat (m1 to m2, n1 to n2) as integer
Dim max as double
max = -100
p1 = 0
p2 = 0
For p1 = LBound(parat, 1) to UBound(parat,1) step m 'paratmeter1 for fast
For p2 = LBound(parat, 2) to UBound(parat,2) step n 'paratmeter2 for slow
If p1 < p2, then 'fast < slow
range("parat1") = p1 'enter p1 as paratmeter1
range("parat2") = p2 'enter p2 as paratmeter2
If range("result") > max, then 'seach max result%
max = range("result") 'record max result
opt_p1 = p1 'record optizium fast paratmeter1
opt_p2 = p2 'record optizium slow paratmeter2
End if
End if
Next p2
Next p1
End Sub
作者: alee001 发布时间: 2013-09-21
How about consuming solver directly?
作者: ricyik 发布时间: 2013-09-21

作者: alee001 发布时间: 2013-09-21
作者: ricyik 发布时间: 2013-09-21
This looks interesting.
新手想求教CHING们,下面基本搵最佳化参数流程,可以点样优化CODE写得冇咁dirty?

有冇快D既搜寻法可以用系VBA?(好似EXCEL Solver)
Sub opt_target()
Dim p1 as integer
Dim p2 as integer
Dim ...
作者: a8d7e8 发布时间: 2013-09-21
因为SOLVER唔可以限制两个参数非相同,除非有其他搜寻法,如果唔系都要用传统LOOPING去SEARCH答案,好晒时间

作者: alee001 发布时间: 2013-09-21
因为SOLVER唔可以限制两个参数非相同,除非有其他搜寻法,如果唔系都要用传统LOOPING去SEARCH答案,好晒时间

作者: a8d7e8 发布时间: 2013-09-21
Lagrange multipliers?
作者: a8d7e8 发布时间: 2013-09-21

作者: ricyik 发布时间: 2013-09-21

作者: alee001 发布时间: 2013-09-21

作者: alee001 发布时间: 2013-09-21
作者: ricyik 发布时间: 2013-09-21

Sub grid_search()
'Optimization for 2 no. of parameter
Dim max As Single, s1 As Integer, s2 As Integer, p1 As Integer, p2 As Integer, opt1 As Single, opt2 As Single, count As Integer
ReDim arr(range("p1_start") To range("p1_end"), range("p2_start") To range("p2_end")) As Integer
StartTime = Timer 'Record starting time
Application.ScreenUpdating = False
max = -100: opt1 = 0: opt2 = 0: count = 0
s1 = Application.max((UBound(arr, 1) - LBound(arr, 1)) \ range("p1_step"), 1)
s2 = Application.max((UBound(arr, 2) - LBound(arr, 2)) \ range("p2_step"), 1)
If UBound(arr, 1) = LBound(arr, 1) Then p1 = LBound(arr, 1): GoTo p2_loop Else 'for constant parameter1
For p1 = LBound(arr, 1) To UBound(arr, 1) Step s1
p2_loop:
If UBound(arr, 2) = LBound(arr, 2) Then p2 = LBound(arr, 2): GoTo run_result Else 'for constant parameter2
For p2 = LBound(arr, 2) To UBound(arr, 2) Step s2
run_result:
If p1 < p2 Then
range("p1_optz") = p1
range("p2_optz") = p2
count = count + 1
If range("result") >= max Then
max = range("result")
opt1 = p1
opt2 = p2
End If
End If
If UBound(arr, 2) = LBound(arr, 2) Then GoTo p1_loop_type2 Else
Next p2
p1_loop_type2:
If UBound(arr, 1) = LBound(arr, 1) Then GoTo end_case Else
Next p1
end_case:
Application.ScreenUpdating = True
'record optimizated parapmeters and result
range("B2") = opt1
range("C2") = opt2
range("D2") = count
range("E2") = Format(Timer - StartTime, "00.00") & " seconds"
End Sub
作者: alee001 发布时间: 2013-09-21
With no knowledge of the data, no assumption can be made to choose an appropriate "better" algorithm.
作者: a8d7e8 发布时间: 2013-09-21
作者: alee001 发布时间: 2013-09-21
作者: a8d7e8 发布时间: 2013-09-21
Solver cannot let parameter $C$3>$C$2 or $C$3$C$2

1. Define a cell, say C6 as IF(OR($C$3>$C$2, $C$3<>$C$2),1,0)
2. Then in solver, add constraint: $C$6=1
作者: ricyik 发布时间: 2013-09-21

不过有时用SOLVER解非线性题目时,每次求解会出现不同正确的答案?
(依我两个参数最佳化为例子,可能出现p1及p2的答案分别为8,20; 8,21; 9,20; 9,21,它们同时都是解的最大值,但SOLVER有时会得出其中一组为答案,所以变相没有绝对答案

作者: alee001 发布时间: 2013-09-21
From your code and replies looks you are working on back-testing of moving averages.
Then consider adding more constraints to help solver to choose the pair of parameters which match your trading style and/or test against more kind of stocks, just 2 cents.
[ 本帖最后由 ricyik 於 2013-9-2 08:11 AM 编辑 ]
作者: ricyik 发布时间: 2013-09-21
I had looking for other VBA method in order to reduce searching time than looping, have you any good idea?
作者: alee001 发布时间: 2013-09-21
To improve performance in general, consider using non-VBA languages. One easiest way is to try many cases in parallel, by using multi-threading, etc.
作者: ricyik 发布时间: 2013-09-21
I known some effect to a limited extent on VBA, I had trying open seveal Excel to run VBA so than multi-searching to divide into group of parameters....may be only method.

作者: alee001 发布时间: 2013-09-21
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28