main(){ /*getHammingDistance() test cases:*/ getHammingDistance("AACCTT","GGCCTT"); //return 2 getHammingDistance("TCGGA","AAAAG"); //returns 5 getHammingDistance("A","AG"); //error message prompt returns 0 /*countSubstrPattern() test cases:*/ countSubstrPattern("AATATATAGG","GG");// returns 1 countSubstrPattern("AATATATAGG","ATA");// returns 3 countSubstrPattern("AATATATAGG","ACTGACTGACTG");// returns 0 /*isValidString() test cases:*/ isValidString("AAGGCTATGC","ACGT");//returns 1 / true isValidString("AAGGCTATGa","ACGT");// returns 0 / false isValidString("ACGT","ACGT");// returns 1 / true isValidString("ACGT101_","ACGT");// returns 0 / false isValidString("091212345","0123456789");// returns 1 / true /*getSkew() test cases:*/ getSkew("GGCCAC", 1);// returns 1 getSkew("GGCCAC", 2);// returns 2 getSkew("GGCCAC", 3);// returns 1 getSkew("GGCCAC", 4);// returns 0 getSkew("GGCCAC", 5);// returns 0 /*getMaxSkewN() test cases:*/ getMaxSkewN("GGCCAC", 1);// returns 1 getMaxSkewN("GGCCAC", 2);// returns 2 getMaxSkewN("GGCCAC", 3);// returns 2 getMaxSkewN("GGCCAC", 4);// returns 2 getMaxSkewN("GGCCAC", 5);// returns 2 /*getMinSkewN() test cases:*/ getMinSkewN("GGCCAC", 1);// returns 1 getMinSkewN("GGCCAC", 2);// returns 1 getMinSkewN("GGCCAC", 3);// returns 1 getMinSkewN("GGCCAC", 4);// returns 0 getMinSkewN("GGCCAC", 5);// returns 0 }
/* Get all the number G whose binary representation contains N digits such that * the Hamming distance between G and base is equal to hammingDist */ int getNumHamming(int N, int hammingDist, int base, int result[]) { int numResult = 0, upperBound = (1 << N), num; for(num = BASE_NUM; num < upperBound; num++) if(getHammingDistance(num, base) == hammingDist) { result[numResult] = num; numResult++; } return numResult; }
void getHammingDistanceTest(char *str1, char *str2, int expectedResult) { int hammingDistance = getHammingDistance(str1, str2); printf("getHammingDistance(\"%s\", \"%s\") ", str1, str2); if(hammingDistance == expectedResult) { printf("PASSED\n"); } else { printf("FAILED (Expected %i, got %i.)\n", expectedResult, hammingDistance); errorCount++; } }
int main(){ int choice = 7, result, n; char *str1, *str2; while(choice != 0){ //while user has not chosen to exit the program printMenu(&choice); //calls function printMenu str1 = (char *)malloc(100*sizeof(char)); str2 = (char *)malloc(100*sizeof(char)); switch(choice){ case 1: printf("\n*****************************************\n"); printf("*\t Get Hamming Distance\t\t*\n"); printf("*****************************************\n"); printf("\nEnter string 1: "); scanf("%s", str1); printf("Enter string 2: "); scanf("%s", str2); //return value of function getHammingDistance stored in result result = getHammingDistance(str1, str2); if(result == 0){ //if the strings have unequal length printf("\nError! Strings have unequal length!\n"); }else{ //if the strings have equal length printf("\nHamming Distance: %d\n", result); } break; case 2: printf("\n*****************************************\n"); printf("*\t Count Substring Pattern\t*\n"); printf("*****************************************\n"); printf("\nEnter original string: "); scanf("%s", str1); printf("Enter pattern: "); scanf("%s", str2); //return value of function countSubstrPattern stored in result result = countSubstrPattern(str1, str2); printf("\nNumber of occurence of pattern in orginal: %d\n", result); break; case 3: printf("\n*****************************************\n"); printf("*\t Is String Valid\t\t*\n"); printf("*****************************************\n"); printf("\nEnter alphabet: "); scanf("%s", str1); printf("Enter string: "); scanf("%s", str2); //return value of function isValidString stored in result result = isValidString(str2, str1); if(result == 1){ printf("\nString is valid.\n"); }else{ printf("\nString is not valid.\n"); } break; case 4: printf("\n*****************************************\n"); printf("*\t\tGet Skew\t\t*\n"); printf("*****************************************\n"); printf("\nEnter genome string: "); scanf("%s", str1); printf("Enter number: "); scanf("%d", &n); if(n > (strlen(str1)+1) || n <= 0){ printf("\nError! Number must be within string length!\n"); }else{ //return value of function getSkew stored in result result = getSkew(str1, n); printf("\nNumber of G-C: %d\n", result); } break; case 5: printf("\n*****************************************\n"); printf("*\t Get Maximum Skew of N\t\t*\n"); printf("*****************************************\n"); printf("\nEnter genome string: "); scanf("%s", str1); printf("Enter number: "); scanf("%d", &n); if(n > (strlen(str1)+1) || n <= 0){ printf("\nError! Number must be within string length!\n"); }else{ //return value of function getMaxSkewN stored in result result = getMaxSkewN(str1, n); printf("\nMaximum Skew: %d\n", result); } break; case 6: printf("\n*****************************************\n"); printf("*\t Get Minimum Skew of N\t\t*\n"); printf("*****************************************\n"); printf("\nEnter genome string: "); scanf("%s", str1); printf("Enter number: "); scanf("%d", &n); if(n > (strlen(str1)+1) || n <= 0){ printf("\nError! Number must be within string length!\n"); }else{ //return value of function getMinSkewN stored in result result = getMinSkewN(str1, n); printf("\nMinimum Skew: %d\n", result); } break; case 0: free(str1); free(str2); printf("\nProgram Terminated...\n"); break; default: printf("\nInvalid input!\n"); break; } } }
void BusinessCycle::cycle() { /* firms create If a firm realizes there is no product within a suitable modicum of acceptance, they may invest a certain % of their capital to create a new firm The mother firm may take starting individuals from the unemployment pool to seed the child firm’s ranks */ for(int i = 0; i < firmindex.size(); i++) { //this firm Firm* firm = firmindex.at(i); double productivity = firm->getproductivity(); firm->timeUntraded++; //get a list of employees with which to work std::vector<Individual*> empls = firm->getemployees(); //hire fire threshold double hft = (double)config->get_hire_fire_threshold()/100.0; //hire fire rate int hfr = (int)((double)empls.size() * hft); if(hfr = 0) hfr = 2; //best product for this firm Firm* hasBestProduct = firmindex.at(0); while(hasBestProduct->id == firm->id) hasBestProduct = firmindex.at(getRandomInt(0,firmindex.size())); //how close the product is to what is required double bestDist = 1.0; //find the best product for this firm for(int j = 0; j < firmindex.size(); j++) { if(firmindex.at(j)->unitsLeft > 0 && firmindex.at(j)->id != firm->id) { gType tempProd = firmindex.at(j)->companyProduct; double dist = (double)getHammingDistance(tempProd, firm->rawProduct)/(double)tempProd.size(); if(dist < bestDist) { bestDist = dist; hasBestProduct = firmindex.at(j); } } } double q = (double)(100 - config->get_modicum_of_acceptance())/100.0; if(config->get_avg_starting_capital()/2 < firm->capital && bestDist < q) { double starting = firm->capital / 2; firm->capital -= starting; gType prod = firm->rawProduct; std::vector<Individual*> startingPpl; Individual* tempEmp; for(int c = 0; c < unemployed.size(); c++) { if((double)unemployed.at(c)->getproductivity(prod) > 1.0-hft/2) { tempEmp = unemployed.at(c); startingPpl.push_back(tempEmp); unemployed.erase(unemployed.begin() + c); } else c++; } Firm* newFirm = new Firm(0, config->get_start_individuals()); newFirm->unitsLeft = 100; newFirm->companyProduct = prod; newFirm->capital = starting; firmindex.push_back(newFirm); } //how much does the product cost?: (quality of raw) firm->productCost = 1.0;//-bestDist; //while there is product left and the firm has enough money to purchase it int raw = 0; //std::cout << "\n---Firm "<<firm->id << "---\n" <<"\nRaw units available: " << hasBestProduct->unitsLeft << "\n"; double before = firm->capital; while(hasBestProduct->unitsLeft > 0 && firm->capital >= hasBestProduct->productCost) { //make it so hasBestProduct->unitsLeft--; hasBestProduct->capital += hasBestProduct->productCost; firm->capital -= hasBestProduct->productCost; raw++; } firm->buysFrom = hasBestProduct; double after = firm->capital; //std::cout << "Raw units acquired: " << raw << " from Firm: "<< hasBestProduct->id <<"\n"; //how much product is produced?: (int)(productivity)(qty of raw purchased)*(#employees) firm->unitsLeft += (int)(((double)productivity+.5)*(double)raw); //std::cout << "Firm " << firm->id << " created " << (int)((double)productivity*(double)raw) << " units.\n"; //std::cout << "Firm " << firm->id << " has " << firm->unitsLeft << " units that will sell for " << firm->productCost <<" each.\n"; //std::cout << "Our cost was: " << getDelta(before,after) << ". \n"; hasBestProduct->timeUntraded = 0; //money += qtypurchased*(quality of raw) //individuals mentor //declare the product for each employee so we can sort based on productivity firm->employeeProductUpdate(); std::vector<Individual*> mentors; // get the number of people who will be mentoring int numMentors = (int)(empls.size()*(double)config->get_modicum_of_acceptance()/100.0); //std::cout << firmindex.at(i)->getemployees().size()<<"\n"; firm->sortEmployees(); //get the mentors for(int j = 0; j < numMentors; j++) { //a certain percentage of individuals within the firm whose skill sets match the product type most closely //must mentor new individuals who start at an initial age if(!empls.at(j)->isRetired()) { mentors.push_back(empls.at(j)); } } // mentor new employee for each mentor pair for(int j = 1; j < mentors.size(); j++) { Individual* x; //They may do this via random skill (bit) selection or via crossover if(getRandomInt(0,2)) x = new Individual(mentors.at(j)->skillSet, mentors.at(j-1)->skillSet, false); else { x = new Individual(mentors.at(j)->skillSet, mentors.at(j-1)->skillSet, true); } //"retrain" the least effective employee empls.at(empls.size()-1)->skillSet = mutate(empls.at(empls.size()-1)->skillSet); firm->employees.push_back(x); all_the_peoples.push_back(x); } Individual* tempEmp; int numFired = 0; //firms may fire individuals for(int c = 0; c < empls.size();) { double empProductivity = empls.at(c)->getproductivity(firm->companyProduct); if(getDelta(empProductivity,productivity) > hft) { tempEmp = empls.at(c); empls.erase(empls.begin()+c); unemployed.push_back(tempEmp); numFired++; } else c++; } // firms may hire individuals numFired = getRandomInt(0, numFired+1); for(int c = 0; c < unemployed.size() && numFired >0;) { double empProductivity = empls.at(c)->getproductivity(firm->companyProduct); if(getDelta(empProductivity,productivity) <= hft) { tempEmp = unemployed.at(c); unemployed.erase(unemployed.begin()+c); empls.push_back(tempEmp); numFired--; } else c++; } //if they are retired, turn them into soylent green. for(int c = 0; c < empls.size();) { if(empls.at(c)->isRetired()) { for(int y = 0; y < all_the_peoples.size(); y++) { if(empls.at(c) == all_the_peoples.at(y)) { all_the_peoples.erase(all_the_peoples.begin()+y); break; } } empls.erase(empls.begin()+c); } else c++; } if(empls.size() ==0 || firm->timeUntraded > config->get_inactivity_rate()) { // lay everyone off if there is anyone there while(!empls.empty()) { Individual* temp = empls.back(); empls.pop_back(); unemployed.push_back(temp); } // remove the firm forever. firmindex.erase(firmindex.begin()+i); i--; } } //get all employees for(int j = 0; j < all_the_peoples.size(); j++) { //iterate their ages all_the_peoples.at(j)->age++; } for(int j = 0; j < unemployed.size(); j++) { //iterate their ages unemployed.at(j)->timeUnemployed++; } //if they have given up, turn them into soylent green. for(int c = 0; c < unemployed.size();) { if(unemployed.at(c)->timeUnemployed > config->get_inactivity_rate()) { for(int y = 0; y < all_the_peoples.size(); y++) { if(unemployed.at(c) == all_the_peoples.at(y)) { all_the_peoples.erase(all_the_peoples.begin()+y); break; } } unemployed.erase(unemployed.begin()+c); } else c++; } }
main(){ int x; printf("getHammingDistance\n"); x = getHammingDistance("AACCTT","GGCCTT"); printf("%d\n",x ); x = getHammingDistance("TCGGA","AAAAG"); printf("%d\n",x ); x = getHammingDistance("A","AG"); printf("%d\n",x ); printf("countSubstrPattern\n"); x = countSubstrPattern("AATATATAGG","GG"); printf("%d\n",x); x = countSubstrPattern("AATATATAGG","ATA"); printf("%d\n",x); x = countSubstrPattern("AATATATAGG","ACTGACTGACTG"); printf("%d\n",x); printf("isValidString\n"); x = isValidString("AAGGCTATGC","ACGT"); printf("%d\n",x); x = isValidString("AAGGCTATGa","ACGT"); printf("%d\n",x); x = isValidString("ACGT","ACGT"); printf("%d\n",x); x = isValidString("ACGT101_","ACGT"); printf("%d\n",x); x = isValidString("091212345","0123456789"); printf("%d\n",x); printf("getSkew\n"); x = getSkew("GGCCAC", 1); printf("%d\n",x ); x = getSkew("GGCCAC", 2); printf("%d\n",x ); x = getSkew("GGCCAC", 3); printf("%d\n",x ); x = getSkew("GGCCAC", 4); printf("%d\n",x ); x = getSkew("GGCCAC", 5); printf("%d\n",x ); printf("getMaxSkewN\n"); x = getMaxSkewN("GGCCAC", 1); printf("%d\n",x ); x = getMaxSkewN("GGCCAC", 2); printf("%d\n",x ); x = getMaxSkewN("GGCCAC", 3); printf("%d\n",x ); x = getMaxSkewN("GGCCAC", 4); printf("%d\n",x ); x = getMaxSkewN("GGCCAC", 5); printf("%d\n",x ); printf("getMinSkewN\n"); x = getMinSkewN("GGCCAC", 1); printf("%d\n",x ); x = getMinSkewN("GGCCAC", 2); printf("%d\n",x ); x = getMinSkewN("GGCCAC", 3); printf("%d\n",x ); x = getMinSkewN("GGCCAC", 4); printf("%d\n",x ); x = getMinSkewN("GGCCAC", 5); printf("%d\n",x ); }