2016年1月13日水曜日

DIMACS のクリークのデータを Matlab 用に変換する

DIMACS のクリーク用データは、
https://turing.cs.hbg.psu.edu/txn131/clique.html
のサイトから入手できるが、バイナリ形式であるので、これを Matlab で扱いやすいように変換をする。

実際のファイルは、
https://turing.cs.hbg.psu.edu/txn131/file_instances/clique/DIMACS_cliques.tar.gz
にある。

これを展開して、 ascii ファイルに一度変換する。
変換ファイルは、
https://turing.cs.hbg.psu.edu/txn131/file_instances/converter.tar.gz
にあって、make でコンパイルした後に、
$ ./bin2asc DIMACS_cliques/MANN_a9.clq.b
とすると ascii ファイルで MANN_a9.clq が出来上がる。
一括変換なら
$ for i in DIMACS/*.clq.b; do ./bin2asc $i; done
である。

これを Matlab で処理して、一括変換する。
ただし、枝の数が 10 万を超えるものは、大きいので変換はスキップする。
以下のスクリプトを使っている。


TOO_LARGE = 100000;
dirs = dir('*.clq');
for index = 1:length(dirs)
    filename = dirs(index).name;
    fprintf('index = %d, filename = %s... ', index, filename);

    fp = fopen(filename, 'r');
    m = 0;
    n = 0;
    comments = {};
    while 1
        line1 = fgets(fp);
        if length(line1) == 1
            % end of file
            break;
        end
        if line1(1) == 'c'
            comments{length(comments)+1} = line1;
        end
        if line1(1) == 'p'
            strings = strsplit(line1);
            n = str2double(strings(3));
            m = str2double(strings(4));
            fprintf('node = %d, edge = %d\n', n, m);
            if m > TOO_LARGE
                break;
            end
            I = zeros(m, 1);
            J = zeros(m, 1);
            count = 1;
        end
        if line1(1) == 'e'
            strings = strsplit(line1);
            I(count) = str2double(strings(2));
            J(count) = str2double(strings(3));
            % fprintf('e[%d] = %d, %d\n', count, I(count), J(count));
            count = count + 1;
            if rem(count, 5000) == 0
                fprintf('e[%d]... ', count);
            end
        end
    end % end of while
        if m > TOO_LARGE
            fprintf(' : SKIP too large \n');
            continue; % to next file
        end
       
        A = sparse(I,J,ones(m,1),n,n);
        A = A+A';

    savefile = strrep(filename, '.clq', '.mat');
    save(savefile, 'A', 'comments');

    for i = 1:length(comments); fprintf('%s', comments{i}); end

end



出来上がったファイルは、
load MANN_a9
などで読み込むことができて、
for i = 1:length(comments); fprintf('%s', comments{i}); end
でコメントも表示できる。


0 件のコメント:

コメントを投稿