2017-11-27 22 views
0

私はSubset Sum Problemのランダムな解決可能なインスタンスを生成しようとしています。 Wikipediaは、目標値は常にゼロであるべきだと述べていますが、目標値を指定することもできます。これは私がここでやっていることです。Clojureのtest.checkで乱数列をサンプルするにはどうすればよいですか?

したがって、(gen/vector gen/int)を使用してランダムベクトルを作成し、次にランダムなサブベクトルをサンプリングし、そのベクトルを合計してターゲット値を作成することです。 gen/elementsを使用した明らかな戦略の問題は、同じ要素を繰り返しサンプリングする可能性があることです。
私の次善の考えは、インデックスのランダムなセットを作成し、それらのインデックスのすべての要素を抽出することです。より簡単なアプローチはありますか?

+0

それはあなたがランダムアルゴリズムを探しているようtest.check'は、テストのために意味される '一方、サウンド、かなり右の適合ではありません。 'rand'と' rand-int'を使って自分の乱数を生成する方が良いと思います。 –

+0

いいえ、私はそうではありません。私は、二次ディオファントス方程式にSubset Sumインスタンスを減らすアルゴリズムを持っています。そのアルゴリズムをテストするためには、私が書いている特定のテストのための解決可能なインスタンスが必要です。 –

答えて

1

This generator in test.chuck各要素のインクルージョンフラグを生成することによって、あなたが求めていることを実行します。

0

このコードは、あなたがしようとしていると思われるもののモックアップです。それは、整数のベクトルを生成します。整数のベクトルは、それぞれがサブセットに属しているかどうかを示すフラグ(none、some、またはall)を持ちます。ダミー関数(standin for subset-sum)は、テストに合格するかどうかを決定します。 project.clj[tupelo "0.9.56"]が必要です。

(ns tst.demo.core 
    (:require 
    [clojure.test.check :as tc] 
    [clojure.test.check.generators :as tcgen] 
    [clojure.test.check.properties :as tcprop] 
    [tupelo.core :as t] 
    [tupelo.test :as tt] 
    [schema.core :as s])) 

(def IntBoolTuple [ (s/one s/Int "x1") (s/one s/Bool "x2") ]) 

(s/defn is-sum-tuple? :- s/Bool 
    [tuple :- IntBoolTuple] 
    (let [[int-val bool-val] tuple] 
    bool-val)) 

(s/defn tst-fn :- s/Bool 
    "Dummy test function" 
    [tuples :- [IntBoolTuple]] 
    (let [all-ints  (mapv first tuples) 
     flags   (mapv second tuples) 
     tuples-to-add (vec (filter is-sum-tuple? tuples)) 
     ints-to-add (mapv first tuples-to-add) 
     int-sum  (apply + ints-to-add)] 
    (< -20 int-sum))) ; dummy test: pass if not too negative 

(tt/dospec 999 
    (tcprop/for-all [tuples (tcgen/such-that 
          (fn st-fn [tuples] (pos? (count tuples))) ; ensure at least 1 tuple is present 
          (tcgen/vector ; 0 or more tuples, ex: [ [5 false] [2 true] ...] 
           (tcgen/tuple tcgen/int tcgen/boolean) ; a tuple like [5 false] 
          ))] 
    (t/spyx tuples) ; display 
    (tst-fn tuples))) 

上記のコードは次のように出力を生成します。

--------------------------------------- 
    Clojure 1.9.0-beta1 Java 9.0.1 
--------------------------------------- 

Testing tst.demo.core 
tuples => [[-1 false]] 
tuples => [[1 true]] 
tuples => [[-2 true] [-1 false]] 
tuples => [[3 true] [-1 true]] 
tuples => [[1 true]] 
tuples => [[-1 true] [5 true] [-4 true] [5 false]] 
tuples => [[5 true] [2 false] [3 false] [1 true] [0 true]] 
tuples => [[2 false] [-4 false]] 
tuples => [[2 false]] 
tuples => [[2 false] [8 true] [9 true] [9 true] [3 true] [-7 true] [-9 true] [8 false] [-9 true]] 
tuples => [[-9 true] [-1 true]] 
tuples => [[4 false] [-6 true] [0 false] [10 true]] 
tuples => [[-2 false] [7 true] [-12 false] [4 false] [4 false] [11 true] [6 false] [-5 false]] 
tuples => [[-5 false] [6 true] [9 true] [-7 true] [1 false] [3 false] [-9 true] [-9 true] [-8 true] [-8 false] [-12 false]] 
tuples => [[-7 false] [-14 false] [8 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-14 false] [8 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-7 false] [8 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-7 false] [-14 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-7 false] [-14 false] [8 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-7 false] [-14 false] [8 false] [-1 false] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-7 false] [-14 false] [8 false] [-1 false] [-14 true] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-7 false] [-14 false] [8 false] [-1 false] [-14 true] [-10 false] [1 false] [5 false] [-13 true]] 
tuples => [[-7 false] [-14 false] [8 false] [-1 false] [-14 true] [-10 false] [-8 true] [5 false] [-13 true]] 
tuples => [[-7 false] [-14 false] [8 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [-13 true]] 
tuples => [[-7 false] [-14 false] [8 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false]] 
tuples => [[8 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-14 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-14 false] [8 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-14 false] [8 false] [-1 false] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-14 false] [8 false] [-1 false] [-14 true] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-14 false] [8 false] [-1 false] [-14 true] [-10 false] [1 false] [5 false] [-13 true]] 
tuples => [[-14 false] [8 false] [-1 false] [-14 true] [-10 false] [-8 true] [5 false] [-13 true]] 
tuples => [[-14 false] [8 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [-13 true]] 
tuples => [[-14 false] [8 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false]] 
tuples => [[-1 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[8 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[8 false] [-1 false] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[8 false] [-1 false] [-14 true] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[8 false] [-1 false] [-14 true] [-10 false] [1 false] [5 false] [-13 true]] 
tuples => [[8 false] [-1 false] [-14 true] [-10 false] [-8 true] [5 false] [-13 true]] 
tuples => [[8 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [-13 true]] 
tuples => [[8 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false]] 
tuples => [[-14 true] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-1 false] [-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-1 false] [-14 true] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-1 false] [-14 true] [-10 false] [1 false] [5 false] [-13 true]] 
tuples => [[-1 false] [-14 true] [-10 false] [-8 true] [5 false] [-13 true]] 
tuples => [[-1 false] [-14 true] [-10 false] [-8 true] [1 false] [-13 true]] 
tuples => [[-1 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false]] 
tuples => [[-10 false] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-14 true] [-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-14 true] [-10 false] [1 false] [5 false] [-13 true]] 
tuples => [[-14 true] [-10 false] [-8 true] [5 false] [-13 true]] 
tuples => [[-14 true] [-10 false] [-8 true] [1 false] [-13 true]] 
tuples => [[-14 true] [-10 false] [-8 true] [1 false] [5 false]] 
tuples => [[-8 true] [1 false] [5 false] [-13 true]] 
tuples => [[-10 false] [1 false] [5 false] [-13 true]] 
tuples => [[-10 false] [-8 true] [5 false] [-13 true]] 
tuples => [[-10 false] [-8 true] [1 false] [-13 true]] 
tuples => [[-10 false] [-8 true] [1 false] [5 false]] 
tuples => [[1 false] [5 false] [-13 true]] 
tuples => [[-8 true] [5 false] [-13 true]] 
tuples => [[-8 true] [1 false] [-13 true]] 
tuples => [[-8 true] [1 false] [5 false]] 
tuples => [[5 false] [-13 true]] 
tuples => [[-8 true] [-13 true]] 
tuples => [[-8 true] [5 false]] 
tuples => [[-13 true]] 
tuples => [[-8 true]] 
tuples => [[0 true] [-13 true]] 
tuples => [[-4 true] [-13 true]] 
tuples => [[-6 true] [-13 true]] 
tuples => [[-7 true] [-13 true]] 
tuples => [[-8 false] [-13 true]] 
tuples => [[-13 true]] 
tuples => [[-7 true]] 
tuples => [[0 true] [-13 true]] 
tuples => [[-4 true] [-13 true]] 
tuples => [[-6 true] [-13 true]] 
tuples => [[-7 false] [-13 true]] 
tuples => [[-7 true] [0 true]] 
tuples => [[-7 true] [-7 true]] 
tuples => [[-7 true] [-10 true]] 
tuples => [[-7 true] [-12 true]] 
tuples => [[-7 true] [-13 false]] 
{:result false, :seed 1511919758351, :failing-size 14, :num-tests 15, :fail [[[-7 false] [-14 false] [8 false] [-1 false] [-14 true] [-10 false] [-8 true] [1 false] [5 false] [-13 true]]], :shrunk {:total-nodes-visited 27, :depth 9, :result false, :smallest [[[-7 true] [-13 true]]]}, :test-var "dospec-line-46"} 

FAIL in (dospec-line-46) (clojure_test.cljc:21) 
expected: result 
    actual: false 
関連する問題