Built-in variables and functions¶
Parameters¶
Modeling & Solving¶
lsTimeLimit = {10, 50};
Spends 10 (resp. 50) sec to optimize objective 0 (resp. 1). Note that the braces must be omitted when setting the parameter in the command line.lsTimeLimit = 60;
Corresponds tolsTimeLimit = {0,..., 0, 60}
.lsIterationLimit = {1000, 5000};
Spends 1000 (resp. 5000) iterations to optimize objective 0 (resp. 1).lsIterationLimit = 6000;
Corresponds tolsIterationLimit = {0,..., 0, 6000}
.lsTimeBetweenDisplays = 5;
Displays info about the search every 5 sec (default: 1).lsSeed = 9;
Sets pseudo-random number generator seed to 9 (default: 0).lsVerbosity = 1;
Sets verbosity to 1 (no display: 0, default: 1, wordy: 2). In verbosity 2, each second, LocalSolver displays the elapsed time, the number of iterations and the current best objective value.
Programming functions¶
Input & Output¶
Note
To use these functions, you must import the io module. For that, add the
following line at the beginning of your LSP file: use io;
.
f = io.openRead("data.in");
Opens file “data.in” in reading mode.f = io.openWrite("data.out");
Opens file “data.out” in writing mode.f = io.openAppend("data.out");
Opens file “data.out” in append mode.f.close();
Closes the file.f.eof()
Returns true if the end of file is reached.i = f.readInt();
Reads the next int parsed in file.i = f.readDouble();
Reads the next floating-point number parsed in file.s = f.readln();
Reads the next line of file.s = f.readString();
Reads the next string parsed in file.f.flush();
Flush the underlying buffer of the file.print("s = " + s + "\n");
Prints the string in console.f.print("s = " + s + "\n");
Prints the string in file.println("s = " + s);
Prints the string followed by a line feed in console.f.println("s = " + s);
Prints the string followed by a line feed in file.
Map¶
m = {};
Creates an empty map.m = {9, "abc"};
Creates a map containing values 9, “abc” at keys 0, 1 respectively.x = m[1];
Retrieves the value associated to key 1 or returns nil if no such key exists.nbElems = m.count();
Counts the number of values in the map.elems = m.values();
Returns the values of the map as a map.indices = m.keys();
Returns the keys of the map as a map.m.add(123);
Adds 123 in the map with key equals to the largest integer key plus one, or zero if no integer key exists.
String¶
i = "123".toInt();
Converts the string into the corresponding integer.i = "123.45".toDouble();
Converts the string into the corresponding floating-point number.s = " abcd ".trim();
Removes white spaces at the beginning and at the end of the string.len = "abcd".length();
Returns the length of a string.m = "a b c d".split();
Splits string “a b c d” into substrings (as a map). Whitespaces are used as delimiter.m = "a::b::c::d".split("::");
Splits string “a::b::c::d” into substrings (as a map) according to the separator “::”.s = "abcd".substring(1,2);
Returns a new string that is a substring of this string. There are two versions of this function: The first one takes one argument: the start index of the substring. The second one takes 2 arguments: the start index and the length of the substring.b = "abcd".startsWith("ab");
Returns true if the string starts with the specified prefix. Returns true if the prefix is the empty string.b = "abcd".endsWith("cd");
Returns true if the string ends with the specified suffix. Returns true if the suffix is the empty string.s = "ABCD".toLowerCase();
Returns a new string converted to lower case.s = "abcd".toUpperCase();
Returns a new string converted to upper case.s = "I was just at home".indexOf("home");
Returns the position of the substring “home” in the string or -1 if the substring was not found.s = "I was just at home".indexOf("home", 10);
Returns the position of the substring “home” in the string, starting at the specified position or -1 if the substring was not found.s = "abcd".replace("bc","x");
Replaces each substring of a string that matches the literal target string with the specified literal replacement string. The replacement proceeds from the beginning of the string to the end, for example, replacing “aa” with “b” in the string “aaaaa” will result in “bba” rather than “abb”.This function takes 2 arguments: the searched sequence and the replace sequence.
Modeling & Solving¶
v = x.value;
Retrieves the value of modeling expressionx
in the best solution found by the solver.x.value = 1;
Sets the value ofx
to 1 in the initial solution (or throws an error ifx
is not a decision).lsSolution.status
Returns the status of the solution. The status can be one of the following strings:“INCONSISTENT” for inconsistent model: the solver proved that the model admits no feasible solution
“INFEASIBLE” for infeasible solution: the computed solution violates some constraints
“FEASIBLE” for feasible solution: the computed solution satisfies all constraints
“OPTIMAL” for optimal: the solution is feasible and its optimality has been proven.
lsSolution.objectiveBounds[0]
Returns the best bound found by the solver for the first objective.