改め Objective Technician

はぐれ技術者のやりたい放題

おしょすぃー

2006-12-28 21:47:51 | プログラミング
バイトで、ガス拡散シミュレータのプログラムを書き換えてガスセンサのモデルシミュレータを作ることになった。


自分でプログラムを書くのはいいが、他人の書いたコードを読むのほど苦労することはない。

しかも、いろんな人が修正を加えてきた年代モノだから書き方がばらばらで読みにくいったらありゃしない。


あまりにもコードが美しくないので、コメントで/*おしょすぃー*/って入れてやった。


いっそのこと、0から自分で組んだほうが早いかもしれん。



あぁ、仕事持ち帰りたくねぇ。



授業の宿題もいっぱい出てんのに…

ミニハードルを真っ直ぐ並べられない星人

2006-12-26 18:23:09 | 陸上競技
昨日は授業で山の上


今日もバイトで山の上


明日もバイトで山の上


毎日毎日レポートだらけ



バイトでははほとんどの時間プログラミングやってるから別に苦にならん。




練習日誌

12月26日 (Tue) 評定 曇のち雨 寒

jog
ストレッチ

200m Wind Sprint x5本

ドリル
ポール走100m x3本
ミニハードル、ポール付き
14歩助走練習(突っ込みまで)

踏み切りドリル
逆上がり x5


c-down



ポールいっぱい戯れることで、だいぶそっれぽい格好になってきたと思うけど、実際にボックスを前にするとビビリそう。



まだ雪積もらないんだったらマット出したままにしとけばよかったなぁ。


練習日誌

2006-12-19 23:56:24 | 陸上競技
12月19日(Tue)  評定  晴  寒


jog
ストレッチ
ドリル

main
300-300-200-200
43"-42"-27"-28"


秘密の振り上げ特訓

c-down



2セットだと思って抑えちゃった。

もっと出せば良かった。



足が短くなったような気がする理由が分かった。

腰が引けてる。


走るときもハードルのときも踏切りのときも。





まぁ、いいや。





冬だし。






カックラキン大放送、ちゃんと録画できてたし。

トンペイの成れの果て

2006-12-18 20:42:02 | ムダ話
今日の実験発表会。


ある発表者がマイクをこんな持ち方して喋ってた。


発表が始まったとたんにみんな笑いを堪えるのに必死で、それでも彼が薬指と中指にマイクを挟んでしかも小指まで立ててマイクのお尻を上に向けるもんだから発表の中身全然頭に入らんかった。



普段無口で、喋ってるとこ見たことない人だっただけに余計ツボにはまった。



そう彼はこれだけはやってはいけなかったにのだ。




ネタ元

練習日誌

2006-12-17 23:20:26 | 陸上競技
昨日の。


12月16日(Sat) 宮城野原 曇 暖

200mWS×5本

ストレッチ
ドリル
スタブロ
技術走×2本

main
200-400-200  27"-57"-26"
150-300-150  18"-42"-19"


c-down



なんか足が短くなった気がする。実際に短くなってるんじゃなくて、感覚的に。

木曜日にハードルやったときに特にそう思った。


ビデオ見ても、早い引き付けを意識してる割に足の振り出しが小さい。

最近、流しみたいな走り方ばっかりしてて100とか150とかやってないから、ショートスプリントの動きが出来なかった。





まぁ、いいや。





冬だし。






明日は4時からカックラキン大放送(再)だし。

2x2x2ルービックキューブプログラム実行例

2006-12-16 20:31:58 | プログラミング
昨日のプログラムに、こんな初期状態を入力したときの実行例。


初期状態↓。





入力データ

b g w w
r o r y
g y o r
y y g b
r o b o
w g w b

(入力データの作り方は昨日のレポート参照)


実行結果





2x2x2だとセンターキューブがないから、表示をちょっと変えた。

例えば

r r
w b の面 ってのは赤、赤、白、青の面のこと。



表示された通りにキューブを回していった図。↓




























