예제 #1
0
      // Remove a single constraint
   ConstraintSystem& ConstraintSystem::removeConstraint(
                                                  const Constraint& constraint )
   {
      ConstraintList backupList;
      
      for(ConstraintList::iterator it= constraintList.begin();
         it!=constraintList.end();
         ++it)
      {
         bool isEqual(true);

         if(it->header.prefit!=constraint.header.prefit) isEqual=false;
         if(it->header.variance!=constraint.header.variance) isEqual=false;
         if(it->body != constraint.body) isEqual = false;

         if( !isEqual ) backupList.push_back(*it);
      }

      clearConstraint();
      
      for(ConstraintList::iterator it= backupList.begin();
         it!=backupList.end();
         ++it)
      {
         addConstraint(*it);
      }
      
      return (*this);

   }  // End of method 'ConstraintSystem::removeConstraint()'
예제 #2
0
bool DietWizardDialog::checkLimits( IngredientPropertyList &properties, ConstraintList &constraints )
{
	for ( ConstraintList::const_iterator ct_it = constraints.constBegin(); ct_it != constraints.constEnd(); ++ct_it ) {
		if ( (*ct_it).enabled ) {
			IngredientPropertyList::iterator ip_it = properties.find( (*ct_it).id );
			if ( ip_it != properties.end() ) {
				if ( ( (*ip_it).amount > (*ct_it).max ) || ( (*ip_it).amount < (*ct_it).min ) )
					return false;
			}
			else
				return false;
		}
	}
	return true;
}
    set<Constraint *> subConstraint(){
    	set <Constraint *> subcstr;
    	set<int> scope;
    	for(int k=0; k < arity(); k++) {
    		scope.insert(getVar(k)->wcspIndex);
    	}
    	for(set<int>::iterator itx = scope.begin(); itx != scope.end(); ++itx){
    		ConstraintList* xctrs = (wcsp->getVar(*itx))->getConstrs();
    		for (ConstraintList::iterator it=xctrs->begin(); it != xctrs->end(); ++it){
    			Constraint * ctr = (*it).constr;
    			if(ctr->arity() < arity() && scopeIncluded(ctr)) subcstr.insert(ctr);
    		}
    	}

    	return subcstr;
    }
