📄 order3.c
字号:
for (i=K-1;i>1;i--)
{
for (j=i-1;j>0;j--)
{
for (l=j-1;l>=0;l--)
{
if ((-cost_I[i]-cost_I[j]-cost_I[l]+resource_dh2) < resource_available)
{
k = 0;
resource_right = resource_available- resource_p[k];
cost_left = -cost_I[i]-cost_I[j]-cost_I[l];
loss = 0;
gain = 0;
e_c3 = e_counter;
cost = cost_I[i]+cost_I[j]+cost_I[l]; /* negative value */
resource_candidate = - cost;
for (c=0,k=K; k<N; k++)
{
mul_G = (Gg[i][k] + Gg[j][k] + Gg[l][k]) % 2;
if (mul_G == 1)
{
cost = cost + (C[k][Dd[k]] - C[k][DD[k]]);
if (C[k][Dd[k]] - C[k][DD[k]] > 0.0)
{
non_resource_p[c] = C[k][Dd[k]] - C[k][DD[k]];
c++; /* to be used later */
e_c3--;
loss++;
}
else
{
e_c3++;
gain++;
resource_candidate = resource_candidate - (C[k][Dd[k]] - C[k][DD[k]]);
/* new contribution */
}
}
else
{
if (C[k][Dd[k]] - C[k][DD[k]] > 0.0)
{
resource_candidate = resource_candidate + (C[k][Dd[k]] - C[k][DD[k]]);
/* unchanged contribution */
}
else
{
non_resource_p[c] = C[k][DD[k]] - C[k][Dd[k]];
c++; /* to be used later */
}
}
}
if (cost > Max)
{
Max = cost; /* do not reset Max */
change.n_change = 3;
change.position[0] = i;
change.position[1] = j;
change.position[2] = l;
resource_available = resource_init - Max;
/* Recompute resource_dhi for new resource_candidate */
resource_dh0 = 0.0;
resource_dh1 = 0.0;
resource_dh2 = 0.0;
resource_dh3 = 0.0;
for (a=0,b=c-1;a<D_H-e_c3-3;a++,b--) /* -3 as undo Order 3 */
{
if (a < D_H-e_c3-3-1)
{
resource_dh0 = resource_dh0 + non_resource_p[b];
}
if (a < D_H-e_c3-3-2)
{
resource_dh1 = resource_dh1 + non_resource_p[b];
}
if (a < D_H-e_c3-3-3)
{
resource_dh2 = resource_dh2 + non_resource_p[b];
}
if (a < D_H-e_c3-3-4)
{
resource_dh3 = resource_dh3 + non_resource_p[b];
}
sum_tail_init = sum_tail_init + non_resource_p[b];
}
}
}
else
{
l = -1;
}
} /* end l */
} /* end j */
} /* end i */
step = 3;
/* Re permute to obtain the original sequence (use only permute_R as
permute_I worked on rows). Use DD[] as DD[] no longer useful */
for (i=0;i<change.n_change;i++)
{
Dd[change.position[i]] = DD[change.position[i]];
}
for (i=K; i<N; i++)
{
Dd[i] = 0;
for (j=0; j<K; j++)
{
Dd[i] = Dd[i] + Dd[j] * Gg[j][i];
}
Dd[i] = Dd[i] % 2;
}
for (i=0; i<N; i++)
{
DD[permutation_final[i]] = Dd[i];
}
for (i=0; i < N; i++) /* Using a systematic code */
{
if (DD[i] == 0)
out_D[i] = - 1.0;
else
out_D[i] = 1.0;
}
}
void switch_column_I_0(GG,Gg,R,permutation_R,permutation_final)
/* copy GG into Gg */
int GG[K][N],Gg[K][N];
double R[];
int permutation_R[],permutation_final[];
{
int i,j,index,count,start;
int record[N],record_I[N];
double temp[N];
index = 0;
count = 0;
for (i=0; i < K+index; i++) /* record the column positions to switch */
{
if (GG[i-index][i] == 0)
{
record[index] = i;
index++;
}
else
{
record_I[count] = i;
count ++;
}
}
start = K + index - 1;
i = N-1;
while (i > start) /* Unchanged part */
{
for(j=0; j<K; j++)
{
Gg[j][i] = GG[j][i];
permutation_final[i] = permutation_R[N-1-i];
}
i--;
}
while (index > 0) /* Copy from R to L dependent columns found
in first positions */
{
temp[i] = R[i];
for(j=0; j<K; j++)
{
Gg[j][i] = GG[j][record[index-1]];
permutation_final[i] = permutation_R[N-1-record[index-1]];
}
if (i < record[index-1])
{
R[i] = temp[record[index-1]];
}
else
{
R[i] = R[record[index-1]];
}
i--;
index--;
}
while (count > 0) /* Copy I_columns */
{
temp[i] = R[i];
for(j=0; j<K; j++)
{
Gg[j][i] = GG[j][record_I[count-1]];
permutation_final[i] = permutation_R[N-1-record_I[count-1]];
}
if (i < record_I[count-1])
{
R[i] = temp[record_I[count-1]];
}
else
{
R[i] = R[record_I[count-1]];
}
i--;
count--;
}
}
void switch_vector_0(R,permutation,length)
double R[];
int permutation[];
int length;
{
int i;
double temp[N];
for (i=0;i<=length;i++)
{
temp[i] = R[i];
if (permutation[length-i] > i) /* data not overwritten */
{
R[i] = R[permutation[length-i]];
}
else
{
R[i] = temp[permutation[length-i]];
/* data overwritten but stored in temp */
}
}
}
void switch_matrix_0(Gg,permutation,k,n)
int Gg[K][N];
int permutation[];
int k,n;
{
int i,j;
double temp[K][N];
for (j=0;j<=n;j++)
{
for (i=0; i<=k; i++)
{
temp[i][j] = Gg[i][j];
if (permutation[n-j] > j) /* column not overwritten */
{
Gg[i][j] = Gg[i][permutation[n-j]];
}
else
{
Gg[i][j] = temp[i][permutation[n-j]];
/* data overwritten but stored in temp */
}
}
}
}
void peterson_I_0(Gg,zero)
int Gg[K][N];
double zero[N];
{
int i,j,l,m;
for (i=0; i<K; i++)
{
j = 0;
zero[i] = 0.0;
while (Gg[i][j] == 0)
{
j++;
zero[i] = zero[i] + 1.0;
}
for (l=0; l<K ; l++)
{
if ((l != i) && (Gg[l][j] == 1))
{
for (m=0; m<N; m++)
{
Gg[l][m] = (Gg[l][m] + Gg [i][m])%2;
}
}
}
}
}
/* Recursive quick_sort */
void quick_sort_track_0(vals,permutation,left,right) /* Tracks the permutations */
double vals[];
int permutation[];
int left,right;
{
/* ascending = left wall, descending = right wall */
int ascending = left-1, descending = right;
double ref_val,temp;
int temp2;
/* select the comparison value */
if (right > left)
{
ref_val = vals [right];
for(;;)
{
/* while element smaller than reference value,
move left wall upwards
*/
while (vals [++ascending] < ref_val);
/* while element larger than reference value,
move right wall downwards
*/
while (vals [--descending] > ref_val);
if (ascending >= descending) break;
/* if the walls have not passed each other,
exchange values
(if not access to function need to save index)
*/
temp=vals[ascending];
temp2=permutation[ascending];
vals[ascending]=vals[descending];
vals[descending]=temp;
permutation[ascending]=permutation[descending];
permutation[descending]=temp2;
}
temp=vals[ascending];
temp2=permutation[ascending];
vals[ascending]=vals[right];
vals[right]=temp;
permutation[ascending]=permutation[right];
permutation[right]=temp2;
/* if descending wall has not yet reached left partition,
call quick-sort again with this smaller array
*/
quick_sort_track_0( vals, permutation, left, ascending-1);
/* if ascending wall has not yet reached right partition,
call quick-sort again with this smaller array
*/
quick_sort_track_0( vals, permutation, ascending+1, right);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -