Skip to content

Add TypeScript definitions, Part 2 #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 113 additions & 32 deletions types/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ export interface IModelOptions {
exitOnCycles?: boolean;
}

export interface IModelExternalSolverOptions {
solver: "lpsolve",
binPath: string,
tempName: string,
args: string[]
}

/**
* Represents an LP/MILP problem.
* @typeparam TSolutionVar the decision variables that will be outputed to the `Solution` object.
* @typeparam TInternalVar the decision variables that will not be outputed to the `Solution` object.
* @see `ISingleObjectiveModel`
*/
export interface IModel<TSolutionVar extends string = string, TInternalVar extends string = string> {
/** Name of the variable that will be the optimization objective. */
optimize: (TSolutionVar | TInternalVar);
/** To which direction to optimize the objective. */
opType: "max" | "min";
export interface IModelBase<TSolutionVar extends string = string, TInternalVar extends string = string> {
/**
* Optimization constraints.
* Specify an object with variable name as keys.
Expand Down Expand Up @@ -84,6 +88,36 @@ export interface IModel<TSolutionVar extends string = string, TInternalVar exten
* Options for solving this problem.
*/
options?: IModelOptions;
/**
* For server-side JS environment, options for using external solver to solve the model.
* @remarks this is still very much in progress and subject to change...
*/
external?: IModelExternalSolverOptions;
}

/**
* Represents a single-objective LP/MILP problem.
* @typeparam TSolutionVar the decision variables that will be outputed to the `Solution` object.
* @typeparam TInternalVar the decision variables that will not be outputed to the `Solution` object.
*/
export interface ISingleObjectiveModel<TSolutionVar extends string = string, TInternalVar extends string = string> extends IModelBase<TSolutionVar, TInternalVar> {
/** Name of the variable that will be the optimization objective. */
optimize: (TSolutionVar | TInternalVar);
/** To which direction to optimize the objective. */
opType: "max" | "min";
}

/**
* Represents a multi-objective LP/MILP problem.
* @typeparam TSolutionVar the decision variables that will be outputed to the `Solution` object.
* @typeparam TInternalVar the decision variables that will not be outputed to the `Solution` object.
*/
export interface IMultiObjectiveModel<TSolutionVar extends string = string, TInternalVar extends string = string> extends IModelBase<TSolutionVar, TInternalVar> {
/**
* Name of the variables that will be the optimization objectives.
* Values of the object are the optimization direction.
*/
optimize: { [variable?: TSolutionVar | TInternalVar]: "max" | "min" };
}

/**
Expand All @@ -92,7 +126,7 @@ export interface IModel<TSolutionVar extends string = string, TInternalVar exten
export interface ISolutionStatus {
/** Whether the problem is feasible. */
feasible: boolean;
/** Value pf the objective function. */
/** Value of the objective function. */
result: number;
/** Whether the decision variables are bounded. */
bounded?: boolean;
Expand All @@ -107,27 +141,26 @@ export interface ISolutionStatus {
export type Solution<TSolutionVar extends string> = ISolutionStatus & { [variable in TSolutionVar]?: number };

/**
* Gets the last solved model.
* Gets the lastest solved model.
*/
export const lastSolvedModel: IModel;

export const lastSolvedModel: IModelBase;

/**
* Converts the LP file content into a model object that jsLPSolver can handle.
* @param model Array of string containing raw content of model we want solver to operate on,
* each item is a line of content, without suffixing `"\n"`.
* See http://lpsolve.sourceforge.net/5.5/lp-format.htm for the spec.
*/
export function ReformatLP(model: string[]): IModel;
export function ReformatLP(model: string[]): IModelBase;
/**
* Convert a friendly JSON model into a model for a real solving library...
* in this case lp_solver.
* @param model The model we want solver to operate on.
*/
export function ReformatLP(model: IModel<any, any>): string;
export function ReformatLP(model: IModelBase<any, any>): string;

/**
* Solves an LP/MILP problem.
* Solves a single-objective LP/MILP problem.
* @param model The model we want solver to operate on.
* @param precision If we're solving a MILP, how tight
* do we want to define an integer, given
Expand All @@ -139,16 +172,47 @@ export function ReformatLP(model: IModel<any, any>): string;
* functions in the *Validate* module
*/
export function Solve<TSolutionVar extends string, TInternalVar extends string>(
model: IModel<TSolutionVar, TInternalVar>, precision?: number,
model: ISingleObjectiveModel<TSolutionVar, TInternalVar>, precision?: number,
full?: boolean, validate?: unknown): Solution<TSolutionVar>;

/**
* Solves a multi-objective LP problem. See `README.md` for more information.
* @param model The model we want solver to operate on.
* @remarks *MULTI OBJECTIVE OPTIMIZATION*: This is kind of a throwaway function I added because I needed it for something.
* I don't know if there's a better way to do this, or if it even makes sense, so please take this with a grain of salt.
*/
export function MultiObjective<TSolutionVar extends string, TInternalVar extends string>(
model: IMultiObjectiveModel<TSolutionVar, TInternalVar>): object;

/**
* @internal
*/
export class Term {
public constructor(public variable: Variable, public coefficient: number);
}

//==================== WIP BELOW ====================//
// Members below this line are automatically generated and need to be sorted out.
// I will gradually move the members up across this line.
/**
* @internal
*/
export type VariablePriority
= 0 | "required"
| 1 | "strong"
| 2 | "medium"
| 3 | "weak";

/** Declaration file generated by dts-gen */
/**
* @internal
*/
export class Variable {
public constructor(public id: any, public cost: number, public index: number, public priority: VariablePriority);
public value: number;
public readonly isInteger?: boolean;
public readonly isSlack?: boolean;
}

/**
* @internal
*/
export class Constraint {
constructor(rhs: any, isUpperBound: any, index: any, model: any);

Expand All @@ -163,8 +227,25 @@ export class Constraint {
setVariableCoefficient(newCoefficient: any, variable: any): any;
}

/**
* @internal
*/
export class Model {
constructor(precision: any, name: any);
constructor(precision?: number, name?: string);

private tableau: Tableau;
public readonly name: string | undefined;
private variables: Variable[];
private integerVariables: Variable[];
private unrestrictedVariables: Variable[];
private constraints: Constraint[];
private nConstraints: number;
private nVariables: number;
private isMinimization: boolean;
private tableauInitialized: boolean;
private relaxationIndex: number;
private useMIRCuts: boolean;
private checkForCycles: boolean;

activateMIRCuts(useMIRCuts: any): void;

Expand Down Expand Up @@ -207,8 +288,11 @@ export class Model {
updateRightHandSide(constraint: any, difference: any): any;
}

/**
* @internal
*/
export class Tableau {
constructor(precision: any);
constructor(precision?: number);

addConstraint(constraint: any): void;

Expand Down Expand Up @@ -280,17 +364,14 @@ export class Tableau {

}

export const External: {
};

export const Numeral: any;

export const branchAndCut: {
};


export function MultiObjective(model: any): any;

export function Term(variable: any, coefficient: any): void;
/**
* @internal
*/
export namespace External {
}

export function Variable(id: any, cost: any, index: any, priority: any): void;
/**
* @internal
*/
export namespace branchAndCut {
}