Skip to content

Commit

Permalink
Add addon callback support
Browse files Browse the repository at this point in the history
  • Loading branch information
mtth committed Nov 12, 2023
1 parent f26aa65 commit ac753a4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/highs-addon/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export declare class Solver {
weights: Matrix
): void;

setCallback(cb: Callback): void;
startCallback(tp: CallbackType): void;
stopCallback(tp: CallbackType): void;

run(cb: (err: Error) => void): void;
getModelStatus(): ModelStatus;
getInfo(): Info;
Expand Down Expand Up @@ -168,3 +172,31 @@ export type SolutionStatus = number;

// https://github.com/ERGO-Code/HiGHS/blob/master/src/lp_data/HConst.h#L127
export type SolutionStyle = number;

// https://ergo-code.github.io/HiGHS/stable/callbacks/
export type Callback = (inputs: CallbackInputs) => CallbackOutputs;

// https://github.com/ERGO-Code/HiGHS/blob/master/src/lp_data/HConst.h#L209
export type CallbackType = number;

export interface CallbackInputs {
// Simplex interrupt
readonly simplex_iteration_count?: number;
// IPM interrupt
readonly ipm_iteration_count?: number;
// MIP improving solution
readonly mip_solution?: unknown;
// MIP interrupt
readonly running_time?: number;
readonly objective_function_value?: number;
readonly num_nodes?: number;
readonly primal_bound?: number;
readonly dual_bound?: number;
readonly mip_gap?: number;
// Unknown
readonly [name: string]: unknown;
}

export interface CallbackOutputs {
readonly user_interrupt: number;
}
43 changes: 43 additions & 0 deletions packages/highs-addon/src/solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,49 @@ Napi::Value Solver::GetInfo(const Napi::CallbackInfo& info) {
return obj;
}

// Callbacks

void Solver::SetCallback(const Napi::CallbackInfo& info) {
// TODO. Need to figure out how best to call JS from worker.
}

int ToCallbackType(const Napi::Env& env, const Napi::Value& val) {
int tp = val.As<Napi::Number>().Int32Value();
if (tp < 0 || tp > HighsCallbackType::kNumCallbackType) {
ThrowError(env, "Unexpected callback type");
return -1;
}
return tp;
}

void Solver::StartCallback(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
int length = info.Length();
if (length != 1 || !info[0].IsNumber()) {
ThrowTypeError(env, "Expected 1 argument [number]");
return;
}
int tp = ToCallbackType(env, info[0]);
if (tp < 0) {
return;
}
this->highs_->startCallback(tp);
}

void Solver::StopCallback(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
int length = info.Length();
if (length != 1 || !info[0].IsNumber()) {
ThrowTypeError(env, "Expected 1 argument [number]");
return;
}
int tp = ToCallbackType(env, info[0]);
if (tp < 0) {
return;
}
this->highs_->stopCallback(tp);
}

// Solutions

Napi::Value ToFloat64Array(const Napi::Env& env, const std::vector<double>& vec) {
Expand Down
4 changes: 4 additions & 0 deletions packages/highs-addon/src/solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class Solver : public Napi::ObjectWrap<Solver> {
void ChangeColsCost(const Napi::CallbackInfo& info);
void AddRows(const Napi::CallbackInfo& info);

void SetCallback(const Napi::CallbackInfo& info);
void StartCallback(const Napi::CallbackInfo& info);
void StopCallback(const Napi::CallbackInfo& info);

void Run(const Napi::CallbackInfo& info);
Napi::Value GetModelStatus(const Napi::CallbackInfo& info);
Napi::Value GetInfo(const Napi::CallbackInfo& info);
Expand Down
7 changes: 7 additions & 0 deletions packages/highs-solver/test/solver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,11 @@ describe('solver', () => {
});
}
});

describe('handles callbacks', () => {
test('simple', async () => {
const solver = sut.Solver.create();
await solver.setModelFromFile(loader.localUrl('simple.lp'));
});
});
});

0 comments on commit ac753a4

Please sign in to comment.