예제 #4
0
int DietWizardDialog::getNecessaryFlags() const
{
	bool need_ingredients = false;
	bool need_categories = false;
	for ( int meal = 0;meal < mealNumber;meal++ ) {
		int dishNo = ( ( MealInput* ) ( mealTabs->widget( meal ) ) ) ->dishNo();
		for ( int dish = 0;dish < dishNo;dish++ ) {
			if ( !need_categories ) {
				if ( categoryFiltering( meal, dish ) ) {
					need_categories = true;
				}
			}

			if ( !need_ingredients ) {
				ConstraintList constraints;
				loadConstraints( meal, dish, &constraints );
				for ( ConstraintList::const_iterator ct_it = constraints.constBegin(); ct_it != constraints.constEnd(); ++ct_it ) {
					if ( (*ct_it).enabled ) {
						need_ingredients = true;
						break;
					}
				}
			}

			if ( need_ingredients && need_categories )
				break;
		}

		if ( need_ingredients && need_categories )
			break;
	}

	kDebug()<<"Do we need to load ingredients: "<<need_ingredients;
	kDebug()<<"Do we need to load categories: "<<need_categories;

	int flags = 0;
	if ( need_ingredients ) flags |= RecipeDB::Ingredients;
	if ( need_categories ) flags |= RecipeDB::Categories;

	return flags;
}
예제 #5
0
bool DietWizardDialog::checkConstraints( Recipe &rec, int meal, int dish )
{
	// Check if the properties are within the constraints
	ConstraintList constraints;
	loadConstraints( meal, dish, &constraints ); //load the constraints

	bool any_enabled = false;
	for ( ConstraintList::const_iterator ct_it = constraints.constBegin(); ct_it != constraints.constEnd(); ++ct_it ) {
		if ( (*ct_it).enabled ) {
			any_enabled = true;
			break;
		}
	}
	if ( !any_enabled )
		return true;

	// Calculate properties of the recipe
	calculateProperties( rec, database );

	bool withinLimits = checkLimits( rec.properties, constraints );

	return ( withinLimits );
}
예제 #6
0
TEST_F(TablesTests, test_constraint_map) {
  ConstraintMap cm;
  ConstraintList cl;

  cl.add(Constraint(EQUALS, "some"));
  cm["path"] = cl;

  // If a constraint list exists for a map key, normal constraints apply.
  EXPECT_TRUE(cm["path"].matches("some"));
  EXPECT_FALSE(cm["path"].matches("not_some"));

  // If a constraint list does not exist, then all checks will match.
  // If there is no predicate clause then all results will match.
  EXPECT_TRUE(cm["not_path"].matches("some"));
  EXPECT_TRUE(cm["not_path"].notExistsOrMatches("some"));
  EXPECT_FALSE(cm["not_path"].exists());
  EXPECT_FALSE(cm["not_path"].existsAndMatches("some"));

  // And of the column has constraints:
  EXPECT_TRUE(cm["path"].notExistsOrMatches("some"));
  EXPECT_FALSE(cm["path"].notExistsOrMatches("not_some"));
  EXPECT_TRUE(cm["path"].exists());
  EXPECT_TRUE(cm["path"].existsAndMatches("some"));
}
예제 #7
0
      // Impose the constraints system to the equation system
      // the prefit residuals vector, hMatrix and rMatrix will be appended.
   void EquationSystem::imposeConstraints()
   {
      if(!equationConstraints.hasConstraints()) return;

      ConstraintList destList;

      ConstraintList tempList = equationConstraints.getConstraintList();
      for(ConstraintList::iterator it = tempList.begin();
         it != tempList.end();
         ++it )
      {
         VariableDataMap dataMapOk;

         bool validConstraint(true);

         try
         {
            VariableDataMap dataMap = it->body;
            for(VariableDataMap::iterator itv = dataMap.begin();
               itv != dataMap.end();
               ++itv )
            {
               bool isFound(false);

               VariableSet::iterator itv2 = varUnknowns.find(itv->first);
               if(itv2!=varUnknowns.end())
               {
                  isFound = true;
                  dataMapOk[*itv2] = dataMap[itv->first];
               }
               else
               {
                  for(itv2 = varUnknowns.begin();
                     itv2 != varUnknowns.end();
                     ++itv2 )
                  {
                     if( (itv->first.getType() == itv2->getType()) &&
                        (itv->first.getSource() == itv2->getSource()) &&
                        (itv->first.getSatellite() == itv2->getSatellite()) )
                     {
                        isFound = true;
                        dataMapOk[*itv2] = dataMap[itv->first];
                        break;
                     }
                  }
               }

               if( !isFound ) validConstraint = false;
            }
         }
         catch(...)
         {
            validConstraint = false;
         }

         if(validConstraint)
         {
            destList.push_back(Constraint(it->header,dataMapOk));
         }
         else
         {
            // we discard all constraints
            return;
         }

      }
         // Update the equation system
      equationConstraints.setConstraintList(destList);


         // Now, we can append the matrix(prefit design and weight)
      try
      {
         Vector<double> meas;
         Matrix<double> design;
         Matrix<double> cov;

         equationConstraints.constraintMatrix(varUnknowns,
            meas, design, cov);

         const int oldSize = measVector.size();
         const int newSize = oldSize + meas.size();
         const int colSize = hMatrix.cols();

         Vector<double> tempPrefit(newSize,0.0);
         Matrix<double> tempGeometry(newSize,colSize,0.0);
         Matrix<double> tempWeight(newSize,newSize,0.0);

         for(int i=0; i< newSize; i++)
         {
               // prefit
            if(i<oldSize) tempPrefit(i) = measVector(i);
            else tempPrefit(i) = meas(i-oldSize);

               // geometry
            for(int j=0;j<colSize;j++)
            {
               if(i<oldSize) tempGeometry(i,j) = hMatrix(i,j);
               else tempGeometry(i,j) = design(i-oldSize,j);
            }

               // weight
            if(i<oldSize) tempWeight(i,i) = rMatrix(i,i);
            else tempWeight(i,i) = 1.0/cov(i-oldSize,i-oldSize);

         }
            // Update these matrix
         measVector = tempPrefit;
         hMatrix = tempGeometry;
         rMatrix = tempWeight;
      }
      catch(...)
      {
         return;
      }

   }  // End of method 'EquationSystem::imposeConstraints()'