int main(int argc, char* argv[]) {

	// construct lattice
	unsigned int rows = 0;
	unsigned int columns = 200;
	lattice_t* lattice = lattice_create(rows, columns, 
		periodic, periodic, periodic, periodic);

	// populate lattice
	coordinate_t coord;
	int* agentId;
	int currentAgentCount = 1;

	for (int j = 10; j < 20; j++) {
		for (int i = 0; i < rows; i++) {
			coord.row = i;
			coord.column = j;

			// N.B. Be sure to free agent memory in lattice
			agentId = malloc(sizeof(int));
			*agentId = currentAgentCount++;

			lattice_push_agent(lattice, coord, agentId);
		}
	}

	// set motility properties
	double motilityProbability = 1.0;
	double xShiftPreference = 0;
	double yShiftPreference = 0;
	bool agentExclusion = true;

	// set proliferation properties
	double proliferationProbability = 0.001;
	proliferation_type_t proliferationType = adjacent;
	unsigned int proliferationDelta = 1;

	// initialise agent tracking information
	unsigned int numTrackedAgents = 1;
	unsigned int timeSteps = 500;
	
	coordinate_t** trackedPositions = malloc((timeSteps + 1) * sizeof(coordinate_t*));
	for (int i = 0; i < timeSteps + 1; i++) {
		trackedPositions[i] = malloc(numTrackedAgents * sizeof(coordinate_t));
	}

	int* trackedAgentIds = malloc(numTrackedAgents * sizeof(int));
	trackedAgentIds[0] = currentAgentCount - 1;

	coordinate_t initialCoord = { .row = rows - 1, .column = 19 };
	trackedPositions[0][0] = initialCoord;

	unsigned int totalAgentCount = 
		lattice_get_total_agent_count(lattice, rows, columns);
	printf("Initial agent count: %d\n", totalAgentCount);

	// Perform simulation
	for (int i = 1; i < timeSteps + 1; i++) {
		performMotilityEvents(lattice, rows, columns, motilityProbability, 
			xShiftPreference, yShiftPreference, agentExclusion, 
			trackedAgentIds, numTrackedAgents, trackedPositions[i]);

		performProliferationEvents(lattice, rows, columns, 
			proliferationProbability, agentExclusion, proliferationType, 
			proliferationDelta, trackedAgentIds, numTrackedAgents, 
			trackedPositions[i]);
	}

	totalAgentCount = 
		lattice_get_total_agent_count(lattice, rows, columns);
	printf("Initial agent count: %d\n", totalAgentCount);

	// Save proliferation information
	bool agentIsTracked = tracked_agents_parser(trackedPositions, timeSteps + 1,
		0, "simulation_tracked_agent.txt", "output/");
	if (agentIsTracked) {
		printf("Stored agent tracking information.\n");
	} else {
		printf("Error: failed to store agent tracking information.\n");
	}

	bool isTracked = lattice_profile_parser(lattice, rows, columns, 
		"simulation_mat.txt", "output/");
	if (isTracked) {
		printf("Stored lattice profile.\n");
	} else {
		printf("Error: failed to store lattice profile.\n");
	}

	// deallocate memory
	lattice_destroy(&lattice, rows, columns, true);
	free(trackedAgentIds);

	for (int i = 0; i < timeSteps + 1; i++) {
		free(trackedPositions[i]);
	}
	free(trackedPositions);

	return EXIT_SUCCESS;
}
int main(int argc, char* argv[]) {

	// construct lattice
	unsigned int rows = 3;
	unsigned int columns = 3;
	lattice_t* lattice = lattice_create(rows, columns, 
		periodic, periodic, periodic, periodic);

	// populate lattice
	int* agentId = malloc(sizeof(int));
	*agentId = 1;

	coordinate_t agentPos = { .row = 0, .column = 0 };
	lattice_push_agent(lattice, agentPos, agentId);	

	int* blockingAgentId = malloc(sizeof(int));
	*blockingAgentId = 2;

	coordinate_t blockingAgentPos = { .row = 2, .column = 2 };
	lattice_push_agent(lattice, blockingAgentPos, blockingAgentId);

	// set motility properties
	double motilityProbability = 1.0;
	double xShiftPreference = 0;
	double yShiftPreference = 0;
	bool agentExclusion = true;

	// initialise agent tracking information
	unsigned int numTrackedAgents = 2;
	unsigned int timeSteps = 1;
	
	coordinate_t** trackedPositions = malloc((timeSteps + 1) * sizeof(coordinate_t*));
	for (int i = 0; i < timeSteps + 1; i++) {
		trackedPositions[i] = malloc(numTrackedAgents * sizeof(coordinate_t));
	}

	int* trackedAgentIds = malloc(numTrackedAgents * sizeof(int));
	trackedAgentIds[0] = 1;
	trackedPositions[0][0] = agentPos;
	trackedAgentIds[1] = 2;
	trackedPositions[0][1] = blockingAgentPos;

	// Perform simulation
	for (int i = 1; i < timeSteps + 1; i++) {
		performMotilityEvents(lattice, rows, columns, motilityProbability, 
			xShiftPreference, yShiftPreference, agentExclusion, 
			trackedAgentIds, numTrackedAgents, trackedPositions[i]);
	}

	// Save tracked information
	bool isTracked = lattice_profile_parser(lattice, rows, columns, 
		"motile_mat.txt", "output/");
	if (isTracked) {
		printf("Stored lattice profile.\n");
	} else {
		printf("Error: failed to store lattice profile.\n");
	}

	bool isTrackedFirstAgent = tracked_agents_parser(trackedPositions, 
		timeSteps + 1, 0, "tracked_1.txt", "output/");
	bool isTrackedSecondAgent =  tracked_agents_parser(trackedPositions, 
		timeSteps + 1, 1, "tracked_2.txt", "output/");
	if (isTrackedFirstAgent && isTrackedSecondAgent) {
		printf("Stored tracking information.\n");
	} else {
		printf("Write Error: failed storing tracking information.\n");
	}

	// deallocate memory
	lattice_destroy(&lattice, rows, columns);
	free(agentId);
	free(blockingAgentId);
	free(trackedAgentIds);

	for (int i = 0; i < timeSteps + 1; i++) {
		free(trackedPositions[i]);
	}
	free(trackedPositions);

	return EXIT_SUCCESS;
}
Example #3
0
int cat_decode_word(search_t *search, const features_t *features, lattice_t *lattice,
               symbol_t *ref, reference_t ref_type, const char *lattice_tmpl)
{
  TRACE(1, "Decoding CAT word...\n");
  int prefix_len = 0;
  const int ref_len = symlen(ref);

  float beam = search->decoder->beam_pruning;

  search_create_emission_cache(search);

  symbol_t *prefix = (symbol_t *)malloc((ref_len+1)*sizeof(symbol_t));
  if (search->decoder->grammar->start != VOCAB_NONE) {
    prefix[prefix_len++] = search->decoder->grammar->start;
  }
  prefix[prefix_len] = VOCAB_NONE;

  if (ref_type == REF_SOURCE || ref_type == REF_TARGET) {
    for (; prefix_len < ref_len; prefix_len++) {
      {
        char *prefix_str = NULL;
        vocab_symbols_to_string(prefix, (ref_type == REF_SOURCE)?search->decoder->vocab->in:search->decoder->vocab->out, &prefix_str);
        TRACE(1, "next prefix: %s\n", prefix_str);
        free(prefix_str);
      }

      search_t *prefix_search = NULL;
      if (ref_type == REF_SOURCE) {
        prefix_search = search_create_from_prefix(search, prefix, NULL);
      }
      else if (ref_type == REF_TARGET) {
        prefix_search = search_create_from_prefix(search, NULL, prefix);
      }
      else {
        REQUIRE(ref_type > REF_NONE && ref_type < REF_MAX, "Invalid reference type\n");
      }
      search->decoder->beam_pruning = beam;

      CHECK(prefix_search->decoder->grammar->list_initial->num_elements > 0, "Empty prefix grammar. Possible lack of coverture\n");
      fprintf(stderr, "n initials = %d, n_states = %d\n", prefix_search->decoder->grammar->list_initial->num_elements, prefix_search->decoder->grammar->num_states);
      grammar_write_dot(prefix_search->decoder->grammar, stderr);
      lattice_t *prefix_lattice = lattice_create(lattice->nbest, lattice->nnode, prefix_search->decoder);
      //lattice_t *prefix_lattice = lattice_create(lattice->nbest, lattice->nnode, search->decoder);

      clock_t tim = clock();
      decode(prefix_search, features, prefix_lattice);
      //decode(search, features, prefix_lattice);
      clock_t tim2 = clock();
      TRACE(1, "iter %d tim %f\n", prefix_len, ((float) ((tim2 - tim) / CLOCKS_PER_SEC) / prefix_search->n_frames) / 0.01);

      //Calculate best hypothesis
      {
        symbol_t *best_ext_hyp = NULL;
        lattice_best_hyp(prefix_lattice, &best_ext_hyp);

        if (best_ext_hyp != NULL) {
          // write lattice
          {
            char path[MAX_LINE];      
            sprintf(path, lattice_tmpl, prefix_len);
            FILE *lattice_file = smart_fopen(path, "w");
            CHECK_SYS_ERROR(lattice_file != NULL, "Couldn't create word graph file '%s'\n", path);
            lattice_write(prefix_lattice, lattice_file, path);
            smart_fclose(lattice_file);
          }

          // add one word to the prefix
          prefix[prefix_len] = ref[prefix_len];
          prefix[prefix_len + 1] = VOCAB_NONE;

          {
            char *sentence_str = NULL;
            extended_vocab_symbols_to_string(best_ext_hyp, prefix_lattice->decoder->vocab, &sentence_str);
            TRACE(1, "%s\n", sentence_str);
            free(sentence_str);
          }
          free(best_ext_hyp);
        } else {
          TRACE(1, "Sentence not recognized. Increasing beam search\n");
          // add one word to the prefix
          //prefix[prefix_len] = ref[prefix_len];
          //prefix[prefix_len + 1] = VOCAB_NONE;

          //abort();
          prefix_len--;
          beam *= 2;
        }

      }


      lattice_delete(prefix_lattice);
      search_delete(prefix_search);

      fflush(stdout);

    }
  }

  free(prefix);

  return prefix_len;
}
Example #4
0
int cat_decode(search_t *search, const features_t *features, lattice_t *lattice,
               symbol_t *ref, reference_t ref_type)
{
  int iter = 0;
  bool is_different = true;
  const int ref_len = symlen(ref);

  search_create_emission_cache(search);

  symbol_t *prefix = NULL;


  if (ref_type == REF_SOURCE || ref_type == REF_TARGET) {
    do {
      search_t *prefix_search = NULL;
      if (ref_type == REF_SOURCE) {
        prefix_search = search_create_from_prefix(search, prefix, NULL);
      }
      else if (ref_type == REF_TARGET) {
        prefix_search = search_create_from_prefix(search, NULL, prefix);
      }
      else {
        REQUIRE(ref_type > REF_NONE && ref_type < REF_MAX, "Invalid reference type\n");
      }
      CHECK(prefix_search->decoder->grammar->list_initial->num_elements > 0, "Empty prefix grammar. Possible lack of coverture\n");
      fprintf(stderr, "n initials = %d, n_states = %d\n", prefix_search->decoder->grammar->list_initial->num_elements, prefix_search->decoder->grammar->num_states);
      //grammar_write_dot(prefix_search->decoder->grammar, stderr);
      lattice_t *prefix_lattice = lattice_create(lattice->nbest, lattice->nnode, prefix_search->decoder);

      clock_t tim = clock();
      decode(prefix_search, features, prefix_lattice);
      clock_t tim2 = clock();
      TRACE(1, "iter %d tim %fs\n", iter, ((float) ((tim2 - tim) / CLOCKS_PER_SEC) / prefix_search->n_frames) / 0.01);

      //Calculate best hypothesis
      {
        symbol_t *best_ext_hyp = NULL;
        lattice_best_hyp(prefix_lattice, &best_ext_hyp);

        if (best_ext_hyp != NULL) {
          // free previous prefix and compute new hypothesis
          free(prefix);
          prefix = NULL;

          symbol_t *best_in_hyp = NULL, *best_out_hyp = NULL;
          extended_vocab_separate_languages(prefix_lattice->decoder->vocab, best_ext_hyp, &best_in_hyp, &best_out_hyp);

          if (ref_type == REF_SOURCE) {
            prefix = symdup(best_in_hyp);
          }
          else if (ref_type == REF_TARGET) {
            prefix = symdup(best_out_hyp);
          }
          free(best_out_hyp);
          free(best_in_hyp);

          int prefix_len = sym_longest_prefix_match(ref, prefix);

          if (prefix_len == ref_len) {
            is_different = false;
          }
          else  {
            // add one word to the prefix
            prefix[prefix_len] = ref[prefix_len];
            prefix[prefix_len + 1] = VOCAB_NONE;
          }

          {
            char *prefix_str = NULL;
            vocab_symbols_to_string(prefix, (ref_type == REF_SOURCE)?prefix_lattice->decoder->vocab->in:prefix_lattice->decoder->vocab->out, &prefix_str);
            TRACE(1, "next prefix: %s\n", prefix_str);
            free(prefix_str);
          }
          char *sentence_str = NULL;
          extended_vocab_symbols_to_string(best_ext_hyp, prefix_lattice->decoder->vocab, &sentence_str);
          TRACE(1, "%s\n", sentence_str);
          free(sentence_str);
        } else {
          TRACE(1, "next prefix: NULL\n");
          TRACE(1, "Sentence not recognized. Increasing beam search\n");
          prefix_search->decoder->beam_pruning *= 2;
          iter--;
        }

        free(best_ext_hyp);
      }


      lattice_delete(prefix_lattice);
      search_delete(prefix_search);

      iter++;

      fflush(stdout);

    } while (is_different);
  }

  {
    search_t *prefix_search = NULL;
    if (ref_type == REF_SOURCE) {
      prefix_search = search_create_from_prefix(search, prefix, NULL);
      TRACE(1, "forced decoding...\n");
    }
    else if (ref_type == REF_TARGET) {
      prefix_search = search_create_from_prefix(search, NULL, prefix);
      TRACE(1, "forced decoding...\n");
    }
    else if (ref_type == REF_PREFIX) {
      prefix_search = search_create_conditioned_to_prefix(search, ref);
      TRACE(1, "suffix decoding...\n");
    }
    else {
      REQUIRE(ref_type > REF_NONE && ref_type < REF_MAX, "Invalid reference type\n");
    }
    CHECK(prefix_search->decoder->grammar->list_initial->num_elements > 0, "Empty prefix grammar. Possible lack of coverture\n");
    //grammar_write_dot(prefix_search->decoder->grammar, stderr);
    //lattice_t *prefix_lattice = lattice_create(lattice->nnode, lattice->nbest, prefix_search->decoder);

    clock_t tim = clock();
    decode(prefix_search, features, lattice);
    clock_t tim2 = clock();
    TRACE(1, "iter %d tim %f\n", iter, ((float) ((tim2 - tim) / CLOCKS_PER_SEC) / prefix_search->n_frames) / 0.01);
    search_delete(prefix_search);
  }

  free(prefix);

  return iter;
}
int main(int argc, char* argv[]) {

	// construct lattice
	unsigned int rows = 5;
	unsigned int columns = 5;
	lattice_t* lattice = lattice_create(rows, columns, 
		periodic, periodic, periodic, periodic);

	// populate lattice
	int* agentId = malloc(sizeof(int));
	*agentId = 1;

	coordinate_t agentPos = { .row = 2, .column = 2 };
	lattice_push_agent(lattice, agentPos, agentId);	

	// set motility properties
	double proliferationProbability = 1.0;
	proliferation_type_t proliferationType = ballistic;
	unsigned int proliferationDelta = 4;
	bool agentExclusion = true;

	// initialise agent tracking information
	unsigned int numTrackedAgents = 1;
	unsigned int timeSteps = 1;
	
	coordinate_t** trackedPositions = malloc((timeSteps + 1) * sizeof(coordinate_t*));
	for (int i = 0; i < timeSteps + 1; i++) {
		trackedPositions[i] = malloc(numTrackedAgents * sizeof(coordinate_t));
	}

	int* trackedAgentIds = malloc(numTrackedAgents * sizeof(int));
	trackedAgentIds[0] = 1;
	trackedPositions[0][0] = agentPos;

	// Perform simulation
	for (int i = 1; i < timeSteps + 1; i++) {
		performProliferationEvents(lattice, rows, columns, 
			proliferationProbability, agentExclusion, proliferationType, 
			proliferationDelta, trackedAgentIds, numTrackedAgents, 
			trackedPositions[i]);
	}

	// Save proliferation information
	bool isTracked = lattice_profile_parser(lattice, rows, columns, 
		"prolif_mat.txt", "output/");
	if (isTracked) {
		printf("Stored lattice profile.\n");
	} else {
		printf("Error: failed to store lattice profile.\n");
	}

	bool agentIsTracked = tracked_agents_parser(trackedPositions, timeSteps + 1,
		0, "tracked_agent.txt", "output/");
	if (agentIsTracked) {
		printf("Stored agent tracking information.\n");
	} else {
		printf("Error: failed to store agent tracking information.\n");
	}

	// deallocate memory
	lattice_destroy(&lattice, rows, columns);
	free(agentId);
	// free(blockingAgentId);
	free(trackedAgentIds);

	for (int i = 0; i < timeSteps; i++) {
		free(trackedPositions[i]);
	}
	free(trackedPositions);

	return EXIT_SUCCESS;
}
int main(int argc, char* argv[]) {

	// construct lattice
	unsigned int rows = 1;
	unsigned int columns = 300;
	lattice_t* lattice = lattice_create(rows, columns, 
		periodic, periodic, periodic, periodic);

	// initialise lattice positioning
	unsigned int const kNumStdDevs = 5;
	unsigned int const kStdDevsRepeatCount = 1;
	unsigned int const kRepeatCount = 1000;
	double stddevs[] = { 0.1, 0.2, 0.3, 0.4, 0.5 };

	unsigned int const kTimeSetsNum = 3;
	unsigned int timeSets[] = { 200, 500, 1000 };

	// initialise random number storage
	VSLStreamStatePtr stream;
	float randomNumbers[columns];

	// initialise temporary node storage
	double xPosition;
	double yPosition = 0.0;
	coordinate_t coord;

	// initialise loop variables
	bool trackedLatticeLayout;
	char latticeLayoutFileName[50];
	char latticeProfileFilename[100];

	// initialise agent tracking information
	unsigned int numTrackedAgents = 0;
	coordinate_t* trackedPositions = NULL;
	int* trackedAgentIds = NULL;

	// set motility properties
	double motilityProbability = 1.0;
	double xShiftPreference = 0;
	double yShiftPreference = 0;
	bool agentExclusion = false;

	// generation random lattices
	for (int stdDevIndex = 0; stdDevIndex < kNumStdDevs; stdDevIndex++) {
		
		for (int boundRepeatCount = 0; 
			boundRepeatCount < kStdDevsRepeatCount; boundRepeatCount++) {

			// generate any required random numbers (uniform dist)
			vslNewStream(&stream, BRNG, arc4random());	
			vsRngGaussian(VSL_RNG_METHOD_GAUSSIAN_BOXMULLER, stream, 
				columns, randomNumbers, 0.0f, stddevs[stdDevIndex]);

			// perturb and sort node locations
			for (int col = 0; col < columns; col++) {
				randomNumbers[col] += col;
			}
			qsort(randomNumbers, columns, sizeof(float), compare);

			// specify node locations
			for (int row = 0; row < rows; row++) {
				for (int col = 0; col < columns; col++) {
					coord.row = row;
					coord.column = col;
					xPosition = (double)randomNumbers[col];
					lattice_specify_position(lattice, coord, xPosition, yPosition);
				}
			}

			// save node locations
			bool saveNodeLocations = true;
			if (saveNodeLocations) {
				sprintf(latticeLayoutFileName, "node_positions_%0.02f_%d_ghosts.txt", 
					stddevs[stdDevIndex], boundRepeatCount);
				trackedLatticeLayout = lattice_parser_node_positions(lattice, rows, 
					columns, latticeLayoutFileName, "output/");
				if (!trackedLatticeLayout) {
					printf("Error: failed storing lattice layout information (case: %0.02f %d).\n", 
						stddevs[stdDevIndex], boundRepeatCount);
				}
			}

			// perform simulations
			bool performSimulation = true;
			if (performSimulation) {

				// perform simulations
				for (int repeatCount = 0; repeatCount < kRepeatCount; repeatCount++) {

					// populate lattice
					int* agentId;
					int currentAgentId = 1;
					coordinate_t agentPos;

					for (int j = 130; j < 171; j++) {
						agentId = malloc(sizeof(int));
						*agentId = currentAgentId++;

						agentPos.row = 0;
						agentPos.column = j;

						lattice_push_agent(lattice, agentPos, agentId);
					}

					// perform simulation
					for (int timeStep = 0; timeStep < timeSets[kTimeSetsNum-1]; 
							timeStep++) {
						performMotilityEvents(lattice, rows, columns, motilityProbability, 
							xShiftPreference, yShiftPreference, agentExclusion, 
							trackedAgentIds, numTrackedAgents, trackedPositions);

						for (int j = 0; j < kTimeSetsNum; j++) {
							if (timeStep == timeSets[j]-1) {
								// store lattice profile
								sprintf(latticeProfileFilename, "lattice_profile_%0.02f_%d_%d_%d_ghosts.txt", 
									stddevs[stdDevIndex], boundRepeatCount, repeatCount, timeStep+1);
								bool isTracked = lattice_occupancy_parser(lattice, rows, columns, 
									latticeProfileFilename, "output/");
								if (!isTracked) {
									printf("Error: failed to store lattice profile.\n");
								}
							}
						}
					}

					// clear lattice and deallocate memory
					lattice_clear(lattice, rows, columns, true);
				}
			}
		}
	}

	// deallocate memory
	lattice_destroy(&lattice, rows, columns, true);

	return EXIT_SUCCESS;
}