Beispiel #1
0
void ConstraintBase::setStatus(ConstraintStatus _status)
{
    if (getProblem() == nullptr)
        std::cout << "constraint not linked with 'top', only status changed" << std::endl;
    else if (_status != status_)
    {
        if (_status == CTR_ACTIVE)
            getProblem()->addConstraintPtr(this);
        else if (_status == CTR_INACTIVE)
            getProblem()->removeConstraintPtr(this);
    }
    status_ = _status;
}
/**
 * Problem factory which uses the same arguments as the main from where
 * is called (minimum two arguments)
 * @param argc : Number of arguments
 * @param argv : Array of arguments
 */
Problem * ProblemFactory::getProblem(int argc, char ** argv) {
  if (argc==2) {
    return getProblem(argv[1], 0, NULL);
  } else if (argc>2) {
    char * argv1 = argv[1];
    for (int i=0; i<argc-2; i++) {
      argv[i] = argv[i+2];
    }
    return getProblem(argv1, argc-2, argv);
  } else {
    cerr << "Too few arguments to build a problem.";
    exit(-1);
  }
}
Beispiel #3
0
/* Train problem by Liblinear L1R_LR */
void Machine::training()
{
    // init parameter
    parameter _parameter;
    _parameter.solver_type = L1R_LR; // type of Machine learning Algorithm
    _parameter.eps = 0.01;
    _parameter.C = 1;
    _parameter.nr_weight = 0;
    _parameter.weight_label = new int(1);
    _parameter.weight = new double(1.0);

    clock_t start, finish;

    start = clock();

    cout << "Start training ..." << endl;
    getProblem();
    _model = train( &_problem, &_parameter);
    cout << "Finish training. " 	<< endl;

    finish = clock();
    printf("training took %f seconds to execute\n",
           ((double) (finish - start)) / CLOCKS_PER_SEC); //

    return;
}
Beispiel #4
0
std::string CEFMTask::getInternalSpecies(const CFluxMode & fluxMode) const
{
  const CModel* pModel = getProblem()->getModel();

  if (pModel == NULL)
    return "";

  std::map< const CMetab *, C_FLOAT64 > Data = getNetReactionData(fluxMode);

  std::stringstream Modifiers;
  Modifiers.flags(std::ios::fixed);
  Modifiers.precision(0);
  std::string ModifiersSeparator = "";

  std::map< const CMetab *, C_FLOAT64 >::const_iterator it = Data.begin();
  std::map< const CMetab *, C_FLOAT64 >::const_iterator end = Data.end();

  for (; it != end; ++it)
    {
      if (fabs(it->second) < 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon())
        {
          Modifiers << ModifiersSeparator;
          Modifiers << CMetabNameInterface::getDisplayName(pModel, *it->first, true);
          ModifiersSeparator = ", ";
        }
    }

  return Modifiers.str();
}
Beispiel #5
0
std::string CEFMTask::getNetReaction(const CFluxMode & fluxMode) const
{
  const CModel* pModel = getProblem()->getModel();

  if (pModel == NULL)
    return "";

  std::map< const CMetab *, C_FLOAT64 > Data = getNetReactionData(fluxMode);

  std::stringstream Substrates;
  Substrates.flags(std::ios::fixed);
  Substrates.precision(0);
  std::string SubstratesSeparator = "";

  std::stringstream Products;
  Products.flags(std::ios::fixed);
  Products.precision(0);
  std::string ProductsSeparator = "";

  std::map< const CMetab *, C_FLOAT64 >::const_iterator it = Data.begin();
  std::map< const CMetab *, C_FLOAT64 >::const_iterator end = Data.end();

  for (; it != end; ++it)
    {
      if (it->second > 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon())
        {
          Products << ProductsSeparator;

          if (it->second > 1.0 + 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon())
            {
              Products << it->second << " * ";
            }

          Products << CMetabNameInterface::getDisplayName(pModel, *it->first, true);
          ProductsSeparator = " + ";
        }
      else if (it->second < -100.0 * std::numeric_limits< C_FLOAT64 >::epsilon())
        {
          Substrates << SubstratesSeparator;

          if (it->second < -1.0 - 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon())
            {
              Substrates << -it->second << " * ";
            }

          Substrates << CMetabNameInterface::getDisplayName(pModel, *it->first, true);
          SubstratesSeparator = " + ";
        }
    }

  if (fluxMode.isReversible())
    {
      return Substrates.str() + " = " + Products.str();
    }
  else
    {
      return Substrates.str() + " -> " + Products.str();
    }
}
Beispiel #6
0
ConstraintBase::~ConstraintBase()
{
	//std::cout << "deleting ConstraintBase " << nodeId() << std::endl;
    is_deleting_ = true;

    // add constraint to be removed from solver
    if (getProblem() != nullptr)
        getProblem()->removeConstraintPtr(this);

    //std::cout << "removeConstraintPtr " << std::endl;

    // remove constraint to frame/landmark/feature
    if (frame_ptr_ != nullptr)
        frame_ptr_->removeConstrainedBy(this);
    if (feature_ptr_ != nullptr)
        feature_ptr_->removeConstrainedBy(this);
    if (landmark_ptr_ != nullptr)
        landmark_ptr_->removeConstrainedBy(this);

    //std::cout << "removed constraints to " << std::endl;
}
Beispiel #7
0
string Machine::segment(string sentence)
{
    // initialize feats
    feats->clear();

    // extract sentence to feats
    extract(sentence, PREDICT);
    if (feats->size() == 0)	return "";

    // convert feats to Liblinear's problem struct
    getProblem();

    string ans = "";
    for (size_t i = 0; i < feats->size(); ++i) {
        if (predict(_model, _problem.x[i]) == index_SPACE)
            ans += vfeats->at(i + WINDOW_LENGTH).syllabel + SPACE;
        else
            ans += vfeats->at(i + WINDOW_LENGTH).syllabel + UNDER;
    }
    ans += vfeats->at(feats->size() + WINDOW_LENGTH).syllabel;

    while (ans[0] <= 40 && ans.length() > 0) ans.erase(0);
    string dummy = "";
    dummy += ans[0];
    for(size_t i = 1; i < ans.length(); ++i) {
        if ((ans[i] == SPACE || ans[i] == UNDER)
                && (ans[i-1] == SPACE || ans[i-1] == UNDER)) {
            // do nothing
        }
        else {
            dummy += ans[i];
        }
    }

    // check the last character. If last character is '.' and previous character
    // is underscore, we will replace underscore by space
    if (dummy[dummy.size()-1] == '.' && dummy[dummy.size()-2] == UNDER) {
        dummy[dummy.size()-2] = SPACE;
    }

    //delete problem and free memory
    delProblem();
    feats->clear();

    return dummy;
}
Beispiel #8
0
//==============================================================================
std::ostream& Population::print(std::ostream& os) const
{
  os << getProblem() << "\n";
  os << "Population size: " << getSize() << "\n\n";
  os << "List of individuals: \n";

  for (std::size_t i = 0u; i < getSize(); ++i)
  {
    os << "#" << i << ":\n";
    os << "\tDecision vector:\t"
       << mPopulation.col(static_cast<int>(i)).transpose() << "\n";
    os << "\tDecision vector:\t"
       << mFitness.col(static_cast<int>(i)).transpose() << "\n";
  }

  // TODO(JS): Print champions if the dimension is one.

  return os;
}
Beispiel #9
0
int main() {
	// Get the names of all the roommates and put them in the names vector
	// Make sure that this code can access the input file 
	// and that the input file is named rm.txt
	vector<string> names = getProblem("rm.txt");

	// Will be a 2-d vector of names. Every vector inside this vector represents a room.
	vector< vector<string> > solution;

	// EXAMPLE SOLUTION: put people into rooms of two using their order in the names vector
	for(int a = 0; a < names.size(); a += 4) {
		vector<string> room;
		room.push_back(names[a]);
		room.push_back(names[a+1]);
		room.push_back(names[a+2]);
		room.push_back(names[a+3]);

		solution.push_back(room);
	}

	outputSolutionsToFile("PUT YOUR NAME HERE", solution);

	return EXIT_SUCCESS;
}
/**
 * Problem factory
 * @param name : Name of the problem
 */
