Linear Programming with Google or-tools in C++: Use callback function to stop search - linear-programming

I am using Google's or-tools library in Combination with the SCIP solver to solve an integer linear program, by leveraging the MPSolver class. I would like to have a customizable callback function being called every-time a new solution candidate is found. This callback function should be able to evaluate the solution and and stop the ilp-solver early if the solution is "good enough" based on some users criteria. The equivalent in Gurobi-py would look something like this:
def callback(model_cb: gp.Model, where):
assert where != gp.GRB.Callback.MULTIOBJ
if where == gp.GRB.Callback.MIPSOL:
model_cb._check_is_success(model_cb.cbGetSolution(s)):
model._interrupted_success = True
model_cb.terminate()
model = gp.Model()
model._check_is_success = check_is_success
model._interrupted_success = False
[...]
# Set up model
model.optimize(callback)
I found the SetCallback in the [OR-Tools documentation}(https://developers.google.com/optimization/reference/linear_solver/linear_solver/MPSolver) for the MPSolver class, but no usage examples. Where can I find an example on how to use Callback functions for early termination of the ILP-solver in or-tools?

Is you problem purely integral?
You can use the native CP-sat API.
It is a better linear integer solver, and supports callback in all languages.
See https://github.com/google/or-tools/blob/stable/ortools/sat/docs/solver.md#c-code-4

Related

How to use LinearSvm?

Currently I'm using FastTree for binary classification, but I would like to give SVM a try and compare metrics.
All the docs mention LinearSvm, but I can't find code example anywhere.
mlContext.BinaryClassification.Trainers does not have public SVM trainers. There is LinearSvm class and LinearSvm.TrainLinearSvm static method, but they seem to be intended for different things.
What am I missing?
Version: 0.7
For some reason there is no trainer in the runtime API but there is a linear SVM trainer in the Legacy API (for v0.7) found here. They might be generating a new one for the upcoming API, so my advice is to either use the legacy one, or wait for a newer API.
At this stage, ML.Net is very much in development.
Copy pasting the response I got on Github:
I have two answers for you: What the status of the API is, and how to use the LinearSVM in the meantime.
First, we have LinearSVM in the ML.NET codebase, but we do not yet have samples or the API extensions to place it in mlContext.BinaryClassification.Trainers. This is being worked through in issue #1318. I'll link this to that issue, and mark it as a bug.
In the meantime, you can use direct instantiation to get access to LinearSVM:
var arguments = new LinearSvm.Arguments()
{
NumIterations = 20
};
var linearSvm = new LinearSvm(mlContext, arguments);
var svmTransformer = linearSvm.Fit(trainSet);
var scoredTest = svmTransformer.Transform(testSet);
This will give you an ITransformer, here called svmTransformer that you can use to operate on IDataView objects.

Reverse engineering the checksum algorithm

I have an IP camera that receives commands using POST HTTP requests(for example to call PTZ commands or set various camera settings). The standard way of controlling it is through it's own web interface which is partially an ActiveX plugin and partially standard html+js. Of course because of the ActiveX part it only works in IE under Windows.
I'm attempting to change that by figuring out all the commands and writing a small python or javascript code to do the same, so that it is more cross platform.
I have one major problem. Each POST request contains a calculated "cc" field which I assume is a checksum. The JS code in the cam interface points out that it is calculated by calling a function inside the plugin:
tt = new Date().Format("yyyyMMddhhmmss");
jo_header["tt"] = tt;
if (getCpPlugin() != null && getCpPlugin().valid) {
jo_header["cc"] = getCpPlugin().nsstpGetCC(tt, session_id);
}
nsstpGetCC function obviously calculates the checksum from two parameters the timestamp and session_id. Real example(captured with Wireshark):
tt = "20171018231918"
session_id = "30303532646561302D623434612D3131"
cc = "849e586524385e1071caa4023a3df75401e5bb82"
Checksum seems to be 160bit. I tried both sha-1 and ripemd-160 and all combinations of concatenating tt and session_id I could think of. But I can't seem to get the same hash as the one the original plugin gets. The plugin dll seems to be written in c++. And I have almost no experience with decompilation to dive into this problem from that angle.
So my question basically is can someone figure out how they calculated that cc, or at least give me an idea in which direction to research further. Maybe I'm looking at wrong hash algorithms or something... Or give me some idea how I could somehow figure out what the original ActiveX function nsstpGetCC is doing for example by decompilation or maybe by monitoring it's operation in memory while running. What tools should I use?

How to find documentation for available standard methods in Python 2.7

I'm new to python, and while it's a pretty simple language, I'm having a hard time finding a solid and easy to read language reference that lists all the supported build-in methods and libraries that come with the installation. The main documentation site is confusing. There's more info about what's deprecated than what's recommended. I tried using pydoc to find method usage. For example, I want to see a simple list of all the methods that are part of the string class (e.g. replace(), toupper(), etc). But I'm not sure how to use it to list the methods, or to list a method and its usage. What do people use for a quick reference that works?
When I do something like 'pydoc string', I see a message that says "Warning: most of the code you see here isn't normally used nowadays.
Beginning with Python 1.6, many of these functions are implemented as
methods on the standard string object. They used to be implemented by
a built-in module called strop, but strop is now obsolete itself."
So while there's info about the method replace() there, I'm worried that it's not the right info based on that warning. How can I see the methods of the standard string object?
Documentation about standard functions:
https://docs.python.org/2/library/functions.html
Documentation about standard libraries:
https://docs.python.org/2/library/
You could use dir() and help(). i.e. :
From python shell :
>>> import math
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>> help(math.tan)
Will print :
Help on built-in function tan in module math:
tan(...)
tan(x)
Return the tangent of x (measured in radians).
(press "q" to exit the help page)
Hope it helps.
EDIT
Another solution from the shell :
$ python -m pydoc sys
Then press "q" to exit.

How to test asynchronuous code

I've written my own access layer to a game engine. There is a GameLoop which gets called every frame which lets me process my own code. I'm able to do specific things and to check if these things happened. In a very basic way it could look like this:
void cycle()
{
//set a specific value
Engine::setText("Hello World");
//read the value
std::string text = Engine::getText();
}
I want to test if my Engine-layer is working by writing automated tests. I have some experience in using the Boost Unittest Framework for simple comparison tests like this.
The problem is, that some things I want the engine to do are just processed after the call to cycle(). So calling Engine::getText() directly after Engine::setText(...) would return an empty string. If I would wait until the next call of cycle() the right value would be returned.
I now am wondering how I should write my tests if it is not possible to process them in the same cycle. Are there any best practices? Is it possible to use the "traditional testing" approach given by Boost Unittest Framework in such an environment? Are there perhaps other frameworks aimed at such a specialised case?
I'm using C++ for everything here, but I could imagine that there are answers unrelated to the programming language.
UPDATE:
It is not possible to access the Engine outside of cycle()
In your example above, std::string text = Engine::getText(); is the code you want to remember from one cycle but execute in the next. You can save it for later execution. For example - using C++11 you could use a lambda to wrap the test into a simple function specified inline.
There are two options with you:
If the library that you have can be used synchronously or using c++11 futures like facility (which can indicate the readyness of the result) then in your test case you can do something as below
void testcycle()
{
//set a specific value
Engine::setText("Hello World");
while (!Engine::isResultReady());
//read the value
assert(Engine::getText() == "WHATEVERVALUEYOUEXPECT");
}
If you dont have the above the best you can do have a timeout (this is not a good option though because you may have spurious failures):
void testcycle()
{
//set a specific value
Engine::setText("Hello World");
while (Engine::getText() != "WHATEVERVALUEYOUEXPECT") {
wait(1 millisec);
if (total_wait_time > 1 sec) // you can put whatever max time
assert(0);
}
}

wxGetElapsedTime function in wxWidgets 2.9+

I'm trying to follow some old C++ tutorial which use old version o wxWidgets framework (2.6 I think), but I have complied version 2.9.1, where I could not find wxGetElapsedTime() and wxStartTimer() methods.
The elapsed time method is used as argument to srand() method to 'randomize' initial random seed value and the author of the tutorial use after that wxStartTimer() to reset timer to zero (instead of use wxGetElapsedTime(true)).
srand(wxGetElapsedTime(false));
wxStartTimer();
After some search on Google I found, that those methods are deprecated, but I could not find some clue which functions are used in 2.9+ wxWidgets instead.
try
srand((int)wxGetLocalTime());
and
wxStopWatch * stopWatch = new wxStopWatch();
updateSomethingByTime( stopWatch->Time() );
You could try the wxStopWatch class it has methods like those for measuring times. Not sure how you'd get the system uptime for seeding srand() though, although you could get the current time from wxDateTime and use that.