Skip to content

Commit 501af76

Browse files
committedJun 26, 2024·
add simulated annealing thresholds
1 parent 61a95e2 commit 501af76

File tree

6 files changed

+101
-5
lines changed

6 files changed

+101
-5
lines changed
 

‎src/helper/argParse.ts

+5
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ export const parsedArgs = y
245245
"How to bias where to place an operation during the scheduling mutation. 'reversebell' , 'uniform' uniformly selects in the possible interval.",
246246
choices: SPILLING,
247247
})
248+
.option("threshold", {
249+
default: 0,
250+
number: true,
251+
describe: "How likely is a worse solution to be accepted. -1 for auto, i.e. depening on how bad it is.",
252+
})
248253
.help("help")
249254
.alias("h", "help")
250255
.wrap(Math.min(160, y.terminalWidth()))

‎src/optimizer/optimizer.class.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { RegisterAllocator } from "@/registerAllocator";
4141
import type { AnalyseResult, OptimizerArgs } from "@/types";
4242

4343
import { genStatistics, genStatusLine, logMutation, printStartInfo } from "./optimizer.helper";
44-
import { init } from "./optimizer.helper.class";
44+
import { init, acceptWorseWithThres } from "./optimizer.helper.class";
4545
import MeasureUtil from "./measure.class";
4646

4747
let choice: CHOICE;
@@ -285,10 +285,11 @@ export class Optimizer {
285285
let kept: boolean;
286286

287287
if (
288-
// A is not worse and A is new
289-
(metricA <= metricB && currentFunctionIsA()) ||
290-
// or B is not worse and B is new
291-
(metricA >= metricB && !currentFunctionIsA())
288+
acceptWorseWithThres(
289+
currentFunctionIsA() ? metricA : metricB,
290+
currentFunctionIsA() ? metricB : metricA,
291+
this.args.threshold,
292+
)
292293
) {
293294
Logger.log("kept mutation");
294295
kept = true;

‎src/optimizer/optimizer.helper.class.ts

+15
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,18 @@ export function init(tmpDir: string, args: neededArgs): { symbolname: string; me
186186
}
187187
return createMS(r, sharedObjectFilename);
188188
}
189+
export function acceptWorseWithThres(new_metric: number, old_metric: number, thres: number): boolean {
190+
// if the new one is already better, then accept
191+
if (new_metric <= old_metric) {
192+
return true;
193+
}
194+
195+
// else acceptance depends on the threshold.
196+
// -1 means auto
197+
if (thres == -1) {
198+
const relation = old_metric / new_metric; // 0<relation<1
199+
return Math.random() <= 0.077426 * Math.exp(2.5584 * relation);
200+
}
201+
// else acceptance is constant and we accept with that probability
202+
return Math.random() <= thres;
203+
}

‎src/types/optimizer.types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export type OptimizerArgs = {
4747
uicaarch: UICA_OPTIONS_T;
4848
objectiveFunction: OBJECTIVE_FUNCTION_OPTIONS_T;
4949
spilling: SPILLING_T;
50+
threshold: number;
5051
};
5152
export type ParsedArgsT = OptimizerArgs & {
5253
startFromBestJson: boolean;
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright 2023 University of Adelaide
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
18+
19+
import { acceptWorseWithThres } from "@/optimizer/optimizer.helper.class";
20+
21+
let originalRandom: () => number;
22+
23+
beforeAll(() => {
24+
originalRandom = Math.random;
25+
});
26+
afterAll(() => {
27+
Math.random = originalRandom;
28+
});
29+
30+
describe("acceptWorseWithThres", () => {
31+
it("should return true if new is smaller than old", () => {
32+
expect(acceptWorseWithThres(10, 20, -1)).toBeTruthy();
33+
});
34+
35+
it("should likely return true thres=auto and new approx old", () => {
36+
Math.random = () => 0.1;
37+
expect(acceptWorseWithThres(21, 20, -1)).toBeTruthy();
38+
});
39+
it("should likely return false thres=auto and new far off old", () => {
40+
Math.random = () => 0.1;
41+
expect(acceptWorseWithThres(200, 20, -1)).toBeFalsy();
42+
});
43+
it("should likely return true if le than thres, independent of how much worse new is", () => {
44+
Math.random = () => 0.1;
45+
expect(acceptWorseWithThres(21, 20, 0.1)).toBeTruthy();
46+
expect(acceptWorseWithThres(22, 20, 0.1)).toBeTruthy();
47+
expect(acceptWorseWithThres(2200, 20, 0.1)).toBeTruthy();
48+
});
49+
it("should likely return false if higher than thres, independent of how much worse new is", () => {
50+
Math.random = () => 0.2;
51+
expect(acceptWorseWithThres(21, 20, 0.1)).toBeFalsy();
52+
expect(acceptWorseWithThres(22, 20, 0.1)).toBeFalsy();
53+
expect(acceptWorseWithThres(2200, 20, 0.1)).toBeFalsy();
54+
});
55+
it("should not return true if thres is 0 and the new one is worse (default)", () => {
56+
Math.random = () => 0.2;
57+
expect(acceptWorseWithThres(21, 20, 0)).toBeFalsy();
58+
expect(acceptWorseWithThres(22, 20, 0)).toBeFalsy();
59+
expect(acceptWorseWithThres(2200, 20, 0)).toBeFalsy();
60+
Math.random = () => 0.1;
61+
expect(acceptWorseWithThres(21, 20, 0)).toBeFalsy();
62+
expect(acceptWorseWithThres(22, 20, 0)).toBeFalsy();
63+
expect(acceptWorseWithThres(2200, 20, 0)).toBeFalsy();
64+
Math.random = () => 0.00001;
65+
expect(acceptWorseWithThres(21, 20, 0)).toBeFalsy();
66+
expect(acceptWorseWithThres(22, 20, 0)).toBeFalsy();
67+
expect(acceptWorseWithThres(2200, 20, 0)).toBeFalsy();
68+
Math.random = () => 0.9;
69+
expect(acceptWorseWithThres(21, 20, 0)).toBeFalsy();
70+
expect(acceptWorseWithThres(22, 20, 0)).toBeFalsy();
71+
expect(acceptWorseWithThres(2200, 20, 0)).toBeFalsy();
72+
});
73+
});

‎test/test-helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export function getTestArgs(filename: string): OptimizerArgs {
5555
objectiveFunction: "cycles",
5656
uicaarch: "RKL",
5757
spilling: "reversebell",
58+
threshold: 0,
5859
};
5960
}
6061

0 commit comments

Comments
 (0)
Please sign in to comment.