示例#1
0
文件: reasoner.cpp 项目: jrbn/vlog
ReasoningMode Reasoner::chooseMostEfficientAlgo(Literal &query,
        EDBLayer &layer, Program &program,
        std::vector<uint8_t> *posBindings,
        std::vector<Term_t> *valueBindings) {
    uint64_t cost = 0;
    if (posBindings != NULL) {
        //Create a new pattern with the values substituted
        int idxValues = 0;
        VTuple newTuple = query.getTuple();
        for (std::vector<uint8_t>::iterator itr = posBindings->begin(); itr != posBindings->end();
                ++itr) {
            newTuple.set(VTerm(0, valueBindings->at(idxValues)), *itr);
            idxValues++;
        }
        Literal newLiteral(query.getPredicate(), newTuple);
        size_t singleCost = estimate(newLiteral, NULL, NULL, layer, program);
        BOOST_LOG_TRIVIAL(debug) << "SingleCost is " <<
                                 singleCost << " nBindings " << (valueBindings->size() / posBindings->size());

        //Are bindings less than 10? Then singleCost is probably about right
        uint64_t nValues = valueBindings->size() / posBindings->size();
        if (nValues > 10) {
            //Copy the first 10 values
            std::vector<Term_t> limitedValueBindings;
            for (int i = 0; i < 10 * posBindings->size(); ++i) {
                limitedValueBindings.push_back(valueBindings->at(i));
            }
            uint64_t tenCost = estimate(query, posBindings,
                                        &limitedValueBindings, layer, program);
            BOOST_LOG_TRIVIAL(debug) << "TenCost is " << tenCost;

            //y = mx + b. m is slope, b is constant.
            //I assume the singleCost is the cost at position 0. Otherwise it's not a line
            double m = (double)(tenCost - singleCost) / (10);   // 9? --Ceriel
            long b = singleCost;
            cost = (uint64_t) (m * nValues + b);
        } else {
            cost = singleCost;
        }
    } else {
        cost = estimate(query, NULL, NULL, layer, program);
    }
    ReasoningMode mode = cost < threshold ? TOPDOWN : MAGIC;
    BOOST_LOG_TRIVIAL(debug) << "Deciding whether I should resolve " <<
                             query.tostring(&program, &layer) << " with magic or QSQR. Estimated cost: " <<
                             cost << " threshold for QSQ-R is " << threshold;
    return mode;
}
bool CNFGenerator::OnePersonOneTable()
{
	Clause onePersonAtLeastOneTable;
	Clause onePersonAtMaxOneTable;
	for (int i =0;i<people;i++)
	{
		onePersonAtLeastOneTable.clear();
		onePersonAtMaxOneTable.clear();
		//cout <<"atleast 1 =";
		for (int j=0;j<tables;j++)
		{
			Literal newLiteral(i,j,false);
			//cout <<newLiteral;
			onePersonAtLeastOneTable.insert(newLiteral);
			
		}
		//cout<<endl;

		//sort(onePersonAtLeastOneTable.begin(),onePersonAtLeastOneTable.end());
		SetOfClauses.insert(pair<Clause,int> (onePersonAtLeastOneTable,++CNFGenerator::globalClauseCount));
		
		for (int j=0;j<tables;j++)
		{
			for (int k=j+1;k<tables;k++)
			{
				onePersonAtMaxOneTable.clear();
				Literal newLiteral1(i,j,true);
				Literal newLiteral2(i,k,true);
				onePersonAtMaxOneTable.insert(newLiteral1);
				onePersonAtMaxOneTable.insert(newLiteral2);
				//sort(onePersonAtMaxOneTable.begin(),onePersonAtMaxOneTable.end());
				SetOfClauses.insert(pair<Clause,int> (onePersonAtMaxOneTable,++CNFGenerator::globalClauseCount));
			}
		}
		
	}

	return true;
}