How to print out all constraints in Gurobi C++? - gurobi

I am using Gurobi now but the model turns out to be infeasible, so I am trying to print out all the constraints to see if I made mistakes. I know a few functions on how to print out the names of each constraint, but just couldn't find the final solution to print the constraints themselves(the mathematical expressions).
GRBConstr *c=0;
c = model.getConstrs();
for(int i=0;i<model.get(GRB_IntAttr_NumConstrs);++i){
cout << c[i].get(GRB_StringAttr_ConstrName) << endl;
}

To debug a model, the best option is to write the model file in LP format. In your example, add the code:
model.update();
model.write("debug.lp");
Then browse the file debug.lp in your favorite text editor.

Related

Read file to String when a new document is created in Chromium?

I am looking to read a file to string so that I can execute it's contents (JS) when any new document (tab, iframe, etc) is created.
This is not an Extension but C++ code being built into a standalone binary
I have the following code -
InjectionRenderFrameObserver::DidCreateNewDocument(){
std::string jq;
base::FilePath path("jQuery.js");
LOG(INFO) << "FilePath::" << path.value();
if(base::PathExists(path)){
LOG(INFO) << "FileExists::";
}
if(!base::ReadFileToString(path, &jq)){
LOG(ERROR) << "Can't read content of '" << path.value() << "'.";
}
}
The Console logs the following:
[967856:1:1122/1950927.423702:INFO:injection_render_frame_observer.cc(16)] FilePath::jQuery.js
[967856:1:1122/1950927.423981:INFO:injection_render_frame_observer.cc(20)] Can't read content of 'jQuery.js'.
I haven't been able to connect a debugger to it yet (having some system issues) but was wondering if there is an obvious issue that would prevent the above from working that I am missing since the Chromium Dev wiki/knowledge space is limited for new developers (or at least I haven't found a good source yet).
The above code is my example code (what I am starting with) to get a file loaded. The end goal will be a feature switch that allows a default or provided path to script that will run before each document is created.

Export PDF page labels on command line

I'd like to export the page-labels stored in some PDF documents for easy parsing. I know I could dig into the PDF document after having it converted with qpdf, but this seems like overkill.
Is there no commandline tool that will simply print the page label for each page (or together with other meta-data)? I know that PDFSpy will export the label, but $300 isn't an option, preferably the solution should be free.
Short answer:
I am not aware of any (free) tool that can 'simply print' the page label for each page.
Also, you'll not be able to evade the expansion compressed objects and object streams, using a tool like qpdf or one with equivalent capabilities.
Long answer:
There's no such tool because these are the only a few things you can safely rely on when it comes to page labels. These are the following:
Each PDF document must contain a root object.
That root object must be of /Type /Catalog.
The document's trailer will show where to find the object using the key /Root followed by the indirect object number reference.
IF a PDF document uses non-standard page labels, then the document root object must have an entry named /PageLabels.
Here is where it stops to be relatively easy. Because the object the /PageLabels key refers to may be contained in a compressed object stream. This means that you'd have to expand that object stream.
If you really succeeded to get the description of the page labels as ASCII, you'll discover that it's not an easily parseable flat list (like a dictionary is): it is a number tree.
I'll not go into the details of these complexities, because it would take a very long article to describe all possible variations. You better read it up directly in the official ISO PDF-1.7 specification.
But instead I'll give you an example in ASCII PDF code:
213 0 obj
<< /Type /Catalog
/PageLabels
<<
/Nums
[
0 << % start labeling from page no. 1
/S /r % label with lowercase roman numbers
>>
7 << % start new labeling from page no. 8
/S /D % label with standard decimal numbers
>>
11 << % start labeling page no. 12
/S /D % label with decimal numbers...
/P (ABCD-) % ...but using label prefix 'ABCD-'...
/St 3 % ...followed by '3' as the start decimal.
>>
]
>>
%%...........................
%%...more root object keys...
%%...........................
>>
endobj
The above example will label the pages number 1, 2, 3, ... (last) like this:
i
ii
iii
iv
v
vi
1
2
3
4
ABCD-3
ABCD-4
ABCD-5
ABCD-6
...and so on until last page...
As you can see, the PDF method of labeling pages (mapping page numbers to page names) is completely non-intuitive. You can only understand it by studying the PDF specification.
I've written a small command-line utility based on Poppler that does just this task: https://github.com/HeimMatthias/pdfpagelabels
Disclaimer: I'm the OP and created the original post under a different account. I have been using the solution via pdftk (listed in a comment above) successfully for years in my implementation. However, last year it was time to reimplement our system from scratch and we've had numerous instances where the pdf-tk output could not be parsed by our implementation.
The new command-line tool follows the philosophy of doing just one thing, but doing it well, and simply prints the page labels of all or selected pages of a pdf-file. If anyone finds this useful, and stumbles upon it here, all the better for it.

