how to do an iterated local search in c++?
iterated local search
Collapse
X
-
Tags: None
-
Originally posted by nurulshidanonihow to do an iterated local search in c++? -
[code=cpp]
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fstream>
#include < vector>
const class Localsearch* strtols(const char* params)
{
bool bestImpr = false;
bool bwd = false;
bool fwd = false;
bool swp = false;
int i = 1;
bool eos = false;
do
{
switch(params[i])
{
case 'B':
bestImpr = true;
break;
case 'F':
bestImpr = false;
break;
case 'b':
bwd = true;
break;
case 'f':
fwd = true;
break;
case 's':
swp = true;
break;
default:
eos = true;
break;
}
i=1, i<=6; i++;
} while(!eos);
return new Localsearch(bwd , fwd, swp, bestImpr);
}
int main(int argc, char* arg[])
{
char* instance = NULL;
bool print_eval = false;
bool print_trace = false;
int optimum = 0;
vector<const class Localserach*> ls;
char perturb = 'b';
int lower = 0;
int upper = 6;
int backtrack = 0;
int limit = 0;
unsigned int seed = time(NULL);
// Perturbation
unsigned int sz = candidate->size();
for(int k = 0; k < kicks; k++)
{
int a = rand() % sz;
int b;
do
{
b = rand() % size;
}
while(b==a);
unsigned int i = min(a);
switch(perturb)
{
case 'b':
candidate->doBwd(i);
break;
case 'f':
candidate->doFwd(i);
break;
case 's':
candidate->doSwp(i);
break;
default:
{
int mv = rand();
if(mv< RAND_MAX/3)
{
candidate->doBwd(i);
}
else if (mv <2*RAND_MAX/3)
{
candidate->doFwd(i);
}
else
{
candidate->doSwp(i);
}
}
}
}
// Local search
int start;
do
{
start = candidate->eval():
vector< const class Localsearch*>:: iterator it = ls.begin();
while(it !=ls.end() && !clock.alarm(li mit))
{
(*it)->search(candida te);
it++;
}
}
while(candidate->eval() < start && !clock. alarm(limit));
// Acceptance
if(candidate->eval()->=sol->eval())
{
kicks = (kicks , upper) ? kicks+1 : lower;
badSteps+;
if(badSteps > backtrack)
{
//candidate->clone(sol):
delete candidate;
sol->setSeq(sequenc e);
else
{
delete sol;
candidate ->setSeq(sequenc e);
} badsteps=0;
if(print_trace)
{
cout << clock.elapsed-timw(Timer::VIR TUAL)<< "\\T" << SOL->EVAL();
cout << endl;
}
if(verbose)
{
sol->print(cerr);
cout << clock.elapsed_t ime(Timer::VIRT UAL) << "\t" << sol ->eval(0;
cout << endl;
}
}
}
while(!clock.al arm(limit) && sol->eval() > optimum);
if(!print-trace && !verbose)
{
if(print_eval){
cout << sol->eval() <<endl;
}
else
{
sol->print();
}
}
delete candidate;
delete sol;
return 0;
}[/code]Comment
-
Could you please explain what is the issue/problem you are facing in your code and what is your code actually doing??Comment
-
Let say, I have this, the output is 2 which from B, then how can the computer detect that the number from A or B?How to write in programming language?
[code=c]
printf("\n");
cout << "The minimum value of the arrays A & B is "<< minimum << "." << endl;
printf("\n");
cout << "Initial solution " << minimum << "."<<endl;[/code]Comment
-
Comment
-
C++ cannot tell you where a value comes from:
[code=cpp]
cout << minimum;
[/code]
Here the variable named minimum is displayed. All you see id the value inside the variable. You cannot tell from this code where the value came from. You will need more code to do that.
To vary 1<=i<=10 is really 0 < i < 11 which is:
[code=c]
for (int i = 0; i < 11; ++i)
{
//your loop logic here
}
[/code]Comment
-
Originally posted by nurulshidanoniIf I have the uppe bound and lower bound; 1<=i<=10, how to write in language programming c++?Comment
-
i use c++, I,m the beginner still study about c++. If i have a mistake, let me know, because I dont know the rules of c++.
My qustion is ihave an upper bound and lower bound ; 0<=i<=10, how to write in c++?Comment
-
Originally posted by nurulshidanoniMy qustion is ihave an upper bound and lower bound ; 0<=i<=10, how to write in c++?
(0 <= i ) && (i <= 10)Comment
-
How to correct the error of the bold?
error C2061: syntax error : identifier 'cout'
Error executing cl.exe.
147.exe - 1 error(s), 0 warning(s)
[code=c]
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <math.h>
#include <stdio.h>
#include <time.h>
using namespace std;
int i, j;
int x[7];
int A[7], B[7], C[7], D[7], E[7], F[7];
// The members of the array int minimum = numbers[0] ;
int minimum, minA, minB, flagB;
int main(void)
{
for(i=1;i<7;i++ )
{
x[i]=i;
A[i]=(2+pow(x[i],2));
B[i]=(1+pow(x[i],3));
C[i]=(2+pow(x[i],2)-1);
D[i]=(3*pow(x[i],3));
E[i]=(1+pow(x[i],2));
F[i]=(3+pow(x[i],2));
}
{
cout <<"\n";
for(i=1;i<7; i++)
{ printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]);
}
cout <<"\n";
for(i=1;i<7; i++)
{
printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]);
}
cout <<"\n";
for(i=1;i<7; i++)
{
printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);
}
cout <<"\n";
for(i=1;i<7; i++)
{
printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);
}
cout <<"\n";
for(i=1;i<7; i++)
{
printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
}
cout <<"\n";
for(i=1;i<7; i++)
{
printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
}
}
printf("\n");
printf ("A\tB\tC\tD\tE \tF");
printf ("\n_\t_\t_\t_\ t_\t_\t");
for(i=1;i<7;i++ )
{
cout<<endl;
printf("%d\t", A[i]);
printf("%d\t", B[i]);
printf("%d\t", C[i]);
printf("%d\t", D[i]);
printf("%d\t", E[i]);
printf("%d\t", F[i]);
}
cout <<"\n";
// Compare the members
minimum = A[1];
for (i = 1; i < 7; ++i)
{
if (A[i]< minimum)
{
minimum = A[i];
minA = i;
} cout <<"\n";
// Announce the result
cout << "The minimum value of the array A is "<< A[minA] << "." << endl;
// minimum = B[1];
for (i = 1; i < 7; ++i)
{
if (B[i]< minimum)
{
minimum = B[i];
minB = i;
}
cout <<"\n";
cout << "The minimum value of the arrays B is "<< B[minB]<< "." << endl;
// to determine the minimum whether in A or B
minimum = A[minA];
if(B[minB] < minimum)
{
minimum = B[minB];
flagB = 1; //the minimum is in B if flagB=1
}
cout <<"\n";
if(flagB = 1)
cout<< "Initial solution:" <<B[minB] << "."<<endl;
else if
cout<< "Initial solution:" << A[minB] << "."<<endl;
return 0;
}
}
}[/code]Comment
-
-
Couple things, first off, anything with a .h is a deprecated header file. I'm not sure about conio, but all those others can be replaced as follows:
<filename.h> becomes <cfilename>
Though <stdio.h> is a C library through and through and doesn't have a C++ analog other than <iostream>. It works, it's just C rather than C++.
There's no longer a bolded part, since it's in code tags, can you tell us what part is giving you problems?Comment
-
Why the output is 0 , even thought there is no 0 in the array?
[code=c]
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <ctime>
using namespace std;
int i;
int x[7];
int A[7], B[7], C[7], D[7], E[7], F[7];
// The members of the array int minimum = numbers[0] ;
int minimum, minA, minB, flagB;
int main(void)
{
for(i=1;i<7;i++ )
{
x[i]=i;
A[i]=(2+pow(x[i],2));
B[i]=(1+pow(x[i],3));
C[i]=(2+pow(x[i],2)-1);
D[i]=(3*pow(x[i],3));
E[i]=(1+pow(x[i],2));
F[i]=(3+pow(x[i],2));
}
{
cout <<"\n";
for(i=1;i<7; i++)
{ printf("[%d][%d] A[%d]=%d\n", i,1, i, A[i]);
}
cout <<"\n";
for(i=1;i<7; i++)
{
printf("[%d][%d] B[%d]=%d\n", i,2, i, B[i]);
}
cout <<"\n";
for(i=1;i<7; i++)
{
printf("[%d][%d] C[%d]=%d\n", i,3, i, C[i]);
}
cout <<"\n";
for(i=1;i<7; i++)
{
printf("[%d][%d] D[%d]=%d\n", i,4, i, D[i]);
}
cout <<"\n";
for(i=1;i<7; i++)
{
printf("[%d][%d] E[%d]=%d\n", i,5, i, E[i]);
}
cout <<"\n";
for(i=1;i<7; i++)
{
printf("[%d][%d] F[%d]=%d\n", i,6, i, F[i]);
}
}
printf("\n");
printf ("A\tB\tC\tD\tE \tF");
printf ("\n_\t_\t_\t_\ t_\t_\t");
for(i=1;i<7;i++ )
{
cout<<endl;
printf("%d\t", A[i]);
printf("%d\t", B[i]);
printf("%d\t", C[i]);
printf("%d\t", D[i]);
printf("%d\t", E[i]);
printf("%d\t", F[i]);
}
cout <<"\n";
// Compare the members
minimum = A[i];
for (i = 1; i < 7; ++i)
{
if (A[i]< minimum)
{
minimum = A[i];
minA = i;
} cout <<"\n";
// Announce the result
cout << "The minimum value of the array A is "<< A[minA] << "." << endl;
minimum = B[i];
for (i = 1; i < 7; ++i)
{
if (B[i]< minimum)
{
minimum = B[i];
minB = i;
}
cout <<"\n";
cout << "The minimum value of the arrays B is "<< B[minB]<< "." << endl;
// to determine the minimum whether in A or B
minimum = A[minA];
if(B[minB] < minimum)
{
minimum = B[minB];
flagB = 1; //the minimum is in B if flagB=1
}
cout <<"\n";
if(flagB = 1)
cout<< "Initial solution:" <<B[minB] << "."<<endl;
else
cout<< "Initial solution:" <<A[minA] << "."<<endl;
return 0;
}
}
}[/code=c]Comment
Comment