1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . IO ;
4
- using Microsoft . ML ;
5
3
using Microsoft . VisualStudio . TestTools . UnitTesting ;
6
4
7
5
namespace Microsoft . ML . Auto . Test
8
6
{
9
7
[ TestClass ]
10
8
public class SweeperTests
11
9
{
12
- [ Ignore ]
13
10
[ TestMethod ]
14
- public void Smac2ParamsTest ( )
11
+ public void Smac3ParamsTest ( )
15
12
{
13
+ var numInitialPopulation = 10 ;
14
+
16
15
var sweeper = new SmacSweeper ( new SmacSweeper . Arguments ( )
17
16
{
18
17
SweptParameters = new INumericValueGenerator [ ] {
19
- new FloatValueGenerator ( new FloatParamArguments ( ) { Name = "foo" , Min = 1 , Max = 5 } ) ,
20
- new LongValueGenerator ( new LongParamArguments ( ) { Name = "bar" , Min = 1 , Max = 1000 , LogBase = true } )
18
+ new FloatValueGenerator ( new FloatParamArguments ( ) { Name = "x1" , Min = 1 , Max = 1000 } ) ,
19
+ new FloatValueGenerator ( new FloatParamArguments ( ) { Name = "x2" , Min = 1 , Max = 1000 } ) ,
20
+ new FloatValueGenerator ( new FloatParamArguments ( ) { Name = "x3" , Min = 1 , Max = 1000 } ) ,
21
21
} ,
22
+ NumberInitialPopulation = numInitialPopulation
22
23
} ) ;
23
24
24
- Random rand = new Random ( 0 ) ;
25
25
List < RunResult > results = new List < RunResult > ( ) ;
26
26
27
- int count = 0 ;
28
- while ( true )
27
+ RunResult bestResult = null ;
28
+ for ( var i = 0 ; i < numInitialPopulation + 1 ; i ++ )
29
29
{
30
30
ParameterSet [ ] pars = sweeper . ProposeSweeps ( 1 , results ) ;
31
- if ( pars == null )
32
- {
33
- break ;
34
- }
31
+
35
32
foreach ( ParameterSet p in pars )
36
33
{
37
- float foo = 0 ;
38
- long bar = 0 ;
34
+ float x1 = ( p [ "x1" ] as FloatParameterValue ) . Value ;
35
+ float x2 = ( p [ "x2" ] as FloatParameterValue ) . Value ;
36
+ float x3 = ( p [ "x3" ] as FloatParameterValue ) . Value ;
39
37
40
- foo = ( p [ "foo" ] as FloatParameterValue ) . Value ;
41
- bar = ( p [ "bar" ] as LongParameterValue ) . Value ;
38
+ double metric = - 200 * ( Math . Abs ( 100 - x1 ) +
39
+ Math . Abs ( 300 - x2 ) + Math . Abs ( 500 - x3 ) ) ;
42
40
43
- double metric = ( ( 5 - Math . Abs ( 4 - foo ) ) * 200 ) + ( 1001 - Math . Abs ( 33 - bar ) ) + rand . Next ( 1 , 20 ) ;
44
- results . Add ( new RunResult ( p , metric , true ) ) ;
45
- count ++ ;
46
- Console . WriteLine ( "{0}--{1}--{2}--{3}" , count , foo , bar , metric ) ;
41
+ RunResult result = new RunResult ( p , metric , true ) ;
42
+ if ( bestResult == null || bestResult . MetricValue < metric )
43
+ {
44
+ bestResult = result ;
45
+ }
46
+ results . Add ( result ) ;
47
+
48
+ Console . WriteLine ( $ "{ metric } \t { x1 } ,{ x2 } ") ;
47
49
}
50
+
48
51
}
52
+
53
+ Console . WriteLine ( $ "Best: { bestResult . MetricValue } ") ;
54
+
55
+ Assert . IsNotNull ( bestResult ) ;
56
+ Assert . IsTrue ( bestResult . MetricValue != 0 ) ;
49
57
}
50
58
59
+
51
60
[ Ignore ]
52
61
[ TestMethod ]
53
- public void Smac4ParamsTest ( )
62
+ public void Smac4ParamsConvergenceTest ( )
54
63
{
55
64
var sweeper = new SmacSweeper ( new SmacSweeper . Arguments ( )
56
65
{
@@ -61,15 +70,14 @@ public void Smac4ParamsTest()
61
70
new FloatValueGenerator ( new FloatParamArguments ( ) { Name = "x4" , Min = 1 , Max = 1000 } ) ,
62
71
} ,
63
72
} ) ;
64
-
65
- Random rand = new Random ( 0 ) ;
73
+
66
74
List < RunResult > results = new List < RunResult > ( ) ;
67
75
68
76
RunResult bestResult = null ;
69
77
for ( var i = 0 ; i < 300 ; i ++ )
70
78
{
71
79
ParameterSet [ ] pars = sweeper . ProposeSweeps ( 1 , results ) ;
72
-
80
+
73
81
// if run converged, break
74
82
if ( pars == null )
75
83
{
@@ -82,14 +90,14 @@ public void Smac4ParamsTest()
82
90
float x2 = ( p [ "x2" ] as FloatParameterValue ) . Value ;
83
91
float x3 = ( p [ "x3" ] as FloatParameterValue ) . Value ;
84
92
float x4 = ( p [ "x4" ] as FloatParameterValue ) . Value ;
85
-
93
+
86
94
double metric = - 200 * ( Math . Abs ( 100 - x1 ) +
87
95
Math . Abs ( 300 - x2 ) +
88
96
Math . Abs ( 500 - x3 ) +
89
- Math . Abs ( 700 - x4 ) ) ;
97
+ Math . Abs ( 700 - x4 ) ) ;
90
98
91
99
RunResult result = new RunResult ( p , metric , true ) ;
92
- if ( bestResult == null || bestResult . MetricValue < metric )
100
+ if ( bestResult == null || bestResult . MetricValue < metric )
93
101
{
94
102
bestResult = result ;
95
103
}
@@ -102,5 +110,44 @@ public void Smac4ParamsTest()
102
110
103
111
Console . WriteLine ( $ "Best: { bestResult . MetricValue } ") ;
104
112
}
113
+
114
+ [ Ignore ]
115
+ [ TestMethod ]
116
+ public void Smac2ParamsConvergenceTest ( )
117
+ {
118
+ var sweeper = new SmacSweeper ( new SmacSweeper . Arguments ( )
119
+ {
120
+ SweptParameters = new INumericValueGenerator [ ] {
121
+ new FloatValueGenerator ( new FloatParamArguments ( ) { Name = "foo" , Min = 1 , Max = 5 } ) ,
122
+ new LongValueGenerator ( new LongParamArguments ( ) { Name = "bar" , Min = 1 , Max = 1000 , LogBase = true } )
123
+ } ,
124
+ } ) ;
125
+
126
+ Random rand = new Random ( 0 ) ;
127
+ List < RunResult > results = new List < RunResult > ( ) ;
128
+
129
+ int count = 0 ;
130
+ while ( true )
131
+ {
132
+ ParameterSet [ ] pars = sweeper . ProposeSweeps ( 1 , results ) ;
133
+ if ( pars == null )
134
+ {
135
+ break ;
136
+ }
137
+ foreach ( ParameterSet p in pars )
138
+ {
139
+ float foo = 0 ;
140
+ long bar = 0 ;
141
+
142
+ foo = ( p [ "foo" ] as FloatParameterValue ) . Value ;
143
+ bar = ( p [ "bar" ] as LongParameterValue ) . Value ;
144
+
145
+ double metric = ( ( 5 - Math . Abs ( 4 - foo ) ) * 200 ) + ( 1001 - Math . Abs ( 33 - bar ) ) + rand . Next ( 1 , 20 ) ;
146
+ results . Add ( new RunResult ( p , metric , true ) ) ;
147
+ count ++ ;
148
+ Console . WriteLine ( "{0}--{1}--{2}--{3}" , count , foo , bar , metric ) ;
149
+ }
150
+ }
151
+ }
105
152
}
106
153
}
0 commit comments