1 | package problems |
---|
2 | |
---|
3 | import ( |
---|
4 | "sort" |
---|
5 | "strconv" |
---|
6 | "strings" |
---|
7 | |
---|
8 | expr "go-symexpr" |
---|
9 | ) |
---|
10 | |
---|
11 | type ExprProblemType int |
---|
12 | |
---|
13 | const ( |
---|
14 | UnknownPType ExprProblemType = iota |
---|
15 | ExprBenchmark |
---|
16 | ExprDiffeq |
---|
17 | ExprRealData |
---|
18 | ) |
---|
19 | |
---|
20 | func (ept ExprProblemType) String() string { |
---|
21 | switch ept { |
---|
22 | case ExprBenchmark: |
---|
23 | return "benchmark" |
---|
24 | case ExprDiffeq: |
---|
25 | return "diffeq" |
---|
26 | case ExprRealData: |
---|
27 | return "real" |
---|
28 | default: |
---|
29 | return "UnknownPType" |
---|
30 | } |
---|
31 | return "" |
---|
32 | } |
---|
33 | |
---|
34 | func ProblemTypeFromString(ptype string) ExprProblemType { |
---|
35 | switch ptype { |
---|
36 | case "benchmark": |
---|
37 | return ExprBenchmark |
---|
38 | case "diffeq": |
---|
39 | return ExprDiffeq |
---|
40 | case "real": |
---|
41 | return ExprRealData |
---|
42 | default: |
---|
43 | return UnknownPType |
---|
44 | } |
---|
45 | return UnknownPType |
---|
46 | } |
---|
47 | |
---|
48 | type ExprProblem struct { |
---|
49 | Name string |
---|
50 | VarNames []string |
---|
51 | FuncTree expr.Expr // function as tree |
---|
52 | MaxIter int |
---|
53 | |
---|
54 | // type of evaluation / data |
---|
55 | SearchType ExprProblemType |
---|
56 | |
---|
57 | // data |
---|
58 | TrainFns []string |
---|
59 | TestFns []string |
---|
60 | Train []*PointSet |
---|
61 | Test []*PointSet |
---|
62 | HitRatio float64 |
---|
63 | |
---|
64 | // variable information |
---|
65 | SearchVar int |
---|
66 | UsableVars []int |
---|
67 | IndepNames []string |
---|
68 | DepndNames []string |
---|
69 | SysNames []string |
---|
70 | |
---|
71 | // tree gen/restrict information |
---|
72 | TreeCfg *TreeParams |
---|
73 | } |
---|
74 | |
---|
75 | type ExprProblemComm struct { |
---|
76 | // incoming channels |
---|
77 | Cmds chan int |
---|
78 | |
---|
79 | // outgoing channels |
---|
80 | Rpts chan *ExprReportArray |
---|
81 | Res chan *ExprReportArray |
---|
82 | Gen chan [2]int |
---|
83 | } |
---|
84 | |
---|
85 | func ProbConfigParser(field, value string, config interface{}) (err error) { |
---|
86 | |
---|
87 | EP := config.(*ExprProblem) |
---|
88 | if EP.TreeCfg == nil { |
---|
89 | EP.TreeCfg = new(TreeParams) |
---|
90 | } |
---|
91 | |
---|
92 | switch strings.ToUpper(field) { |
---|
93 | case "NAME": |
---|
94 | EP.Name = value |
---|
95 | case "PROBLEMTYPE": |
---|
96 | typ := ProblemTypeFromString(strings.ToLower(value)) |
---|
97 | if typ == UnknownPType { |
---|
98 | } else { |
---|
99 | EP.SearchType = typ |
---|
100 | } |
---|
101 | case "MAXITER": |
---|
102 | EP.MaxIter, err = strconv.Atoi(value) |
---|
103 | |
---|
104 | case "HITRATIO": |
---|
105 | fval, cerr := strconv.ParseFloat(value, 64) |
---|
106 | if cerr != nil { |
---|
107 | return cerr |
---|
108 | } |
---|
109 | EP.HitRatio = fval |
---|
110 | |
---|
111 | case "TRAINDATA": |
---|
112 | EP.TrainFns = strings.Fields(value) |
---|
113 | case "TESTDATA": |
---|
114 | EP.TestFns = strings.Fields(value) |
---|
115 | |
---|
116 | case "USABLEVARS": |
---|
117 | usable := strings.Fields(value) |
---|
118 | for _, v := range usable { |
---|
119 | ival, cerr := strconv.Atoi(v) |
---|
120 | if cerr != nil { |
---|
121 | return cerr |
---|
122 | } |
---|
123 | EP.UsableVars = append(EP.UsableVars, ival) |
---|
124 | } |
---|
125 | EP.UsableVars = unique(EP.UsableVars) |
---|
126 | EP.TreeCfg.UsableVars = EP.UsableVars[:] |
---|
127 | EP.TreeCfg.UsableVars = unique(EP.TreeCfg.UsableVars) |
---|
128 | |
---|
129 | case "SEARCHVAR": |
---|
130 | ival, cerr := strconv.Atoi(value) |
---|
131 | if cerr != nil { |
---|
132 | return cerr |
---|
133 | } |
---|
134 | EP.SearchVar = ival |
---|
135 | |
---|
136 | default: |
---|
137 | // check augillary parsable structures [only TreeParams for now] |
---|
138 | found, ferr := ParseTreeParams(field, value, EP.TreeCfg) |
---|
139 | if ferr != nil { |
---|
140 | return ferr |
---|
141 | } |
---|
142 | if !found { |
---|
143 | } |
---|
144 | |
---|
145 | } |
---|
146 | return |
---|
147 | } |
---|
148 | |
---|
149 | func unique(list []int) []int { |
---|
150 | sort.Ints(list) |
---|
151 | var last int |
---|
152 | i := 0 |
---|
153 | for _, x := range list { |
---|
154 | if i == 0 || x != last { |
---|
155 | last = x |
---|
156 | list[i] = x |
---|
157 | i++ |
---|
158 | } |
---|
159 | } |
---|
160 | return list[0:i] |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | func (ep *ExprProblem) CreatePS(Name string, MaxIter int, HitRatio float64, SearchVar int, ProblemTypeString string) { //IndepNames []string, DepndNames []string, SysNames []string |
---|
165 | ep.SearchType = ProblemTypeFromString(ProblemTypeString) |
---|
166 | ep.Name = Name |
---|
167 | |
---|
168 | ep.TrainFns = append(ep.TrainFns, "dummy") |
---|
169 | ep.TestFns = append(ep.TestFns, "dummy") |
---|
170 | |
---|
171 | ep.MaxIter = MaxIter |
---|
172 | ep.HitRatio = HitRatio |
---|
173 | ep.SearchVar = SearchVar |
---|
174 | } |
---|