sunasunaxの日記

制約論理プログラム iZ-C の紹介

魔方陣セルの即値化順による計算時間のサンプル調査

方陣セルの即値化順による計算時間のサンプル調査

-----------------------------------------------------------------------------
tout=3; nn=5; pp="\( \+[0-9]\+\)"; for*1; do printf "%5d" $aa >/dev/pts/40; timeout $tout ./magicsqure_simp $nn 6 8 16 18 12 0 24 4 20 $(echo 1 2 3 11 21 23 13 22 7 17 5 9 10 14 15 19 |tr " " "\n" |shuf) |sed -ne"/ARGS\|Fail\|Elapsed/ ! d; /ARGS/{h;}; /Fail/{H;}; /Elapsed/{H; g; s/\n/ /g; s/Nb..*Fails = //; s/Elapsed.*ime = //; p;}"; done |sort -nrk 27 |sed -e"s/$pp$pp$pp$pp$pp/\1\2\3\4\5 | /g;" |tail |grep -n " 0 \| 1 \| 2 \| 3 \| 4 "

1:ARGS 6 8 16 18 12 | 0 24 4 20 9 | 7 14 2 23 10 | 11 22 5 13 1 | 19 17 15 3 21 | 1133007 2.90488 s
2:ARGS 6 8 16 18 12 | 0 24 4 20 9 | 5 15 2 21 14 | 10 11 7 13 22 | 3 23 1 17 19 | 1122005 2.92625 s
3:ARGS 6 8 16 18 12 | 0 24 4 20 7 | 9 14 19 2 11 | 13 22 17 15 23 | 3 5 21 10 1 | 1096427 2.84937 s
4:ARGS 6 8 16 18 12 | 0 24 4 20 7 | 9 17 15 14 19 | 11 1 22 13 23 | 3 5 21 10 2 | 1063721 2.72319 s
5:ARGS 6 8 16 18 12 | 0 24 4 20 7 | 5 14 2 19 1 | 15 9 13 23 3 | 10 22 21 11 17 | 1049436 2.77985 s
6:ARGS 6 8 16 18 12 | 0 24 4 20 5 | 7 19 17 11 13 | 1 2 10 9 22 | 3 23 14 21 15 | 1006366 2.56388 s
7:ARGS 6 8 16 18 12 | 0 24 4 20 9 | 5 10 14 13 2 | 7 22 11 23 1 | 3 19 15 21 17 | 993892 2.6823 s
8:ARGS 6 8 16 18 12 | 0 24 4 20 5 | 7 14 10 11 15 | 2 1 3 13 17 | 9 22 21 19 23 | 982430 2.67024 s
9:ARGS 6 8 16 18 12 | 0 24 4 20 7 | 5 15 19 3 17 | 2 11 1 21 10 | 14 23 9 22 13 | 980750 2.55122 s
10:ARGS 6 8 16 18 12 | 0 24 4 20 7 | 5 15 14 13 22 | 23 3 2 11 21 | 10 19 9 17 1 | 959628 2.51054 s

-----------------------------------------------------------------------------
/**************************************************************************
* 魔方陣
* ./magicsqure_simp 5 6 8 16 18 12 0 24 4 20 1 2 3 11 21 23 13 22 7 17 5 9 10 14 15 19
**************************************************************************/
#include <stdlib.h>
#include <time.h>
#include "iz.h"

int DIM, LIM_MAIN;
CSint **CELL;
/**************************************************************************/
void printSolution(CSint **allvars, int nbVars) {
int i, val;
static int NbSolutions = 0;

//if(++NbSolutions >= 10000 / 2) exit(1);
if(++NbSolutions >= 10000 / 2) cs_cancelSearch();

// printf("\033[0;0f");
// printf("\nSolution %6d\n", ++NbSolutions);
return;
for (i = 0; i < DIM*DIM; i++) {
val = cs_getValue(CELL[i]);
printf("%3d", val);
if*2;
DIM = atoi(argv[1]); LIM_MAIN = DIM*(DIM*DIM-1)/2;

CELL = (CSint **)malloc(DIM*DIM * sizeof(CSint *));
CSint **N_CELL = (CSint **)malloc(DIM*DIM * sizeof(CSint *));

CSint ***RC_CELL = (CSint ***)malloc(DIM * sizeof(CSint **));
CSint ***CR_CELL = (CSint ***)malloc(DIM * sizeof(CSint **));
for(i = 0; i < DIM; i++){
RC_CELL[i] = (CSint **)malloc(DIM * sizeof(CSint *));
CR_CELL[i] = (CSint **)malloc(DIM * sizeof(CSint *));
}

CSint ***dRL = (CSint ***)malloc(2 * sizeof(CSint **));
for(i = 0; i < 2; i++){
dRL[i] = (CSint **)malloc(DIM * sizeof(CSint *));
}

int n=0;
for(i = 0; i < DIM; i++){
for(j = 0; j < DIM; j++){
CELL[n++] = RC_CELL[i][j] = CR_CELL[j][i] = cs_createCSint(0, DIM*DIM-1);
}
}

for(i = 0; i < DIM; i++){
cs_EQ(cs_Sigma(RC_CELL[i], DIM), LIM_MAIN);
cs_EQ(cs_Sigma(CR_CELL[i], DIM), LIM_MAIN);
}

for(i = 0; i < DIM; i++){
dRL[0][i] = RC_CELL[i][i];
dRL[1][i] = RC_CELL[i][DIM - 1 - i];
}
cs_AllNeq(&CELL[0], DIM*DIM);

for(i = 0; i < 2; i++){
cs_EQ(cs_Sigma(dRL[i], DIM), LIM_MAIN);
}

cs_Lt(RC_CELL[0][0], RC_CELL[0][DIM-1]);
cs_Lt(RC_CELL[0][DIM-1], RC_CELL[DIM-1][0]);
cs_Lt(RC_CELL[0][0], RC_CELL[DIM-1][DIM-1]);

cs_printf("%A\n", CELL, DIM*DIM);
printf("ARGS ");
for(i = 0; i < DIM * DIM; i++){
int j = atoi(argv[2+i]);

printf("%3d", j);
N_CELL[i] = CELL[j];
}
putchar('\n');
cs_printf("%A\n", N_CELL, DIM*DIM);
//exit(1);
printf("\033[2J\033[0;0f"); /* clear screen */
/*
cs_findAll(&N_CELL[0], DIM*DIM, cs_findFreeVar, printSolution);
cs_findAll(&N_CELL[0], DIM*DIM, cs_findFreeVarNbElements, printSolution);
cs_findAll(&N_CELL[0], DIM*DIM, cs_findFreeVarNbElementsMin, printSolution);
cs_findAll(&N_CELL[0], DIM*DIM, cs_findFreeVarNbConstraints, printSolution);
*/
cs_findAll(&N_CELL[0], DIM*DIM, cs_findFreeVar, printSolution);

cs_printStats();
printf("Elapsed Time = %g s\n", (double) (clock() - t0) / CLOCKS_PER_SEC);
time(&nowtime); printf("%s", ctime(&nowtime));
cs_end();
return EXIT_SUCCESS;
}

*1:aa=0; aa<2 * 1200; aa++

*2:i+1) % DIM == 0) putchar('\n');
}
}
/**************************************************************************/
/**************************************************************************/
int main(int argc, char **argv) {
int i, j;
clock_t t0 = clock();
time_t nowtime;

cs_init();
time(&nowtime); printf("%s", ctime(&nowtime