2010-06-01から1ヶ月間の記事一覧

数独9*9

頑張って計算はしているようだが、 数分では結果がかえってこない・・・。 /* sudoku solver */ sig Cell { x:Int, y:Int, v:Int } { x >= 0 and x <= 8 y >= 0 and y <= 8 v >= 1 and v <= 9 } fact AllCellsPosition { no disj c1,c2:Cell | c1.x == c2.x …

タスクサマリ

日常業務で何をやっていたのかは逐一メモしているわけですが、 それを集計するというのはよくやるわけです。 というわけでそんなプログラム。 # 09:00 12:00 Hoge メール対応 # 12:00 13:00 社内 休憩 # 13:00 16:00 Fuga 訪問 # # という形式のファイルを元…

数独4*4補助ルール付き

グループ内(2*2,3*3などの正方マス)には同一数字は入らない、 というルールを付け加えて、前回のものと速度を比較した。 sig Cell { x:Int, y:Int, v:Int } { x >= 0 and x <= 3 y >= 0 and y <= 3 v >= 1 and v <= 4 } fact AllCellsPosition { no disj c1,…

depth-first-search

深さ優先探索で、橋渡し問題を解く。 ; 各stateは'n か's(対岸)のどちらか) (defstruct state :farmer :wolf :goat :cabbage) (defn opposite [x] (if (= x 'n) 's 'n)) (defn expand [st] (let [f (:farmer st) w (:wolf st) g (:goat st) c (:cabbage st)]…

数式微分

何度見ても美しいと思うSchemeの課題をClojureで。 (defn third [x] (first (next (next x)))) (defn constant? [exp] (cond (contains? #{'+ '- '* '/ '**} exp) false (coll? exp) false :else true)) (defn diff [e x] (cond (constant? e) (if (= e x) 1…

数独4*4

こういう数独を解きます。 http://d.hatena.ne.jp/ku-ma-me/20080108 0, 0, 0, 4 0, 0, 1, 2 0, 1, 4, 3 4, 3, 2, 1sig Cell { x:Int, y:Int, v:Int } { x >= 0 and x <= 3 y >= 0 and y <= 3 v >= 1 and v <= 4 } fact AllCellsPosition { no disj c1,c2:Ce…

Thread Life Cycle

状態遷移のチェックだってできるぜ。 といいたいところだが、まぁ簡単に図を書くくらいならLTSAと同じだなぁ、 というところをやってみる。 /* LTSAのマネ.Thread Life Cycle THREAD = CREATED, CREATED = (start -> RUNNABLE |stop -> TERMINATED), RUNNING…

Role Based Access Control

第6回Formal Methods 勉強会で出た、RBACのモデルのお話。 こんな感じかな。 /* Role Based Access Control ロールに階層関係を追加 */ /* ユーザアカウント */ sig User { roles: set Role } /* ロール。Admin,Guestなど */ sig Role { perms: set Permissi…

ライントレーサー

"Interface" Jan.2010 「外部センサのデータにより、モータの回転を自律制御するアルゴリズムを考えよう!(間瀬順一、吉村悠)」より。 さっと作ると、かなり落とし穴のある制御のモデルになってしまった。 上手いこと制御のアルゴリズムを考えて私に教えて…

Quantifications

sig Name { address: set Addr } sig Addr {} pred All [] { all n:Name | one n.address } pred Some [] { some n:Name | one n.address } pred No [] { no n:Name | one n.address } pred Lone [] { lone n:Name | one n.address } pred One [] { one n:Na…

RlationalOperators

sig Book { name:set Name } sig Name { addr:set Addr, mainaddress: one Addr } sig Addr {} fun Arrow []: set Name -> Addr { Name -> Addr } fun Dot [] : set Name { Book.(Book->Name) } fun Box [] : set Addr { (Name->Addr)[Name] } fun Transpose…

SetOperators.als

/* Name = {(G0),(A0),(A1)} Alias = {(A0),(A1)} Group = {(G0)} RecentlyUsed = {(G0),(A1)} */ abstract sig Name {} sig Alias extends Name {} sig Group extends Name {} sig RecentlyUsed in Name {} fun Union []:set Name { Alias + Group } fun In…

コース申込システム

某社のコース申込システムをモデリング。 /* 豆蔵トレーニングコース申込システム 豆蔵のトレーニングコースの概要は以下の通り。 このシステムにおいて、コースへの申込が会場の定員数を超えないように管理したい。 [コース] コースにはセットコース、単体…

CommonLispのatomが欲しい

CommonLispのatomに対抗してatom?を作る。 user> (defn atom? [x] (cond (not (seq? x)) true (empty? x) true :else false)) #'user/atom? user> (atom? 'sss) true user> (atom? (cons '1 '())) false user> (atom? nil) true user> (atom? '()) true user…

再帰

うーむ、CommonLispではさくっと動くものがClojureだと動かない・・・ user> (defn count-anywhere [item tree] "count the times item appears anywhere within tree." (cond (= item tree) 1 (not (seq? tree)) 0 :else (+ (count-anywhere item (first tr…

insertion-sort

関数型でやるとこんな感じ? (defn insert [sorted-coll val] (concat (filter #(> val %) sorted-coll) [val] (filter #(<= val %) sorted-coll))) (defn insertion-sort [coll] (loop [vals (rest coll) sorted-coll (list (first coll))] (if (empty? val…