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