예제 #1
0
  void normalizeRM(matrix_t & Q, StateMap const & staMap, float subs)
  {
    vector_t equiFreq = deriveEquiFreqForReversibleRM(Q);
    HammingDistance hammingDistance(staMap);
    number_t normConst = 0;
    for (unsigned i = 0; i < Q.size1(); ++ i)
      for (unsigned j = 0; j < Q.size2(); ++ j)
	normConst += equiFreq(i) * Q(i,j) * hammingDistance(i,j);

    Q = Q * (subs / normConst);
  }
예제 #2
0
int main(int argc, char const *argv[]) {
    // int a = 4;
    // while (a) {
    //     printf("%d\n", a);
    //     a = a >> 1;
    // }
    printf("result is:%d\n", hammingDistance(1, 4));
    printf("result is:%d\n", hammingDistance(33, 33));
    printf("result is:%d\n", hammingDistance(0, 1));
    return 0;
}
예제 #3
0
std::vector<PerceptualHash::ComparisonResult> PerceptualHash::nbest(int n, const ulong64& hash, const std::vector<ulong64>& dataSet) {
    if (n == 1) {
        std::vector<PerceptualHash::ComparisonResult> result;
        result.push_back(best(hash, dataSet));
        return result;
    }

    std::vector<int> nbestDistances(n, sizeof(ulong64) * 8);
    std::vector<size_t> nbestObjectIndexes(n, -1);

    for (size_t i = 0; i < dataSet.size(); i++) {
        const int dist = hammingDistance(hash, dataSet[i]);
        for (size_t j = 0; j < nbestDistances.size(); j++) {
            if (dist < nbestDistances[j]) {
                for (size_t k = n - 1; k > j; k--) {
                    nbestDistances[k] = nbestDistances[k - 1];
                    nbestObjectIndexes[k] = nbestObjectIndexes[k - 1];
                }
                nbestDistances[j] = dist;
                nbestObjectIndexes[j] = i;
                break;
            }
        }
    }

    std::vector<ComparisonResult> nbestObjects(n);
    for (size_t i = 0; i < n; i++) {
        ComparisonResult c;
        c.distance = nbestDistances[i];
        c.index = nbestObjectIndexes[i];
        nbestObjects[i] = c;
    }

    return nbestObjects;
}
예제 #4
0
RecoveredClasses AssociativeMemory::recovery(const MemoryMatrix& m) const {
  RecoveredClasses results;
  if (classes > 0) {
    vector<int> lut(classes, -1);
    unsigned int distance;
    for (FundamentalSet::iterator it = CF->begin(); it != CF->end(); ++it) {
        OrderedCouple* oc = *it;
        distance = hammingDistance(m, *(oc->y));
        if (oc->classname < classes) {
          if (lut[oc->classname] == -1)
            lut[oc->classname] = distance;
          else
            lut[oc->classname] += distance;
        }
    }
    results.push_front(0);
    for (unsigned int i = 1; i < classes; i++) {
      if (lut[i] != -1) {
        if (lut[results.front()] == -1 || lut[i] < lut[results.front()]) {
          results.clear();
          results.push_front(i);
        } else {
          if (lut[i] == lut[results.front()])
            results.push_back(i);
        }
      }
    }
    if (results.front() == 0 && lut[0] == -1)
      results.clear();
  }
  return results;
}
int main(){
    int choice;
    
    do{
        choice = interfaceMenu();
        
        switch(choice){
            case 1:
                hammingDistance();
                break;
            case 2:
                subPattern();
                break;
            case 3:
                validString();
                break;
            case 4:
                gcSkew();
                break;
            case 5:
                maxSkew();
                break;
            case 6:
                minSkew();
                break;
            case 0:
                return 0;
            default:
                printf("\n Wrong input!");
        }
    }while(choice!= 0);
}
예제 #6
0
double computeOccurencies(DNAseq setSeq, DNAseq seq) {
	double freq = 0;
	for (int j = 0; j < ((setSeq.length() - seq.length()) + 1); j++) {
		int dist = hammingDistance(seq.seq, setSeq.seq.substr(j, seq.length()));
		if (dist == 0) {
			freq++;
		}
	}
	return freq;

}
예제 #7
0
int main(int argc, _TCHAR* argv[])
{
	int A[] = {0, 1, 2 , 3, 4, 5};
	int ret = hammingSum(A, sizeof(A)/sizeof(int));
	int sum = 0, n = sizeof(A)/sizeof(int);
	for (int i = 0; i < n-1; i++) {
		for (int j = i+1; j < n; j++) {
			sum += hammingDistance(A[i], A[j]);
		}
	}
	return 0;
}
예제 #8
0
PerceptualHash::ComparisonResult PerceptualHash::best(const ulong64& hash, const std::vector<ulong64>& dataSet) {
    size_t minIndex = -1;
    int minDist = sizeof(ulong64) * 8;

    for (size_t i = 0; i < dataSet.size(); i++) {
        const int dist = hammingDistance(hash, dataSet[i]);
        if (dist < minDist) {
            minDist = dist;
            minIndex = i;
        }
    }

    ComparisonResult c;
    c.distance = minDist;
    c.index = minIndex;

    return c;
}
예제 #9
0
 unsigned HammingDistance::operator()(symbol_t s, symbol_t t)
 {
   return hammingDistance(s, t);  // from utils.h
 }
