Exemplo n.º 1
0
Relation Relation::combinator(Relation& right)
{
	Scheme temp;

	vector <pair<int,int> > at_relation;

	temp = my_values.combine_schemes(right.my_values, at_relation);

	Relation result; 

	for (int i = 0; i < temp.size(); i++)

		result.set_value(temp.at(i));

	for (set<Tuple>::iterator it = Tuple_Set.begin(); it != Tuple_Set.end(); ++it)

		for (set<Tuple>::iterator it2 = right.Tuple_Set.begin(); it2 != right.Tuple_Set.end(); ++it2)
		{
			Tuple t(*it);

			if (t.joinable((*it2), at_relation))
			{
				t = t.combinator((*it2), at_relation);

				result.set_tuple(t);
			}
		}
			return result;
}
Exemplo n.º 2
0
string Interpretter::outputFacts() {
    string out = "";
    // get a list of relations
    for (auto relation : db) {
        // print the relation
        out += relation.first + "\n";
        Scheme s = relation.second.getScheme();
        set <Tuple> t = relation.second.getTuples();
        
        // for each of the tuples in the relation's set
        for (auto tuple : t) {
            out += "  ";
            // for each of the attributes in the relation's scheme:
            for (int attr = 0; attr < s.size(); attr++) {
                // print the attribute
                out += s[attr] + "=";
                
                // print the value at the relation's tuple
                out += tuple[attr] + " ";
            }
            if (tuple.size() > 0) {
                out.pop_back();
            }
        out += "\n";
        }
    out += "\n";
    }
    return out;
}
//static
uint SchemeUtils::calculateQuantumCost(const Scheme& scheme)
{
    uint size = scheme.size();

    if (size == 0)
        return 0;

    ReverseElement prevElement;
    bool isPreviousElementWasUsedBefore = true;

    uint cost = 0;
    for (auto& element : scheme)
    {
        if (!isPreviousElementWasUsedBefore)
        {
            uint peresCost = 0;
            if (isPeresGate(prevElement, element, &peresCost))
            {
                cost -= getElementQuantumCost(prevElement);
                cost += peresCost;

                isPreviousElementWasUsedBefore = true;
                continue;
            }
        }

        cost += getElementQuantumCost(element);

        prevElement = element;
        isPreviousElementWasUsedBefore = false;
    }

    return cost;
}
Exemplo n.º 4
0
//------------------------------------------------------------------//
// ---------------- LAPLACE -- CELL CENTER -------------------------//
//------------------------------------------------------------------//
// (1) The following function is the core others use this function  //
//------------------------------------------------------------------//
void Grid::triLaplace(triLinSys &axb, shared_ptr<Cell > f, double const &c) {
  if (!thisVar) {cout << "Laplace0: Variable is not locked!!"<< endl; return; }
  //  cout << "LAPLACE !!!! "<< endl; 
  //  cout << f->vol() << endl; 
  Scheme<double> sch = f->normGrad(thisVar); 
  auto area = f->vol().abs(); 
  int n = f->next; 
  int p = f->prev;
  for (auto i = 0; i < sch.size(); ++i) { 
    //    cout << "[" << n << ", "<< p << "] "<< c << " : " << sch.val[i] << " : " << area; 
    auto flux = c*sch.val[i]*area;
    //cout << " : " << flux << endl; 
    //cin.ignore().get(); 
    if (n >= 0) axb.A(n, sch.ind[i]) -= flux/listCell[n]->vol().abs(); 
    if (p >= 0) axb.A(p, sch.ind[i]) += flux/listCell[p]->vol().abs(); 
  }
  if (n >= 0) axb.b[n] += c*sch.c*area/listCell[n]->vol().abs(); 
  if (p >= 0) axb.b[p] -= c*sch.c*area/listCell[p]->vol().abs(); 
  return;  
  // int n = f->next; 
  // int p = f->prev;
  // auto c0 = (p>=0) ? *(listCell.begin() + p) : f;
  // auto c1 = (n>=0) ? *(listCell.begin() + n) : f;
  // Vec3 dx = c1->getCoord() - c0->getCoord(); 
  // Vec3 faceArea = f->vol(); 
  // Vec3 norm = faceArea/faceArea.abs();
  // if (p>=0 && n>=0) {
  //   double flux = c*faceArea.abs()/(dx*norm);
  //   axb.A[p][p] -= flux; 
  //   axb.A[n][n] -= flux; 
  //   axb.A[p][n] += flux; 
  //   axb.A[n][p] += flux;
  //   return; 
  // } else {
  //   if (!thisVar) {cout<<"laplace0: Boundary is not locked!"<<endl; return;}
  //   auto bndr = (p >= 0) ? -n-1 : -p-1; 
  //   auto row = (p >= 0) ? p : n; 
  //   if (bndr < 0 || bndr >= thisVar->listBC.size() || !(thisVar->listBC[bndr])) {
  //     cout << "wrong with bndr : Value "<< bndr <<endl; 
  //     return; 
  //   }
  //   auto bc = thisVar->listBC[bndr];
  //   if (bc->type == 0) {
  //     double flux = c*faceArea.abs()/(dx*norm);
  //     axb.A[row][row] -= flux*(1.0 - bc->a_val);
  //     axb.b[row] -= flux*(bc->b_val); 
  //   } else if (bc->type == 1) { 
  //     double flux = c*faceArea.abs();
  //     if (n >= 0) flux = -flux; 
  //     axb.A[row][row] += flux*(bc->a_val);
  //     axb.b[row] -= flux*(bc->b_val);
  //   } else {
  //     cout << "Type is not recognized!"<< endl; 
  //   }
  // }	
  return; 
}
Scheme CompositeGenerator::generate(const TruthTable& table, ostream& outputLog)
{
    float totalTime = 0;
    float time = 0;

    // process truth table with Reed-Muller generator
    uint size = table.size();
    uint n = (uint)(log(size) / log(2));
    outputLog << "n = " << n << endl;

    uint threshold = getRmGeneratorWeightThreshold(n);
    outputLog << "RM generator index weight threshold: " << threshold << endl;

    RmGenerator rmGenerator(threshold);
    RmGenerator::SynthesisResult rmResult;

    {
        AutoTimer timer(&time);
        rmGenerator.generate(table, &rmResult);
    }
    totalTime += time;

    outputLog << "RM generator time: ";
    logTime(outputLog, time);
    outputLog << "RM scheme complexity: " << rmResult.scheme.size() << endl;

    // process residual truth table with Group Theory based generator
    GtGenerator gtGenerator;

    Scheme gtLeftScheme;
    Scheme gtRightScheme;

    {
        AutoTimer timer(&time);
        gtLeftScheme = gtGenerator.generate(rmResult.leftMultTable);
        gtRightScheme = gtGenerator.generate(rmResult.rightMultTable);
    }
    totalTime += time;

    outputLog << "GT generator time: ";
    logTime(outputLog, time);
    outputLog << "GT left scheme complexity: " << gtLeftScheme.size() << endl;
    outputLog << "GT right scheme complexity: " << gtRightScheme.size() << endl;

    // combine GT and RM schemes
    Scheme& scheme = rmResult.scheme;

    RmGenerator::PushPolicy pushPolicy = rmGenerator.getPushPolicy();
    if (pushPolicy.defaultPolicy)
    {
        if (gtLeftScheme.size() < gtRightScheme.size())
            scheme.insert(scheme.begin(), gtLeftScheme.cbegin(), gtLeftScheme.cend());
        else
            scheme.insert(scheme.end(), gtRightScheme.cbegin(), gtRightScheme.cend());
    }
    else
    {
        scheme.insert(scheme.begin(), gtLeftScheme.cbegin(), gtLeftScheme.cend());
        scheme.insert(scheme.end(), gtRightScheme.cbegin(), gtRightScheme.cend());
    }

    outputLog << "Complexity before optimization: " << scheme.size() << endl;
    outputLog << "Quantum cost before optimization: ";
    outputLog << SchemeUtils::calculateQuantumCost(scheme) << endl;

    // optimize scheme complexity
    PostProcessor postProcessor;

    {
        AutoTimer timer(&time);
        scheme = postProcessor.optimize(scheme);
    }
    totalTime += time;

    bool isValid = TruthTableUtils::checkSchemeAgainstPermutationVector(scheme, table);
    assert(isValid, string("Generated scheme is not valid"));

    // log post processing parameters
    outputLog << "Optimization time: ";
    logTime(outputLog, time);
    outputLog << "Complexity after optimization: " << scheme.size() << endl;
    outputLog << "Quantum cost after optimization: ";
    outputLog << SchemeUtils::calculateQuantumCost(scheme) << endl;

    outputLog << "Total time: ";
    logTime(outputLog, totalTime);

    return scheme;
}