CPLEX debug window freezes - c++

I am trying to code my math model in CPLEX language using C++ (Concert Technology). when I run my code in the compiler window read my inputs and freezes. when trying to debug my code, the visual studio shows the following line as a breakpoint.
//constraint 2
for (cc = 0; cc < NumberOfCourses; cc++) {
IloExpr Constraint2(env);
for (rr = 0; rr < AvailableRooms; rr++) {
Constraint2 += RoomCapacity[rr] * Y[cc][rr];
mod.add(Constraint2 >= Students[cc]); // this line
Constraint2.end();
}
}
i have set up the parameters and variables as follow:
double RoomCapacity[AvailableRooms];
double Students[NumberOfCourses];
//Ycr
IloBoolVarArray2 Y(env, NumberOfCourses);
for (cc = 0; cc < NumberOfCourses; cc++)
Y[cc] = IloBoolVarArray(env, AvailableRooms);
I do not understand what is wrong with my constraint!

Your code is wrong: In the very first inner iteration you add Constraint2 to your model and then you end() that variable. So in the second inner iteration, you are doing Constraint2 += ... while the Constraint2 object was already deleted. This will lead to undefined behavior (hang, crash, ...).
I guess what you wanted to write is this (pull add() and end() out of the inner loop):
for (cc = 0; cc < NumberOfCourses; cc++) {
IloExpr Constraint2(env);
for (rr = 0; rr < AvailableRooms; rr++) {
Constraint2 += RoomCapacity[rr] * Y[cc][rr];
}
mod.add(Constraint2 >= Students[cc]);
Constraint2.end();
}

Related

How to write a reccurence relation for an iterative function

Currently I'm working project which I have to provide an reccurence relation of a function. The thing is that the function is an iterative function and I did not manage to write its reccurence relation. In addition, I am not sure if such a thing is possible or not. Is there any way to write a reccurence relation for an iterative function.
Here is my code that I struggle with :
typedef Pair<Job*>* Schecule;
vector<Schecule>* scheculeJobs(Job* jobs[],int jobCount){
mergeSort(jobs,0,jobCount - 1 ); // O(nlogn)
vector<Schecule>* schedules = new vector<Schecule>();
for (int i = 0; i < jobCount; i++){
Job* firstJob = jobs[i];
for(int j = i +1 ; j < jobCount ; j++){
Job* secondJob = jobs[j];
Schecule schedule = new Pair<Job*>(firstJob,secondJob);
schedules->push_back(schedule);
if(schedule->first->deadline == schedule->second->deadline){
Schecule inverseSchedule = new Pair<Job*>
(secondJob,firstJob);
schedules->push_back(inverseSchedule);
}
}
}
return schedules;
}

Why do I get this C++ error (terminate called after throwing an instance of 'std::bad_alloc') when I am playing the code?

I wrote the code pasted below to perform a method that starts monotrack and generates tracks.
void KalmanTracker::TrackInitiation(AssignData *track_, vector<int> *CT_order_, Camera *cam)
{
vector<int> CT_order= *CT_order_;
Functions f;
double T,X_MAX,X_MIN,Y_MAX,Y_MIN;
int numtracks = 0;
for(int curr_p=0; curr_p<track_->num_DTBP; curr_p++) { //plot in the current frame
if(track_->Data_TBP.at(curr_p).Free == false) {
for(int ant_p=0; ant_p<track_->num_DANT; ant_p++) { //plot in the previous frame
T = track_->Data_TBP.at(curr_p).Dato.t_obt - track_->Data_ANT.at(ant_p).Dato.t_obt;
X_MAX = track_->Data_ANT.at(ant_p).Dato.Est_PosXY.at(Xx) + MAX_SPEED*T + 3.0*sqrt(2.0*RX);
X_MIN = track_->Data_ANT.at(ant_p).Dato.Est_PosXY.at(Xx) - MAX_SPEED*T - 3.0*sqrt(2.0*RX);
Y_MAX = track_->Data_ANT.at(ant_p).Dato.Est_PosXY.at(Yy) + MAX_SPEED*T + 3.0*sqrt(2.0*RY);
Y_MIN = track_->Data_ANT.at(ant_p).Dato.Est_PosXY.at(Yy) - MAX_SPEED*T - 3.0*sqrt(2.0*RY);
if((track_->Data_TBP.at(curr_p).Dato.Est_PosXY.at(Xx) < X_MAX) && (track_->Data_TBP.at(curr_p).Dato.Est_PosXY.at(Xx) > X_MIN) &&
(track_->Data_TBP.at(curr_p).Dato.Est_PosXY.at(Yy) < Y_MAX) && (track_->Data_TBP.at(curr_p).Dato.Est_PosXY.at(Yy) > Y_MIN)) {
if((track_->Data_TBP.at(curr_p).Dato.Area > 10) && (track_->Data_ANT.at(ant_p).Dato.Area > 10) &&
(track_->Data_TBP.at(curr_p).Dato.InBox == false) && (track_->Data_ANT.at(ant_p).Dato.InBox == false)) {
f.InitiateMonotrack(track_,track_->Data_ANT.at(ant_p).Dato,track_->Data_TBP.at(curr_p).Dato,&CT_order,numtracks); //Start the monotrack
numtracks++;
track_->Data_TBP.at(curr_p).Free = true;
}
}
}
}
}
// Reordering and saving CT_order
int num_non_discarded = 0;
for(int i=0; i<track_->num_CT+numtracks; i++) {
if (CT_order.at(i) != -1) {
CT_order.at(num_non_discarded) = CT_order.at(i);
num_non_discarded++;
}
}
track_->num_CT = min(num_non_discarded,MAX_CT-1);
// Copy CT_order to the CT_order of the track
for(int i=0; i<track_->num_CT; i++)
track_->CT_order.at(i) = CT_order.at(i);
// Releasing previous plots
int num_ANT = 0;
for(int i=0; i<track_->num_DANT; i++)
track_->Data_ANT.at(i).Free = true;
// Copy the current plot to previous plot
for(int curr_p=0; curr_p<track_->num_DTBP; curr_p++) {
if (track_->Data_TBP.at(curr_p).Free == false) {
track_->Data_ANT.at(num_ANT).Dato.t_obt = track_->Data_TBP.at(curr_p).Dato.t_obt;
track_->Data_ANT.at(num_ANT).Dato.Est_PosXY.resize(2);
track_->Data_ANT.at(num_ANT).Dato.Est_PosXY.at(Xx) = track_->Data_TBP.at(curr_p).Dato.Est_PosXY.at(Xx);
track_->Data_ANT.at(num_ANT).Dato.Est_PosXY.at(Yy) = track_->Data_TBP.at(curr_p).Dato.Est_PosXY.at(Yy);
track_->Data_ANT.at(num_ANT).Dato.Area = track_->Data_TBP.at(curr_p).Dato.Area;
track_->Data_ANT.at(num_ANT).Dato.InBox = track_->Data_TBP.at(curr_p).Dato.InBox;
track_->Data_TBP.at(curr_p).Dato.image.copyTo(track_->Data_ANT.at(num_ANT).Dato.image);
track_->Data_ANT.at(num_ANT).Free = false;
num_ANT++;
}
}
track_->num_DANT = min(num_ANT,MAX_DTBP-1);
// Releasing current plots
for(int curr_p=0; curr_p<track_->num_DTBP; curr_p++)
track_->Data_TBP.at(curr_p).Free = true;
track_->num_DTBP = 0;
*CT_order_= CT_order;
}
When I execute the .cpp file I keep getting the error message:
terminate called after throwing an instance of 'std::bad_alloc'
terminate called recursively
what(): std::bad_alloc
I have gathered this has to do with a memory shortage or variables falling out of the main() function, but I can not figure out how to address the problem in this specific situation. If it is relevant, I am working on a Linux computer, programming in eclipse, using C++ language and opencv and boost libraries, because my project has two threads.
[EDIT] The debugger tells me:
Can't find a source file at "/build/glibc-t3gR2i/glibc-2.23/signal/../sysdeps/unix/sysv/‌​linux/raise.c" Locate the file or edit the source lookup path to include its location.
And the source not found is __GI_raise() at raise.c.
And after that error, the console tells me:
[xcb] Unknown sequence number while processing reply
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that. dronesNuevo: ../../src/xcb_io.c:635: _XReply: Assertion `!xcb_xlib_threads_sequence_lost' failed.
But other times the debugger tells me:
Thread 3 received signal SIGSEGV, Segmentation fault.
[Cambiando a Thread 0x7fffe1f69700 (LWP 17536)] 1574 ../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S: No existe el archivo o el directorio.
__memmove_ssse3_back () at ../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S:1574
and:
Can't find a source file at "/build/glibc-t3gR2i/glibc-2.23/string/../sysdeps/x86_64/mul‌​tiarch/memcpy-ssse3-‌​back.S"
Locate the file or edit the source lookup path to include its location.
Being the source not found __memmove_ssse3_back() at memcpy-sse3-back.S
The backtrace that I get:
*** Error in `path': double free or corruption (!prev): 0x00007fffa007ecb0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7ffff5e577e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x7fe0a)[0x7ffff5e5fe0a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7ffff5e6398c]
path[0x40d3b6]
path[0x40cc26]
path[0x40bd78]
path[0x40b3a2]
path[0x409f3f]
path[0x40813b]
path[0x412bd7]
path[0x43df93]
path[0x44826b]
path[0x447f38]
path[0x447ac2]
/usr/lib/x86_64-linux-gnu/libboost_thread.so.1.58.0(+0x115d5)[0x7ffff6a5b5d5]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x76ba)[0x7ffff59b06ba]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7ffff5ee682d]

Jogl crashing in the shader initialization, at the glLinkProgram

We got a new notebook with an Nvidia 880m
it came with win8.1, we installed the 7 x64
Trying to execute jogl, java is crashing
http://pastebin.com/gv6117NK
it crashes when glLinkProgram is called
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j jogamp.opengl.gl4.GL4bcImpl.dispatch_glLinkProgram1(IJ)V+0
j jogamp.opengl.gl4.GL4bcImpl.glLinkProgram(I)V+46
j javax.media.opengl.DebugGL4bc.glLinkProgram(I)V+9
j glsl.GLSLProgramObject.initializeProgram(Ljavax/media/opengl/GL3;Z)V+113
I am creating the shader using some code that I wrote in past, it always worked and I never got problems, so I am afraid it has something to do with the nvidia opengl driver..
I am initializing the program in this way
public final void initializeProgram(GL3 gl3, boolean cleanUp) {
_progId = gl3.glCreateProgram();
for (int i = 0; i < _vertexShaders.size(); i++) {
gl3.glAttachShader(_progId, _vertexShaders.get(i));
}
for (int i = 0; i < _fragmentShaders.size(); i++) {
gl3.glAttachShader(_progId, _fragmentShaders.get(i));
}
gl3.glLinkProgram(_progId);
int[] params = new int[]{0};
gl3.glGetProgramiv(_progId, GL3.GL_LINK_STATUS, params, 0);
if (params[0] != 1) {
System.err.println("link status: " + params[0]);
gl3.glGetProgramiv(_progId, GL3.GL_INFO_LOG_LENGTH, params, 0);
System.err.println("log length: " + params[0]);
byte[] abInfoLog = new byte[params[0]];
gl3.glGetProgramInfoLog(_progId, params[0], params, 0, abInfoLog, 0);
System.err.println(new String(abInfoLog));
}
gl3.glValidateProgram(_progId);
if (cleanUp) {
for (int i = 0; i < _vertexShaders.size(); i++) {
gl3.glDetachShader(_progId, _vertexShaders.get(i));
gl3.glDeleteShader(_vertexShaders.get(i));
}
for (int i = 0; i < _fragmentShaders.size(); i++) {
gl3.glDetachShader(_progId, _fragmentShaders.get(i));
gl3.glDeleteShader(_fragmentShaders.get(i));
}
}
}
Any tips?
Both 337 and 340 drivers fail
As jmaasing suggested, I had to force the use of the dedicated graphic card
I also had to add "C:\Program Files\Internet Explorer" to the path for the IEShims.dll and update the retail win7 to the sp1 and various updates

Can Allegro update the number of joysticks at runtime?