Problem * ProblemFactory::getProblem(char * name) {
  return getProblem(name, 0, NULL);
}
Beispiel #11
0
//Główny proces zakładu
void proces_zaklad(int me, int typ_zakladu, double *times)
{
	//Zapisywane łączne czasy wykonywanych operacji
	double *timeWork = times+0, *timeProblems = times+1, *timeMyProblems = times+2, *timeWait = times+3;
	//Czy ten zakład ma nierozwiązany problem
	bool problemRekrutacja = false, problemSporPrawny = false, problemInform = false;
	//Licznik rozwiązanych macierzy od klientów
	int matrixes_solved = 0;
	//Rodzaj rozwiązywanych problemów przez zakład
	enum Problem rozwiazujeP = rozwiazuje(typ_zakladu);

	//Maksymalna liczba rozwiązanych macierzy
	while(matrixes_solved < 10000)
	{
		MPI_Status status, status_tmp;
		int tag, source, flag = 0;

		//Sprawdzenie czy istnieje oczekująca wiadomość
		MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, &status);
		if(flag) //Oczekująca wiadomość na odebranie
		{
			source = status.MPI_SOURCE; //Nadawca wiadomości
			tag = status.MPI_TAG; //Tag wiadomości
			if(tag == END_PROGRAM) //Oczekująca wiadomość to informacja o zakończeniu programu
			{
				//Opcjonalne, odebranie wiadomości o zakończeniu programu
				MPI_Recv(0, 0, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status_tmp);
				return;
			}
			else if(tag == MESSAGE) //Oczekująca wiadomość to problem do rozwiązania
			{
				struct Message m;

				//Odebranie wiadomości
				MPI_Recv(&m, sizeof(struct Message), MPI_CHAR, source, tag, MPI_COMM_WORLD, &status_tmp);

				if(m.problemType == NONE) //Wiadomość to macierz do rozwiązania od klient
				{
					//Rozwiązanie macierzy od klienta
					double *values = NULL;
					int size = m.matrixSize;
					getMatrix(size, &values);
					*timeWork += solveProblem(size, values);
					free(values);

					matrixes_solved++;

					//"Generacja" problemu
					enum Problem p = getProblem();
					switch(p)
					{
						case REKRUTACJA:
						problemRekrutacja = true;
						break;

						case SPOR_PRAWNY:
						problemSporPrawny = true;
						break;

						case INFORM:
						problemInform = true;
						break;
					}

					if(p != NONE) //Wystąpił problem, powiadomienie procesu nadzorcy o oczekującym na rozwiązanie problemie
					{
						struct Message mx;
						mx.matrixSize = getProblemSize(p);
						mx.problemType = p;
						mx.problemSource = me;
						MPI_Send(&mx, sizeof(struct Message), MPI_CHAR, 0, MESSAGE, MPI_COMM_WORLD);
					}
					
				}
				else if(m.problemType == rozwiazujeP) //Wiadomość to problem który może rozwiązać ten zakłąd
				{
					//Rozwiązanie problemu
					double *values = NULL;
					int size = m.matrixSize;
					getMatrix(size, &values);
					*timeProblems += solveProblem(size, values);
					free(values);

					//Powiadomienie procesu nadzorcy o rozwiązaniu problemu
					MPI_Send(&m, sizeof(struct Message), MPI_CHAR, 0, PROBLEM_SOL, MPI_COMM_WORLD);
					//Powiadomienie "autora" problemu o rozwiązaniu problemu
					MPI_Send(&m, sizeof(struct Message), MPI_CHAR, m.problemSource, PROBLEM_SOL, MPI_COMM_WORLD);
				}
			}
			else if(tag == PROBLEM_SOL) //Oczekująca wiadomość to informacja o rozwiązaniu problemu
			{
				//Odebranie wiadomości
				struct Message m;
				MPI_Recv(&m, sizeof(struct Message), MPI_CHAR, source, tag, MPI_COMM_WORLD, &status_tmp);

				switch(m.problemType)
				{
					case REKRUTACJA:
					problemRekrutacja = false;
					break;

					case SPOR_PRAWNY:
					problemSporPrawny = false;
					break;

					case INFORM:
					problemInform = false;
					break;
				}
			}
		}
		else //Brak oczekującej wiadomości, żądanie otrzymania zadania do wykonania
		{
			struct Message m;
			m.problemType = NONE;
			MPI_Send(&m, sizeof(struct Message), MPI_CHAR, 0, MESSAGE, MPI_COMM_WORLD);
		}
	}

	//Powiadomienie procesu nadzorcy o zakończeniu programu
	MPI_Send(0, 0, MPI_CHAR, 0, END_PROGRAM, MPI_COMM_WORLD);
}