Skip to content

Commit

Permalink
add simulated annealing thresholds
Browse files Browse the repository at this point in the history
  • Loading branch information
dderjoel committed Jun 26, 2024
1 parent 61a95e2 commit 501af76
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/helper/argParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ export const parsedArgs = y
"How to bias where to place an operation during the scheduling mutation. 'reversebell' , 'uniform' uniformly selects in the possible interval.",
choices: SPILLING,
})
.option("threshold", {
default: 0,
number: true,
describe: "How likely is a worse solution to be accepted. -1 for auto, i.e. depening on how bad it is.",
})
.help("help")
.alias("h", "help")
.wrap(Math.min(160, y.terminalWidth()))
Expand Down
11 changes: 6 additions & 5 deletions src/optimizer/optimizer.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { RegisterAllocator } from "@/registerAllocator";
import type { AnalyseResult, OptimizerArgs } from "@/types";

import { genStatistics, genStatusLine, logMutation, printStartInfo } from "./optimizer.helper";
import { init } from "./optimizer.helper.class";
import { init, acceptWorseWithThres } from "./optimizer.helper.class";
import MeasureUtil from "./measure.class";

let choice: CHOICE;
Expand Down Expand Up @@ -285,10 +285,11 @@ export class Optimizer {
let kept: boolean;

if (
// A is not worse and A is new
(metricA <= metricB && currentFunctionIsA()) ||
// or B is not worse and B is new
(metricA >= metricB && !currentFunctionIsA())
acceptWorseWithThres(
currentFunctionIsA() ? metricA : metricB,
currentFunctionIsA() ? metricB : metricA,
this.args.threshold,
)
) {
Logger.log("kept mutation");
kept = true;
Expand Down
15 changes: 15 additions & 0 deletions src/optimizer/optimizer.helper.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,18 @@ export function init(tmpDir: string, args: neededArgs): { symbolname: string; me
}
return createMS(r, sharedObjectFilename);
}
export function acceptWorseWithThres(new_metric: number, old_metric: number, thres: number): boolean {
// if the new one is already better, then accept
if (new_metric <= old_metric) {
return true;
}

// else acceptance depends on the threshold.
// -1 means auto
if (thres == -1) {
const relation = old_metric / new_metric; // 0<relation<1
return Math.random() <= 0.077426 * Math.exp(2.5584 * relation);
}
// else acceptance is constant and we accept with that probability
return Math.random() <= thres;
}
1 change: 1 addition & 0 deletions src/types/optimizer.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type OptimizerArgs = {
uicaarch: UICA_OPTIONS_T;
objectiveFunction: OBJECTIVE_FUNCTION_OPTIONS_T;
spilling: SPILLING_T;
threshold: number;
};
export type ParsedArgsT = OptimizerArgs & {
startFromBestJson: boolean;
Expand Down
73 changes: 73 additions & 0 deletions test/optimiser/optimiser.helper.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright 2023 University of Adelaide
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";

import { acceptWorseWithThres } from "@/optimizer/optimizer.helper.class";

let originalRandom: () => number;

beforeAll(() => {
originalRandom = Math.random;
});
afterAll(() => {
Math.random = originalRandom;
});

describe("acceptWorseWithThres", () => {
it("should return true if new is smaller than old", () => {
expect(acceptWorseWithThres(10, 20, -1)).toBeTruthy();
});

it("should likely return true thres=auto and new approx old", () => {
Math.random = () => 0.1;
expect(acceptWorseWithThres(21, 20, -1)).toBeTruthy();
});
it("should likely return false thres=auto and new far off old", () => {
Math.random = () => 0.1;
expect(acceptWorseWithThres(200, 20, -1)).toBeFalsy();
});
it("should likely return true if le than thres, independent of how much worse new is", () => {
Math.random = () => 0.1;
expect(acceptWorseWithThres(21, 20, 0.1)).toBeTruthy();
expect(acceptWorseWithThres(22, 20, 0.1)).toBeTruthy();
expect(acceptWorseWithThres(2200, 20, 0.1)).toBeTruthy();
});
it("should likely return false if higher than thres, independent of how much worse new is", () => {
Math.random = () => 0.2;
expect(acceptWorseWithThres(21, 20, 0.1)).toBeFalsy();
expect(acceptWorseWithThres(22, 20, 0.1)).toBeFalsy();
expect(acceptWorseWithThres(2200, 20, 0.1)).toBeFalsy();
});
it("should not return true if thres is 0 and the new one is worse (default)", () => {
Math.random = () => 0.2;
expect(acceptWorseWithThres(21, 20, 0)).toBeFalsy();
expect(acceptWorseWithThres(22, 20, 0)).toBeFalsy();
expect(acceptWorseWithThres(2200, 20, 0)).toBeFalsy();
Math.random = () => 0.1;
expect(acceptWorseWithThres(21, 20, 0)).toBeFalsy();
expect(acceptWorseWithThres(22, 20, 0)).toBeFalsy();
expect(acceptWorseWithThres(2200, 20, 0)).toBeFalsy();
Math.random = () => 0.00001;
expect(acceptWorseWithThres(21, 20, 0)).toBeFalsy();
expect(acceptWorseWithThres(22, 20, 0)).toBeFalsy();
expect(acceptWorseWithThres(2200, 20, 0)).toBeFalsy();
Math.random = () => 0.9;
expect(acceptWorseWithThres(21, 20, 0)).toBeFalsy();
expect(acceptWorseWithThres(22, 20, 0)).toBeFalsy();
expect(acceptWorseWithThres(2200, 20, 0)).toBeFalsy();
});
});
1 change: 1 addition & 0 deletions test/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function getTestArgs(filename: string): OptimizerArgs {
objectiveFunction: "cycles",
uicaarch: "RKL",
spilling: "reversebell",
threshold: 0,
};
}

Expand Down

0 comments on commit 501af76

Please sign in to comment.