Is there a way to update the number of joysticks plugged in at run-time other than constantly calling remove_joystick() then install_joystick? This proves to be extremely slow (goes from 60 FPS to around 5).
Allegro 4.2 answers only please...
void Joystick::Update() {
//If joystick input was lost, attempt to reacquire.
if(GetNumJoysticks() == 0) {
throw InputNotAvailableException("Joystick");
}
//If all joysticks were deleted remove input and do nothing.
if(_numjoysticks == 0) {
remove_joystick();
return;
}
//Update state information
if(poll_joystick() < 0) {
throw InputNotAvailableException("Joystick");
}
for(int i = 0; i < _numButtons; ++i) {
_prevButtons[i].b = _curButtons[i].b;
_prevButtons[i].name = _curButtons[i].name;
_curButtons[i].b = joy[_joyNumber].button[i].b;
_curButtons[i].name = joy[_joyNumber].button[i].name;
}
for(int i = 0; i < _numSticks; ++i) {
for(int j = 0; j < joy[_joyNumber].stick[i].num_axis; ++j) {
_prevSticks[i].axis[j].name = _curSticks[i].axis[j].name;
_prevSticks[i].axis[j].pos = _curSticks[i].axis[j].pos;
_prevSticks[i].axis[j].d1 = _curSticks[i].axis[j].d1;
_prevSticks[i].axis[j].d2 = _curSticks[i].axis[j].d2;
_curSticks[i].axis[j].name = joy[_joyNumber].stick[i].axis[j].name;
_curSticks[i].axis[j].pos = joy[_joyNumber].stick[i].axis[j].pos;
_curSticks[i].axis[j].d1 = joy[_joyNumber].stick[i].axis[j].d1;
_curSticks[i].axis[j].d2 = joy[_joyNumber].stick[i].axis[j].d2;
}
_prevSticks[i].flags = _curSticks[i].flags;
_prevSticks[i].name = _curSticks[i].name;
_curSticks[i].flags = joy[_joyNumber].stick[i].flags;
_curSticks[i].name = joy[_joyNumber].stick[i].name;
}
}
int Joystick::GetNumJoysticks() {
remove_joystick();
if(install_joystick(JOY_TYPE_DIRECTX)) {
return 0;
}
return (num_joysticks);
}
The 4.x series does not. The 5.x series does.
You'll have to either listen for native OS events using custom platform specific code (assuming such things exist) and only call the Allegro deinit/init functions when a change is detected, or require the user to initiate joystick refresh manually.
Under Linux, you could inotify_add_watch() /dev/input to check for changes. Looking at the 4.4 Allegro code, looks like you'd want to call the Win32 functions joyGetNumDevs() and joyGetPos(). Something like:
int WIN_MAX_JOYSTICKS = joyGetNumDevs(); // this should never change
JOYINFO ji;
int pluggedin_count = 0;
for (int i = 0; i < WIN_MAX_JOYSTICKS; ++i)
if (joyGetPos(i, &ji) == JOYERR_NOERROR) ++pluggedin_count;
if (pluggedin_count != last_pluggedin_count) /* reinit Allegro */
You'd have to do that every N seconds.
Those joy* functions are Windows functions, so read MSDN docs to learn how to use them.

How to pause a program for a few milliseconds?

How to pause a program for a few milliseconds using C++ managed code?
I tried Sleep() but it didn't work when I included the winbase.h file, I got lots of compile errors!
for(int i=0; i<10; i++)
{
simpleOpenGlControl1->MakeCurrent();
System::Threading::Thread::Sleep(100);
theta_rotated += 2.0* 3.141592653/(double)Model->size()/10.0;
simpleOpenGlControl1->Invalidate();
}
private: System::Void simpleOpenGlControl1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
{
Gl::glMatrixMode(Gl::GL_MODELVIEW);
Gl::glLoadIdentity();
Gl::glClearColor(1, 1, 1, 1);
Gl::glClear(Gl::GL_COLOR_BUFFER_BIT | Gl::GL_DEPTH_BUFFER_BIT);
camera->Tellgl();
Gl::glRotated(Angle,X,Y,Z);
RenderModels();
}
void RenderModels(void)
{
Points = gcnew System::Collections::Generic::List<Point>();
Point p(0,0) ;
int t = -5;
double r = 5;
double x, z, theta;
for(int i = 0;i < Model->size();i++)
{
theta = i*2* 3.141592653/ (double)Model->size();
x = r*sin(theta + theta_rotated);
z = r*cos(theta + theta_rotated);
Gl::glPushMatrix();
Gl::glTranslated(x,0,z);
Model->at(i).Render();
p.X=x;
p.Y=0;
Points->Add(p);
t +=1;
Gl::glPopMatrix();
}
//simpleOpenGlControl1->Invalidate();
}
You want System::Threading::Thread::Sleep().
http://msdn.microsoft.com/en-us/library/system.threading.thread.sleep.aspx
System::Threading::Thread::Sleep() as mentioned in another answer. But let me warn you, it is not precise, and for small (milliseconds) Sleep is extremely imprecise. consider that your app run together with other apps and threads and they all want processor time.
#include <unistd.h>
main() {
usleep(2000); // 2 msecs
}
Read the man pages ;-)
man 3 usleep