Exemplo n.º 1
0
int main(int argc, char* argv[])
{
    int topK;
	if (argc != 5 || sscanf(argv[4], "%d", &topK) != 1) {
        cerr << argc << endl;
        for (int i = 1; i < argc; ++ i) {
            cerr << argv[i] << endl;
        }
		fprintf(stderr, "[usage] <training/testing data, csv format> <selected rules> <new training/testing data, csv format> <top-K>\n");
		return -1;
	}
	//cerr << "top-K = " << topK << endl;
	int dimension = loadFeatureMatrix(argv[1]);
	Rules rules;
	rules.load(argv[2]);

    rules.resize(topK);

	FILE* out = tryOpen(argv[3], "w");
	fprintf(out, "label");
	for (int i = 0; i < rules.size(); ++ i) {
	    fprintf(out, ",rule_%s", rules[i].showup().c_str());
	}
	fprintf(out, "\n");
	vector<int> sum(rules.size(), 0);
	for (int i = 0; i < train.size(); ++ i) {
	    fprintf(out, "%d", (int)trainY[i]);
	    for (int j = 0; j < rules.size(); ++ j) {
            int sat = rules[j].isSatisfiedBy(train[i]);
	        fprintf(out, ",%d", sat);
	        sum[j] += sat;
	    }
	    fprintf(out, "\n");
	}
	fclose(out);

    string filename = argv[1];
	if (filename.find("train") != -1) {
        bool flag = false;
        for (int i = 0; i < rules.size(); ++ i) {
            if (rules[i].satisfiedTrainings.size() != sum[i]) {
                fprintf(stderr, "[Warning] rule %d, %d v.s. %d\n", i, rules[i].satisfiedTrainings.size(), sum[i]);
                flag = true;
            }
        }
        if (!flag) {
            //fprintf(stderr, "Self-check passed!\n");
			;
        } else {
            fprintf(stderr, "Oooooooooooooooooops! You found some troubles!\n");
        }
    }

	return 0;
}
Exemplo n.º 2
0
TEST(Rules, reference) {
    Rules r { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, { 358, 288, 2 } };

    ASSERT_EQ(r[0], (Rule {1, 2, 3}));
    ASSERT_EQ(r[1], (Rule {4, 5, 6}));
    ASSERT_EQ(r[2], (Rule {7, 8, 9}));
    ASSERT_EQ(r.size(), size_t(4));

    swap(r[0], r[1]);
    ASSERT_EQ(r[0], (Rule {4, 5, 6}));
    ASSERT_EQ(r[1], (Rule {1, 2, 3}));

    std::sort(r.begin(), r.end(), rule_compare {});

    ASSERT_EQ(r[0], (Rule {1, 2, 3}));
    ASSERT_EQ(r[1], (Rule {4, 5, 6}));
    ASSERT_EQ(r[2], (Rule {7, 8, 9}));
    ASSERT_EQ(r.size(), size_t(4));

}