完成。



2x2x2なら、どんなにぐちゃぐちゃな状態からでも昨日のプログラムで最大10手で最適解が求まる。もっと手数がかかる状態があるかもしれないけど今のところ見つかってない。





今回のでルービックキューブを研究してたら、いつの間にか手順を覚えて3x3x3のが2分30秒ぐらいで完成できるようになってた。(もちろんコンピュータ使わないで。)




こんなのもできるようになった。


cube in cube.







2分半ぐらいじゃ世のキュービストには到底かなわないけど、3x3x3のを6面揃えられると株式会社メガハウス(日本国内で正式にライセンスを受けてルービックキューブを販売している会社)から「スーパーキュービスト認定書」がもらえるらしい。色を崩したルービックキューブを持った上半身の写真と、6面完成させたキューブを持った写真を送ると認定書が送られてくるらしい。





これ持ってたら、就職のとき書類で落とされることはまずないってさ。

2x2x2ルービックプログラム 続き

2006-12-15 21:10:01 | プログラミング
/*前の記事の続き*/





void heuristic(struct node *cube)
{
  char i,j,k,p=1,q;
  unsigned int color[6], dcube=0, buff=0, hash;
  struct node *node;

  if(cube->surface[0][0][0]!=goal.surface[0][0][0] || 
/*コーナーキューブの位置,向きが違ってる個数を数える*/
cube->surface[2][1][0]!=goal.surface[2][1][0] || 
cube->surface[1][1][1]!=goal.surface[1][1][1])
    dcube++;

  if(cube->surface[0][0][1]!=goal.surface[0][0][1] || 
cube->surface[2][1][1]!=goal.surface[2][1][1] || 
cube->surface[4][1][0]!=goal.surface[4][1][0])
    dcube++;

  if(cube->surface[0][1][0]!=goal.surface[0][1][0] || 
cube->surface[1][1][0]!=goal.surface[1][1][0] || 
cube->surface[3][0][0]!=goal.surface[3][0][0])
    dcube++;

  if(cube->surface[0][1][1]!=goal.surface[0][1][1] || 
cube->surface[4][1][1]!=goal.surface[4][1][1] || 
cube->surface[3][0][1]!=goal.surface[3][0][1])
    dcube++;

  if(cube->surface[5][0][0]!=goal.surface[5][0][0] || 
cube->surface[3][1][0]!=goal.surface[3][1][0] || 
cube->surface[1][0][0]!=goal.surface[1][0][0])
    dcube++;

  if(cube->surface[5][0][1]!=goal.surface[5][0][1] || 
cube->surface[3][1][1]!=goal.surface[3][1][1] || 
cube->surface[4][0][1]!=goal.surface[4][0][1])
    dcube++;

  if(cube->surface[5][1][0]!=goal.surface[5][1][0] || 
cube->surface[2][0][0]!=goal.surface[2][0][0] || 
cube->surface[1][0][1]!=goal.surface[1][0][1])
    dcube++;

  if(cube->surface[5][1][1]!=goal.surface[5][1][1] || 
cube->surface[2][0][1]!=goal.surface[2][0][1] || 
cube->surface[4][0][0]!=goal.surface[4][0][0])
    dcube++;


  for(i=0;i<6;i++)surface[k][i][j])
          color[k]++;     /*色が違ってる数を数える*/

  for(i=0;i<6;i++)heuristic=0;

  else if(hash==12)
    cube->heuristic=1;

  else if(hash==884 || hash==402)
    cube->heuristic=2;

  else if(hash==1317 || hash==78 || hash==3426 || hash==1156 || hash==1750
 || hash==788 || hash==160 || hash==5128 || hash==2291 || hash==6830 || hash==588)
    cube->heuristic=3;

  else if(hash==1966 || hash==378 || hash==3048 || hash==4561 || hash==2615
 || hash==559 || hash==257 || hash==28 || hash==2039 || hash==452 || hash==307
 || hash==9099 || hash==1558 || hash==106 || hash==2939 || hash==18 || hash==62 
|| hash==2040 || hash==3049 || hash==4057 || hash==1535 || hash==1030 ||
 hash==778 || hash==2292 || hash==1534 || hash==234 || hash==506 || hash==210)
    cube->heuristic=4;

  else if(hash==739 || hash==1173 || hash==451 || hash==6074 || hash==3480 ||
 hash==2327 || hash==197 || hash==124 || hash==337 || hash==343 || hash==136 
|| hash==668 || hash==1318 || hash==3912 || hash==1366 || hash==1031 ||
 hash==1100 || hash==12124 || hash==8091 || hash==272 || hash==694 ||
 hash==1029 || hash==143 || hash==595 || hash==208 || hash==498 ||
 hash==4398 || hash==404 || hash==111 || hash==525 || hash==14 || hash==469)
    cube->heuristic=5;

  else if(hash==993 || hash==95 || hash==306 || hash==1724 || hash==23 ||
 hash==403 || hash==885 || hash==596 || hash==2712 || hash==1174 || 
hash==789 || hash==1367 || hash==3609 || hash==176 || hash==597 || hash==338
 || hash==177 || hash==258 || hash==37 || hash==669 || hash==288 || hash==180
 || hash==2713 || hash==1751 || hash==16 || hash==5402 || hash==4058 || hash==45
 || hash==693 || hash==740 || hash==1815 || hash==499 || hash==1967 || hash==918
 || hash==379 || hash==1480 || hash==21 || hash==777 || hash==24 || hash==886 || hash==22)
    cube->heuristic=6;

  else if(hash==11 || hash==152 || hash==15 || hash==20 || hash==36 || hash==162
 || hash==198 || hash==137 || hash==1368 || hash==17 || hash==50 || hash==13
 || hash==75 || hash==1816 || hash==87 || hash==25 || hash==96 || hash==917)
    cube->heuristic=7;

  else if(hash==10 || hash==19 || hash==919)
    cube->heuristic=8;

  else
    cube->heuristic=9;


  cube->expectation=cube->heuristic+cube->cost;

  cube->hash=1.0;
  for(i=0;i<6;i++)hash*=sin(color[i]+i*0.5);   
  /*前と同じノードを展開しないためのハッシュ値を計算*/

  cube->hash*=dcube;

  node=root.nodelist;    /*ノードリストをたどる*/

  while(node!=cube && p){
    if(node->hash==cube->hash){    /*同じハッシュ値をもつノードがあったら*/
      q=0;

      for(k=0;k<5;k++)surface[k][i][j]!=node->surface[k][i][j])    
/*状態を詳しく比較して*/
      q++;

      if(!q){
        p=0;
        cube->revleaf->nextleaf=cube->nextleaf; 
   /*全く同じなら,このノードをリーフリストからはずす*/
        cube->nextleaf->revleaf=cube->revleaf;
      }
    }
  
    node=node->nodelist;
  }
}







void exception(void)  
  /*ルート展開のあと1回だけ呼び出す。状態遷移を網羅するためあと3つ展開する*/
{
  struct node *leaf;
  char i,j,k,l;


  leaf=(struct node *)malloc(sizeof(struct node)*3);
  if(!leaf){
    printf("メモリ割り当てエラー\a\n");
    exit(1);
  }

  endleaf.revleaf->nextleaf=&leaf[0];
  leaf[0].revleaf=endleaf.revleaf;
  leaf[0].nextleaf=&leaf[1];
  leaf[1].revleaf=&leaf[0];
  leaf[1].nextleaf=&leaf[2];
  leaf[2].revleaf=&leaf[1];
  leaf[2].nextleaf=&endleaf;
  endleaf.revleaf=&leaf[2];

  for(i=0;i<3;i++)nodelist=&leaf[0];
  leaf[0].nodelist=&leaf[1];
  leaf[1].nodelist=&leaf[2];
  leaf[2].nodelist=&nodeend;
  nodeend.revnode=&leaf[2];

  for(i=0;i<3;i++){nextleaf){


    if(min>leaf->expectation){
      min=leaf->expectation; 
  /*リーフリストをたどってheuristic+costが最小のものを探す*/
      minG=leaf;
    }

    leaf=leaf->nextleaf;
  }

}





void output(void)     /*解を表示*/
{
  unsigned int i;
  struct node *p;

  p=minG;

  for(i=0;i<min;i++){   reverse->revnode=p;
    p=p->reverse;
  }

  printf("\n\n %d 手で完成\n\n\a",min);

  p=p->revnode;

  for(i=min;i;i--){
    switch(p->ope){     /*解法を表示する*/

      case 0:
        printf(" %c %c\n %c %c    の面を: 180°回転\n\n"
,p->reverse->surface[p->opecolor][0][0],p->reverse->surface[p->opecolor][0][1]
,p->reverse->surface[p->opecolor][1][0],p->reverse->surface[p->opecolor][1][1]);
        break;

      case 1:
        printf(" %c %c\n %c %c    の面を: 90°右に回転\n\n"
,p->reverse->surface[p->opecolor][0][0],p->reverse->surface[p->opecolor][0][1]
,p->reverse->surface[p->opecolor][1][0],p->reverse->surface[p->opecolor][1][1]);
        break;

      case -1:
        printf(" %c %c\n %c %c    の面を: 左に 90°回転\n\n"
,p->reverse->surface[p->opecolor][0][0],p->reverse->surface[p->opecolor][0][1]
,p->reverse->surface[p->opecolor][1][0],p->reverse->surface[p->opecolor][1][1]);
    };
 
    p=p->revnode;
  }

}


/*おわり*/

2x2x2ルービックキューブプログラム

2006-12-15 21:08:53 | プログラミング
/*2x2x2のルービックキューブを解くCプログラム。この場合、探索空間が3x3x3に比べてかなり小さくて、どんな状態からでも解ける。アルゴリズムや入力データなどは昨日のと同じ考え方。実行結果はまたこんど。*/

ソースはこちら


#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define D 2      /*2x2x2のルービックキューブ。ここを4にしても4x4x4が解けるわけじゃない*/

 
struct node{
  char surface[6][D][D];    
  /*ルービックキューブ各面の情報*/

  unsigned int cost;        
   /*今の状態に何手で来たかを各ノードに記憶*/
  unsigned int heuristic;    
  /*このノードのヒューリスティック値*/
  unsigned int expectation;  
  /*heuristic+cost*/
  long double hash;          
 /*前に展開したノードと同じノードを展開しないためにハッシュ値を使う*/

  struct node *next[6];     
 /*ここから展開される子ノードへのポインタ*/
  struct node *reverse;    
   /*親ノードへのポインタ。最後に解へのパスをたどるめに使う*/
  struct node *nextleaf;    
  /*現在のリーフをリスト構造でつなぐ。次のリーフへのポインタ*/
  struct node *revleaf;     
  /*前のリーフへのポインタ*/
  struct node *nodelist;  
    /*ハッシュ値を比べるために,生成したノードをすべてリニアにつなぐ。次のノードへのポインタ*/
  struct node *revnode;   
    /*一つ前のノードへのポインタ*/

  char ope;                  /*親ノードから,どんな状態変化をされて来たのかを記憶*/
  char opecolor;             /*親ノードからこの状態に来るときに,どの面を回したのか記憶*/

}root, goal, *minG, leaflist, endleaf, nodeend;

unsigned int min;      /*heuristic+costの最小値*/


void start(void);          /*rootの初期化など*/
void develop(struct node *cube);      /*ノードを展開する*/
void allocate(struct node *cube);     /*ノードを展開するための,メモリを確保*/
void heuristic(struct node *cube);    /*ヒューリスティック関数*/
void operate(struct node *cube);      /*生成したノードの状態遷移を行う*/
void Rrot(struct node *next, struct node *original, char s);    /*一面を右に90度回転*/
void Lrot(struct node *next, struct node *original, char s);    /*一面を左に90度回転*/
void rot(struct node *next, struct node *original, char s);     /*一面を180度回転*/
void search(void);         /*heuristic+costが最小のリーフを探す*/
void output(void);         /*解へのパスをたどって解法を表示*/
void exception(void);      /*rootを展開するときだけちょっと特別*/


int main(void)
{
  unsigned int depth=0;


  start();    /*いろんなものを初期化*/

  develop(&root);  /*rootを展開*/

  exception();   /*rootを展開その2*/

  search();     /*展開すべきリーフを探す*/

  while(minG->heuristic){    /*ゴール状態でない限りループ*/

    if(minG->cost>depth)
      printf("%dn",++depth);  /*現在展開した一番深いノードの深さを表示*/

    develop(minG); /*リーフを展開*/
    search();      /*次に展開するリーフを探す*/

  }

  output();    /*解法を表示*/

  return 0;
}



void start(void)
{
  char i,j,k;

  for(i=0;i<6;i++)next[i]);   /*展開した各ノードのヒューリスティック値を計算*/

}



void allocate(struct node *cube)
{
  char i,j,k,l;
  struct node *nextcube;

  nextcube=(struct node *)malloc(sizeof(struct node)*6);  /*メモリ確保*/
  if(!nextcube){
    printf("メモリ割り当てエラーan");
    exit(1);
  }

  nodeend.revnode->nodelist=&nextcube[0];    /*ノードリストにつなげる*/

  cube->revleaf->nextleaf=&nextcube[0];     /*リーフリストにつなげる*/
  nextcube[0].revleaf=cube->revleaf;



  for(i=0;i<5;i++){    /*生成したノードをリストにつなげて,親と同じ状態をコピー*/next[i]=&nextcube[i];
    nextcube[i].reverse=cube;
    nextcube[i].nodelist=&nextcube[i+1];

    for(k=0;k<6;k++)surface[k][j][l];

    nextcube[i].cost=1+cube->cost;
    nextcube[i].nextleaf=&nextcube[i+1];
    nextcube[i+1].revleaf=&nextcube[i];

  }

  cube->next[5]=&nextcube[5];
  nextcube[5].reverse=cube;
  nextcube[5].nodelist=&nodeend;
  nodeend.revnode=&nextcube[5];

  for(k=0;k<6;k++)surface[k][j][l];

  nextcube[5].cost=cube->cost+1;
  nextcube[5].nextleaf=cube->nextleaf;
  cube->nextleaf->revleaf=&nextcube[5];

}




void operate(struct node *cube)      /*各ノードで状態遷移を行う*/
{
  struct node *nextcube;
  char i, k=0;


  if(cube->opecolor!=1){

    nextcube=cube->next[k++];

    for(i=0;i<D;i++){surface[0][i][0]=cube->surface[3][i][0];
      nextcube->surface[2][i][0]=cube->surface[0][i][0];
      nextcube->surface[5][i][0]=cube->surface[2][i][0];
      nextcube->surface[3][i][0]=cube->surface[5][i][0];
    }

    Lrot(nextcube, cube, 1);

    nextcube->ope=-1;
    nextcube->opecolor=1;


    nextcube=cube->next[k++];

    for(i=0;i<D;i++){surface[0][i][0]=cube->surface[5][i][0];
      nextcube->surface[2][i][0]=cube->surface[3][i][0];
      nextcube->surface[5][i][0]=cube->surface[0][i][0];
      nextcube->surface[3][i][0]=cube->surface[2][i][0];
    }

    rot(nextcube, cube, 1);

    nextcube->ope=0;
    nextcube->opecolor=1;


    nextcube=cube->next[k++];

    for(i=0;i<D;i++){surface[0][i][0]=cube->surface[2][i][0];
      nextcube->surface[2][i][0]=cube->surface[5][i][0];
      nextcube->surface[5][i][0]=cube->surface[3][i][0];
      nextcube->surface[3][i][0]=cube->surface[0][i][0];
    }

    Rrot(nextcube, cube, 1);

    nextcube->ope=1;
    nextcube->opecolor=1;

  }


  if(cube->opecolor!=2){

    nextcube=cube->next[k++];

    for(i=0;i<D;i++){surface[0][0][D-1-i]=cube->surface[1][D-1-i][1];
      nextcube->surface[4][i][0]=cube->surface[0][0][D-1-i];
      nextcube->surface[5][1][i]=cube->surface[4][i][0];
      nextcube->surface[1][D-1-i][1]=cube->surface[5][1][i];
    }

    Lrot(nextcube, cube, 2);

    nextcube->ope=-1;
    nextcube->opecolor=2;


    nextcube=cube->next[k++];

    for(i=0;i<D;i++){surface[0][0][D-1-i]=cube->surface[5][1][i];
      nextcube->surface[4][i][0]=cube->surface[1][D-1-i][1];
      nextcube->surface[5][1][i]=cube->surface[0][0][D-1-i];
      nextcube->surface[1][D-1-i][1]=cube->surface[4][i][0];
    }

    rot(nextcube, cube, 2);

    nextcube->ope=0;
    nextcube->opecolor=2;


    nextcube=cube->next[k++];

    for(i=0;i<D;i++){surface[0][0][D-1-i]=cube->surface[4][i][0];
      nextcube->surface[4][i][0]=cube->surface[5][1][i];
      nextcube->surface[5][1][i]=cube->surface[1][D-1-i][1];
      nextcube->surface[1][D-1-i][1]=cube->surface[0][0][D-1-i];
    }

    Rrot(nextcube, cube, 2);

    nextcube->ope=1;
    nextcube->opecolor=2;
  }


  if(cube->opecolor!=0){

    nextcube=cube->next[k++];

    for(i=0;i<D;i++){surface[4][1][i]=cube->surface[3][0][D-1-i];
      nextcube->surface[2][1][i]=cube->surface[4][1][i];
      nextcube->surface[1][1][i]=cube->surface[2][1][i];
      nextcube->surface[3][0][D-1-i]=cube->surface[1][1][i];
    }

    Lrot(nextcube, cube, 0);


    nextcube->ope=-1;
    nextcube->opecolor=0;


    nextcube=cube->next[k++];

    for(i=0;i<D;i++){surface[4][1][i]=cube->surface[1][1][i];
      nextcube->surface[2][1][i]=cube->surface[3][0][D-1-i];
      nextcube->surface[1][1][i]=cube->surface[4][1][i];
      nextcube->surface[3][0][D-1-i]=cube->surface[2][1][i];
    }

    rot(nextcube, cube, 0);

    nextcube->ope=0;
    nextcube->opecolor=0;


    nextcube=cube->next[k];

    for(i=0;i<D;i++){surface[4][1][i]=cube->surface[2][1][i];
      nextcube->surface[2][1][i]=cube->surface[1][1][i];
      nextcube->surface[1][1][i]=cube->surface[3][0][D-1-i];
      nextcube->surface[3][0][D-1-i]=cube->surface[4][1][i];
    }

    Rrot(nextcube, cube, 0);

    nextcube->ope=1;
    nextcube->opecolor=0;
  }

}



void Lrot(struct node *next, struct node *original, char s)
{
  char i,j;

  for(i=0;i<D;i++)surface[s][i][j]=original->surface[s][j][D-1-i];   /*左回転*/
}

void Rrot(struct node *next, struct node *original, char s)
{
  char i, j;

  for(i=0;i<D;i++)surface[s][i][j]=original->surface[s][D-1-j][i];    /*右回転*/
}


void rot(struct node *next, struct node *original, char s)
{
  char i,j;

  for(i=0;i<D;i++)surface[s][i][j]=original->surface[s][D-i-1][D-1-j];  
  /*180度回転*/
}



/*続く*/