/
solver.ts
252 lines (191 loc) · 6.32 KB
/
solver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const munkres = require("munkres-js");
const bitCount = 3;
const voltage = 3.3;
// const voltage = 5;
// 75 Ohms built into the TV
const seriesResistance = 75;
// This is incorrect, black voltage is 0.3. However with 3 bits, it's hard to solve for output values between 0.3-1.0,
// so we calculate from 0.0-1.0
// const blackVoltage = 0.0;
const blackVoltage = 0.3;
const whiteVoltage = 1.0;
const colorRangeTolerance = 0.1;
const maxResistor = 2000;
// const resistorIncrement = 10;
type ResistorConfig =
| {
type: "allValues";
increment: number;
}
| {
type: "setValues";
values: number[];
};
// Do a full search over the searchspace (stepping by 10)
// const config: ResistorConfig = {
// type: "allValues",
// increment: 10,
// } as ResistorConfig;
// Search for specific resistor values
const config: ResistorConfig = {
type: "setValues",
values: [
10, 22, 47, 100, 150, 200, 220, 270, 330, 470, 510, 680, 1000, 2000, 2200,
3300, 4700, 5100,
],
} as ResistorConfig;
const numberOfVoltageLevels = Math.pow(2, bitCount);
const voltageLevels: number[] = [];
const voltageRange = whiteVoltage - blackVoltage;
const voltageStepSize = voltageRange / (numberOfVoltageLevels - 1);
for (let i = 0; i < numberOfVoltageLevels; i++) {
// Take the steps between 0-0.7 and shift them to be 0.3-1.0
voltageLevels.push(voltageStepSize * i + blackVoltage);
}
let bestMatchValues: number[] | undefined = undefined;
let bestDiff = Number.MAX_SAFE_INTEGER;
const resistorIterator = (
variableValues: number[],
closure: (variableValues: number[]) => void
) => {
switch (config.type) {
case "allValues": {
for (let i = config.increment; i < maxResistor; i += config.increment) {
closure([...variableValues, i]);
}
break;
}
case "setValues": {
for (const resistorValue of config.values) {
closure([...variableValues, resistorValue]);
}
break;
}
}
};
const buildActiveBitEquation = (values: number[], activeBits: number) => {
const activeValues: number[] = [];
const groundedValues: number[] = [];
for (let i = 0; i < values.length; i++) {
const value = values[i];
const active = activeBits & (1 << i);
if (active) {
activeValues.push(value);
} else {
groundedValues.push(value);
}
}
let groundedPath = 0;
// All grounded resistors are in parallel
let groundedParallel = 1 / 75;
for (const value of groundedValues) {
groundedParallel += 1 / value;
}
groundedPath += 1 / groundedParallel;
// All active resistors are in parallel
let activeParallel = 0;
for (const value of activeValues) {
activeParallel += 1 / value;
}
activeParallel = 1 / activeParallel;
return (groundedPath / (groundedPath + activeParallel)) * voltage;
};
const distributedExpectedPointsWithinRange = (
pointCount: number,
range: { min: number; max: number }
): number[] => {
const points: number[] = [];
const diff = range.max - range.min;
const segmentSize = diff / (pointCount + 1);
for (let i = 0; i <= pointCount + 1; i++) {
points.push(segmentSize * i + range.min);
}
return points;
};
const processValues = (variableValues: number[], childrenCount: number) => {
if (childrenCount !== 0) {
// Continue recursing
resistorIterator(variableValues, (values) => {
processValues(values, childrenCount - 1);
});
return;
}
// Base case, evaluate this last level
const results: number[] = [];
// Ignore the off case
for (let i = 1; i < numberOfVoltageLevels; i++) {
const result = buildActiveBitEquation(variableValues, i);
results.push(result);
}
results.sort((a, b) => a - b);
let processBestActiveBits: number[] | undefined = undefined;
let processBestDiff = Number.MAX_SAFE_INTEGER;
const m = new munkres.Munkres();
// Attempt matching with a different number of usable elements
// There must be at least 1 element in the result, and 2 matches must be black and white voltages
for (
let pointCount = 1;
pointCount < numberOfVoltageLevels - 2;
pointCount++
) {
const points = distributedExpectedPointsWithinRange(pointCount, {
min: blackVoltage,
max: whiteVoltage,
});
// Find the best match for each of these evenly distributed points in the resistor results
const selectedActiveBits: number[] = [];
let selectedDiff = 0;
// Build cost matrix. Munkres-JS will automatically 0 pad to make it square
const matrix: Array<number[]> = [];
for (const point of points) {
const row: number[] = [];
for (const voltageResult of results) {
// Drift with this resistor combination to our target voltage
const distance = Math.abs(point - voltageResult);
row.push(distance);
}
matrix.push(row);
}
const result = m.compute(matrix);
for (const tuple of result) {
const [row, column] = tuple;
const diff = matrix[row][column];
selectedDiff += diff;
// Push selected variable values. These are offset active bits because of 0
const variableActiveBits = column + 1;
selectedActiveBits.push(variableActiveBits);
}
// TODO: This should check all possible allocations of point to generated resistor value to prevent local maxima
// Compare average diff so that more variables selected doesn't mean more diff
selectedDiff = selectedDiff / pointCount;
if (selectedDiff < processBestDiff) {
processBestActiveBits = selectedActiveBits;
processBestDiff = selectedDiff;
}
}
if (processBestDiff < bestDiff) {
bestDiff = processBestDiff;
bestMatchValues = variableValues;
}
};
// processValues([450, 900], 0);
resistorIterator([], (values) => {
// At least one resistor value
console.log("Processing step", values[0]);
processValues(values, bitCount - 1);
});
console.log("Best matches:", bestMatchValues);
console.log("Diff:", bestDiff);
if (bestMatchValues === undefined) {
console.log("No match found");
process.exit();
}
for (let i = 0; i < numberOfVoltageLevels; i++) {
const result = buildActiveBitEquation(bestMatchValues, i);
const activeResistors = bestMatchValues.filter((_, index) => {
return (i & (1 << index)) != 0;
});
const resistorString =
activeResistors.length > 0 ? `${activeResistors.join(", ")}` : "None";
console.log(`${resistorString}: ${result}`);
}