예제 #10
0
 unsigned HammingDistance::operator()(state_t i, state_t j)
 {
   return hammingDistance(staMap_.state2Symbol(i), staMap_.state2Symbol(j));
 }
예제 #11
0
bool Schedule :: isEqualByHamming(PSchedule schedule, int hammingDispersion)
{
    return hammingDistance(schedule, hammingDispersion) == 0;
}
예제 #12
0
/** void setupDecoder(void)
/  Uses the current Convolutional Encoder to generate
/  the appropriate tables for an efficient Viterbi decoder
/  Should be called in main() before interrupt initialized
*/
void setupDecoder() {

	setPuncturing(FALSE);

	int i;
	int j;
	bv_t a = malloc (sizeof(struct bitvec));
	bv_new(a, 1);
	bv_t b = malloc (sizeof(struct bitvec));
	bv_new(b, 1);

	//*** this finds the properties of each state
	//generate the trellis (messages produced by state transitions)
	//generate decoding given two states
	for(i = 0; i < NUM_STATES; i++) {
		  for(j = 0; j < NUM_STATES; j++) {
			Trellis[i][j] = -1;
			Decode[i][j] = -1;
			if(j < 2)
				InverseTransitions[i][j] = -1;
		  }
	}
	// Trellis[0][0] = Trellis[1][2] = 0;
	// Trellis[1][0] = Trellis[0][2] = 3;
	// Trellis[2][1] = Trellis[3][3] = 1;
	// Trellis[3][1] = Trellis[2][3] = 2;

	for(i = 0; i < NUM_STATES; i++) {
		clear_vec(a);
		clear_vec(b);

		//get message and next state for input 0
		setState(i);
		encode(a,0);
		Transitions[i][0] = getState();
		if(InverseTransitions[getState()][0] == -1)
			InverseTransitions[getState()][0] = i;
		else
			InverseTransitions[getState()][1] = i;
		Trellis[i][getState()] = get(a,0,1);
		Decode[i][getState()] = 0;

		//get message and next state for input 1
		setState(i);
		encode(b,1);
		Transitions[i][1] = getState();
		if(InverseTransitions[getState()][0] == -1)
			InverseTransitions[getState()][0] = i;
		else
			InverseTransitions[getState()][1] = i;
		Trellis[i][getState()] = get(b,0,1);
		Decode[i][getState()] = 1;

		//initialize the decoded survivor paths
		paths[i] = malloc(sizeof(struct bitvec));
		bv_new(paths[i], 32);
		paths_next[i] = malloc (sizeof(struct bitvec));
		bv_new(paths_next[i], 32);

	}

	// for(i = 0; i < NUM_CODES; i++) {
	// 	  for(j = 0; j < 2; j++) {
	// 		printf("%d: %d -> %d \n", Decode[i][Transitions[i][j]], i, Transitions[i][j]);
	// 	  }
	// }
	// for(i = 0; i < NUM_CODES; i++) {
	// 	  for(j = 0; j < 2; j++) {
	// 		  printf("%d: %d -> %d \n", Decode[InverseTransitions[i][j]][i], InverseTransitions[i][j], i);
	// 	  }
	// }
	for(i = 0; i < NUM_CODES; i++) {
		for(j = 0; j < NUM_CODES; j++) {
		  printf("%d ", Trellis[i][j]);
		}

		printf("\n");
	}

	printf("Hamming distance \n");
	//pre-generate hamming distances for all messages
	for(i = 0; i < NUM_CODES; i++) {
		  load(a,i);
		  for(j = 0; j < NUM_CODES; j++) {
		    load(b,j);
		    HammingDistance[i][j] = hammingDistance(a,b);
		    printf("%d ", HammingDistance[i][j]);
		  }
		  printf("\n");
	}
	
	//reset the encoder and decoder
	clearState();
	vit_dec_reset();

	bv_free(a);
	bv_free(b);
	free(a);
	free(b);

	//set puncturing (only accomodates 2/3 puncture)
	setPuncturing(puncturedRec);
}
예제 #13
0
int main()
{
    printf("%d\n", hammingDistance(1, 4));
    return 0;
}
예제 #14
0
SolutionSet *MOCHC::execute() {
	
  int populationSize;
  int iterations;
  int maxEvaluations;
  int convergenceValue;
  int minimumDistance;
  int evaluations;

  double preservedPopulation;
  double initialConvergenceCount;
  bool condition = false;
  SolutionSet *solutionSet, *offSpringPopulation, *newPopulation; 

  Comparator * crowdingComparator = new CrowdingComparator();

  SolutionSet * population;
  SolutionSet * offspringPopulation;
  SolutionSet * unionSolution;

  Operator * cataclysmicMutation;
  Operator * crossover;
  Operator * parentSelection;


  //Read the parameters
  populationSize = *(int *) getInputParameter("populationSize");
  maxEvaluations = *(int *) getInputParameter("maxEvaluations");
  convergenceValue = *(int *) getInputParameter("convergenceValue");
  initialConvergenceCount = *(double *)getInputParameter("initialConvergenceCount");
  preservedPopulation = *(double *)getInputParameter("preservedPopulation");
  

  //Read the operators
  cataclysmicMutation = operators_["mutation"];
  crossover	      = operators_["crossover"];
  parentSelection     = operators_["parentSelection"];
  
  iterations  = 0;
  evaluations = 0;

  // calculating the maximum problem sizes .... 
  Solution * sol = new Solution(problem_);
  int size = 0;
  for (int var = 0; var < problem_->getNumberOfVariables(); var++) {
	Binary *binaryVar;
        binaryVar  = (Binary *)sol->getDecisionVariables()[var];
	size += binaryVar->getNumberOfBits();
  } 
  minimumDistance = (int) std::floor(initialConvergenceCount*size);

  // Create the initial solutionSet
  Solution * newSolution;
  population = new SolutionSet(populationSize);
  for (int i = 0; i < populationSize; i++) {
    newSolution = new Solution(problem_);
    problem_->evaluate(newSolution);
    problem_->evaluateConstraints(newSolution);
    evaluations++;
    population->add(newSolution);
  } //for


  while (!condition) {
	offSpringPopulation = new SolutionSet(populationSize);
 	Solution **parents = new Solution*[2];
	
	for (int i = 0; i < population->size()/2; i++) {
  		parents[0] = (Solution *) (parentSelection->execute(population));
		parents[1] = (Solution *) (parentSelection->execute(population));

		if (hammingDistance(*parents[0],*parents[1])>= minimumDistance) {
		   Solution ** offSpring = (Solution **) (crossover->execute(parents));
		   problem_->evaluate(offSpring[0]);
		   problem_->evaluateConstraints(offSpring[0]);
	           problem_->evaluate(offSpring[1]);
		   problem_->evaluateConstraints(offSpring[1]);
		   evaluations+=2;
		   offSpringPopulation->add(offSpring[0]);
		   offSpringPopulation->add(offSpring[1]);
		}		
	}  
	SolutionSet *join = population->join(offSpringPopulation);
 	delete offSpringPopulation;

	newPopulation = rankingAndCrowdingSelection(join,populationSize);
	delete join;
        if (equals(*population,*newPopulation)) {
		minimumDistance--;
	}   

	if (minimumDistance <= -convergenceValue) {
		minimumDistance = (int) (1.0/size * (1-1.0/size) * size);
		int preserve = (int) std::floor(preservedPopulation*populationSize);
		newPopulation->clear(); //do the new in c++ really hurts me(juanjo)
		population->sort(crowdingComparator);
		for (int i = 0; i < preserve; i++) {
			newPopulation->add(new Solution(population->get(i)));
		}
		for (int i = preserve; i < populationSize; i++) {
			Solution * solution = new Solution(population->get(i));
			cataclysmicMutation->execute(solution);
			problem_->evaluate(solution);
			problem_->evaluateConstraints(solution);	
			newPopulation->add(solution);
		}
		
	}

	iterations++;
	delete population;
	population = newPopulation;
	if (evaluations >= maxEvaluations) {
		condition = true;		
	}
  }

  return population;

}
예제 #15
0
int main(void)
{
    printf("%d", hammingDistance(1, 4));

    return 0;
}