locations of loops in source files

In my pass, I'd like to know the locations of loops. For example, in a for loop, such as:
for(int i=0; i<n; i++) { ... }
The line number of for(...) in the source file is what I am interested in. If the .bc file is generated by llvm-gcc with -O0, I can easily get this information by reading the line number of the first instruction of the loop. However, if -O3 is used, this method does not work. How can I still get the loop locations in this case?
In general you cannot, because your loop might be transformed by compiler (e.g. unrolled, reversed, etc.)

Save a YAML Emitter content to a file with YAML-CPP

I just started playing around with yaml-cpp, I managed to build it properly and run some of the examples from the yaml-cpp wiki but I can't find a way to save my emitter to a file.
Is this not possible? I mean the PyYAML library has a 'dump' function for this. Is there no such functionality in yaml-cpp?
Is there some workaround to converting a yaml emitter to a stl stream and then dumping this to a yaml file?
Please let me know
Thanks,
Adam
The function Emitter::c_str() returns a NULL-terminated C-style string (which you do not have to release), which you can then write to a file. For example:
YAML::Emitter emitter;
emitter << "Hello world!";
std::ofstream fout("file.yaml");
fout << emitter.c_str();
There is also Emitter::size(), which returns the number of bytes in that string, in case you want to do something more advanced and don't want to walk the string to find its length.
If you want to just dump a Node to a stream, there's a shortcut:
YAML::Node node = ...;
std::ofstream fout("file.yaml");
fout << node;

Is there a length limit on g++ variable names?

See title​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
Short Answer:
No
Long Answer:
Yes, it has to be small enough that it will fit in memory, but otherwise no, not really. If there is a builtin limit (I don't believe there is) it is so huge you'd be really hard-pressed to reach it.
Actually, you got me really curious, so I created the following Python program to generate code:
#! /usr/bin/env python2.6
import sys;
cppcode="""
#include <iostream>
#include <cstdlib>
int main(int argc, char* argv[])
{
int %s = 0;
return 0;
}
"""
def longvarname(n):
str="x";
for i in xrange(n):
str = str+"0";
return str;
def printcpp(n):
print cppcode % longvarname(n);
if __name__=="__main__":
if len(sys.argv)==2:
printcpp(int(sys.argv[1]));
This generates C++ code using the desired length variable name. Using the following:
./gencpp.py 1048576 > main.cpp
g++ main.cpp -o main
The above gives me no problems (the variable name is roughly 1MB in length). I tried for a gigabyte, but I'm not being so smart with the string construction, and so I decided to abort when gencpp.py took too long.
Anyway, I very much doubt that gcc pre-allocates 1MB for variable names. It is purely bounded by memory.
an additional gotcha, some linkers have a limit on the length of the mangled name. this tends to be an issue with template and nested classes more than identifier length but either could trigger a problem afaik
I don't know what the limit is (or if there is one), but I think it is good practice that there should be one, in order to catch pathological code, for example that created by a runaway code generator. For what it's worth, the C++ Standard suggests a minimum of 1K for identifier length.