Hello,
I am coding a mathematical model with VC++. When I debug the code, I
face with no erroe, but during executing, I face with below error
"Unhandled exception at 0x0040c275 in Tar.exe: 0xC0000005: Access
violation reading location 0x00000004".
Could you tell me, how I can fix it? I have attached the code as
follows:
The code stops because of the erroe in the line including return of
below function.
//********************************************
ILOBPCOLUMN3(Schedule,
IloInt,Obje,
IloIntArray, A,
IloIntArray, B) {
printf("**************************\n");
cout << Obje << endl;
cout << A << endl;
cout << B << endl;
return Obj(Obje) + con(A) + con1(B);
}
//************************************************
// -------------------------------------------------------------- -*-
C++ -*-
// File: examples/src/knapsack.cpp
// --------------------------------------------------------------------------
#include <ilmaestro/ilomaestro.h>
#include <ilmaestro/ilobpgoals.h>
ILOSTLBEGIN
static void Get_Data(void);
static void Get_RMP1(void);
FILE* INDATA;
FILE* First_RMP;
//*** Defining Arrays **********************************
typedef IloArray<IloNumArray> Num2D;
typedef IloArray<Num2D> Num3D;
typedef IloArray<Num3D> Num4D;
typedef IloArray<IloNumArray> IloNumArray2D;
typedef IloArray<IloNumArray2> IloNumArray3;
typedef IloArray<IloNumArray3> IloNumArray4;
//************************************************************************************
// Variables ***********************************************************************
int f1,f2,f3,f4,f5,P,L,J,J1,i1,i2,q,k,k1,group_number,group_number1,group,Real_Jobs,Max_Job,Group,Sum;
int job_number[20],M[7],Machine_Number,Criteria,Max_Setup,t1,Max_Run[7],MaximumRun,
Iteration;
int time_machine[20][20][7],time_machine1[20][20][7],set_up[40][40][5],Run[5][20],RunTime[7],Max_RunTime;
double m,N1,d[20][20][20],Subs[7],Subs1[7];
int Feas_Sol,RMP_W[8000][20][20],RMP_Competion_Time[8000][20][20][7],count,G,Indicator[20];
int X_values[20][20][7],W_values[20][20][7],G_Sequence[20],G_Sequence1[7][20],YY1[20][20][7],J_Sequence[20][20],sequence[20][20],sequence1[7][20][20];
double CC1[20][7],XX1[20][20][7],min_time[20][20],Lower_Bound[50000],Winner;
int FlowTime,Flow_Time[20][20][7],Chosen;
double Start_Time,Finish_Time,Difference_Time,Best_Time,Stage_Time[5],Stage_Solution[5],Lamdaa[10][50000];
int Coef_Lamda[7][500][17][10],Lam[5][5],Constraints_Counter,Constraints_Counter1;
int b,b1,b2,b3,Columnf[50][50];
class Seq{
public:
IloInt Obje;
IloIntArray A;
IloIntArray B;
};
// Model
IloObjective Obj;
IloRangeArray con;
IloRangeArray con1;
IloInt Obje;
IloIntArray Constraintss;
IloIntArray Constraintss1;
IloIntArray A;
IloIntArray B;
//**************************************************************************
//**************************************************************************
ILOBPCOLUMN3(Schedule,
IloInt,Obje,
IloIntArray, A,
IloIntArray, B) {
printf("**************************\n");
cout << Obje << endl;
cout << A << endl;
cout << B << endl;
return Obj(Obje) + con(A) + con1(B);
}
int main(int argc, char** argv) {
//int main() {
IloEnv env;
try {
Get_Data();
Get_RMP1();
//***************************************
// Create Master Problem
//***************************************
IloModel masterModel(env);
IloObjective Obj=IloMinimize(env);
f1=Max_Job*group_number;
con = IloRangeArray(env,f1);
f1=Machine_Number;
con1= IloRangeArray(env,f1);
for (f1=2; f1<=Machine_Number; f1++){
for (f2=1; f2<=group_number; f2++){
for (f3=1; f3<=Max_Job; f3++){
con.add(IloRange(env,0,30000));
}
}
}
for (f1=1; f1<=Machine_Number; f1++){
con1.add(IloRange(env,1,1));
}
masterModel.add(Obj);
masterModel.add(con);
masterModel.add(con1);
// Create the BPC algorithm and extract the master model.
IloMaestro maestro(env);
maestro.extract(masterModel);
printf(" \n\nAAAA\n\n");
// Create and Add Initial Columns
f1 = (Max_Job*group_number) + Machine_Number +1;
Constraintss = IloIntArray(env,f1);
f1= Max_Job*group_number;
A = IloIntArray(env,f1);
B = IloIntArray(env,Machine_Number);
k=1;
for(b=1; b<=2*Feas_Sol;b++){
Constraintss[0]=Columnf[b][0];
for (b1=1;b1<=Max_Job*group_number + Machine_Number; b1++){
Constraintss[b1]=Columnf[b][b1];
}
Obje= Constraintss[0];
for(f5=1 ; f5 <= Max_Job*group_number; f5++){
A[f5-1]= Constraintss[f5];
}
B[0] = Constraintss[Max_Job*group_number+1];
B[1] = Constraintss[Max_Job*group_number+2];
maestro.addInitial(Schedule(env,Obje,A,B));
printf("\n k= %i\n",k);
cout << Obje << endl;
cout << A << endl;
cout << B << endl;
k=k+1;
}
printf(" \n\nBBBB\n\n");
cout << masterModel << endl;
printf(" \n\nCCCC\n\n");
// Solve using default search
IloBPDefaultSearch defaultSearch(env);
printf(" \n\nDDDD\n\n");
maestro.solve(defaultSearch);
printf(" \n\nEEEE\n\n");
// Display the solution
cout << "Primal Solution : " << maestro.getPrimalBound() <<
endl
<< maestro.getPrimalSolution() << endl;
maestro.end();
scanf(" %i\n");
} catch (IloException e) {
cerr << "Error " << e << endl;
}
env.end();
return 0;
}
//*************************************************************************************************************
// Initialize Values
*****************************************************************************************
//*************************************************************************************************************
void Get_Data(void){
// =========== Getting Number of groups and Read Data from Input
file =====
INDATA=fopen("ENTERA.txt","r");
fscanf(INDATA," %i\n",&group_number);
fscanf(INDATA," %i\n",&Machine_Number);
for(group=1;group<group_number+1;group++){
fscanf(INDATA," %i",&job_number[group]);
}
fscanf(INDATA,"\n");
for(f1=1;f1<=group_number;f1++){
for(f2=1;f2<=job_number[f1];f2++){
for(f3=1;f3<=Machine_Number;f3++){
fscanf(INDATA," %i" ,&time_machine[f1][f2][f3]);
}
}
fscanf(INDATA,"\n");
}
for(f1=0;f1<=group_number;f1++){
for(f2=0;f2<=group_number;f2++){
for(f3=1;f3<=Machine_Number;f3++){
fscanf(INDATA," %i",&set_up[f1][f2][f3]);
}
fscanf(INDATA,"\n");
}
}
fclose(INDATA);
// N:The number of real jobs in total
Real_Jobs=0;
for(f1=1;f1<=group_number;f1++){
Real_Jobs=Real_Jobs+job_number[f1];
}
N1= (1.0/Real_Jobs) ;
//Max_Job Finding the maximum job
Max_Job=job_number[1];
for(f1=2;f1<=group_number;f1++){
if(Max_Job<job_number[f1]){
Max_Job=job_number[f1];
}
}
// Run1[] The summation of runtimes in Machines
// Machine1
for(k=1;k<=Machine_Number;k++){
for(f1=1;f1<= group_number;f1++){
for(f2=1;f2<=job_number[f1];f2++){
Run[k][f1]= Run[k][f1] + time_machine[f1][f2][k];
RunTime[k]= RunTime[k] + time_machine[f1][f2][k];
if(Max_Run[k]<time_machine[f1][f2][k]){
Max_Run[k] = time_machine[f1][f2][k];
}
}
}
}
for(k=1;k<=Machine_Number;k++){
MaximumRun=MaximumRun+Max_Run[k];
}
Max_RunTime=RunTime[1];
for(k=2;k<=Machine_Number;k++){
if(Max_RunTime<RunTime[k]){
Max_RunTime=RunTime[k];
}
}
// Finding the Maximum Set-up Time
Max_Setup=0;
for(f1=0;f1<=group_number;f1++){
for(f2=0;f2<=group_number;f2++){
for(f3=1;f3<=Machine_Number;f3++){
if(f1!=f2){
if(Max_Setup < set_up[f1][f2][f3]){
Max_Setup = set_up[f1][f2][f3];
}
}
}
}
}
// Dummy Jobs
for(f1=1;f1<=group_number;f1++){
for(f2=1;f2<=job_number[f1];f2++){
for(f3=1;f3<=Machine_Number;f3++){
time_machine1[f1][f2][f3]=time_machine[f1][f2][f3];
}
}
}
t1= Max_RunTime + MaximumRun + (group_number+1)*Max_Setup;
for(f1=1;f1<=group_number;f1++){
if(job_number[f1]<Max_Job){
f2=Max_Job-job_number[f1];
for(f3=1;f3<=f2;f3++){
for(f4=1;f4<=Machine_Number;f4++){
time_machine1[f1][Max_Job-f3+1][f4] =-t1;
}
}
}
}
for(k=1;k<=Machine_Number;k++){
M[k]=t1;
}
}
//*******************************************************************************************
//*******************************************************************************************
void Get_RMP1(void){
First_RMP=fopen("RMP1.txt","r");
fscanf(First_RMP," %i\n",&Feas_Sol);
for(f1=1;f1<=Feas_Sol;f1++){
for(f2=1;f2<=group_number;f2++){
for(f3=1;f3<=group_number;f3++){
fscanf(First_RMP," %i",&RMP_W[f1][f2][f3]);
}
}
}
for(f1=1 ; f1<=Feas_Sol ; f1++){
for(f2=1 ; f2 <= group_number; f2++){
for(f3=1 ; f3 <= Max_Job; f3++){
for(f4=1;f4<=Machine_Number;f4++){
fscanf(First_RMP," %i",&RMP_Competion_Time[f1][f2][f3][f4]);
}
}
}
}
fclose(First_RMP);
for(f1=0;f1<=Feas_Sol;f1++){
for (f2=0; f2<=group_number; f2++){
for (f3=0; f3<=Max_Job; f3++){
for(f4=0;f4<=Machine_Number;f4++){
Coef_Lamda[f4][f1][f2][f3]=0;
}
}
}
}
for(f1=1;f1<=Feas_Sol;f1++){
for (f2=1; f2<=group_number; f2++){
for (f3=1; f3<=Max_Job; f3++){
Coef_Lamda[Machine_Number][f1][0][0]=Coef_Lamda[2][f1][0][0]+RMP_Competion_Time[f1][f2][f3][Machine_Number];
}
}
}
// Coefficients in Constraints
for(f1=1;f1<=Feas_Sol;f1++){
for (f2=1; f2<=group_number; f2++){
for (f3=1; f3<=Max_Job; f3++){
Coef_Lamda[2][f1][f2][f3]=RMP_Competion_Time[f1][f2][f3][2];
f5=0;
for (f4=1; f4<=group_number; f4++){
f5 = f5+ RMP_W[f1][f2][f4]*time_machine[f4][f3][2];
}
Coef_Lamda[2][f1][f2][f3]=Coef_Lamda[2][f1][f2][f3] - f5;
}
}
}
for(f1=1;f1<=Feas_Sol;f1++){
for (f2=1; f2<=group_number; f2++){
for (f3=1; f3<=Max_Job; f3++){
Coef_Lamda[1][f1][f2][f3]=(-1)*RMP_Competion_Time[f1][f2][f3][1];
}
}
}
f1=1;
b2=0;
for(b=1; b<=2*Feas_Sol;b++){
f2=1;
f3=1;
if(f1==1){
b2=b2+1;
}
Columnf[b][0]=Coef_Lamda[f1][b2][0][0];
for(b1 = 1;b1 <= Max_Job*group_number; b1++){
Columnf[b][b1]=Coef_Lamda[f1][b2][f2][f3];
f3=f3+1;
if(f3>Max_Job){
f3=1;
f2=f2+1;
}
}
if(f1==1){
Columnf[b][b1]=1;
Columnf[b][b1+1]=0;
}
else{
Columnf[b][b1]=0;
Columnf[b][b1+1]=1;
}
if(f1==1){
f1=2;
}
else{
f1=1;
}
}
}
Oleg Starodumov - 01 Dec 2004 08:48 GMT
> I am coding a mathematical model with VC++. When I debug the code, I
> face with no erroe, but during executing, I face with below error
[quoted text clipped - 3 lines]
>
> Could you tell me, how I can fix it?
1. Exclude the possibility of heap corruption by testing with Full PageHeap.
2. If the problem was not found by PageHeap, please post the call stack
at the moment of the exception.
How to enable Full PageHeap:
1) Download Debugging Tools for Windows package and install it:
http://www.microsoft.com/whdc/ddk/debugging/default.mspx
2) In the installation directory of Debugging Tools, find GFlags utility
and run it as the following:
gflags -p /enable YourApp.exe /full
(replace YourApp.exe with the real name of your executable)
3) Run the application under debugger (e.g. Visual Studio debugger).
When an error has been detected, you will be notified with an access violation exception
or a hard-coded breakpoint.
It is better if you test the application when it is linked to non-Debug CRT library.
More information about PageHeap:
http://support.microsoft.com/default.aspx?scid=kb;en-us;286470
Regards,
Oleg