Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void ControlledAnnealing(Item* items,
- unsigned int &globalMaximumValue,
- unsigned int &globalMaximum,
- unsigned int &iterationMaximum,
- unsigned int numberOfSteps)
- {
- unsigned int weight, value, newWeight, newValue, T = numberOfSteps - 30;
- unsigned int startingPoint = distribution(rd);
- double kb = 0.3;
- unsigned int newPoint = 0;
- int changedBit = 0;
- for(unsigned int i = 0; i < numberOfSteps; i++)
- {
- weight = 0; value = 0; newWeight = 0; newValue = 0;
- changedBit = distribution(rd) % ELEMENTS_NUMBER;
- newPoint = CHANGE_BIT(startingPoint, changedBit);
- for(unsigned int j = 0; j < ELEMENTS_NUMBER; j++)
- {
- if(CHECK_BIT(startingPoint, j))
- {
- weight += items[j].Weight;
- value += items[j].Value;
- }
- if(CHECK_BIT(newPoint, j))
- {
- newWeight += items[j].Weight;
- newValue += items[j].Value;
- }
- }
- if(newValue > value && newWeight <= MAX_KNAPSACK_WEIGHT)
- {
- globalMaximum = startingPoint = newPoint;
- globalMaximumValue = newValue;
- iterationMaximum = i;
- }
- else
- {
- if(weight > MAX_KNAPSACK_WEIGHT && newWeight < weight)
- {
- startingPoint = newPoint;
- if(newWeight <= MAX_KNAPSACK_WEIGHT)
- {
- globalMaximum = startingPoint;
- globalMaximumValue = newValue;
- iterationMaximum = i;
- }
- }
- else
- {
- double p1 = -(fabs((double)weight - newWeight));
- double p = exp(p1/(T*kb));
- if(p > (1.0 * distribution(rd) / (RAND_MAX - 1)))
- {
- startingPoint = newPoint;
- }
- }
- }
- if(T > 0) T--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement