import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.common.math.IntMath;
public class WeightedRRImpl {
public static void main(String[] args) {
WeightedRRImpl wrr = new WeightedRRImpl();
Map<Integer, Integer> instanceMap = new HashMap<Integer, Integer>();
instanceMap.put(0, 20);
instanceMap.put(1, 28);
instanceMap.put(2, 21);
/*instanceMap.put(3, 4);
instanceMap.put(4, 25);
instanceMap.put(5, 0);*/
/*
* instanceMap.put(3, 4); instanceMap.put(4, 25);
*/
/*
* instanceMap.put(0, 9); instanceMap.put(1, 10); instanceMap.put(2, 8);
* instanceMap.put(3, 7); instanceMap.put(4, 6);
*/
// int gcd = 1;
List<Integer> instanceList = new ArrayList<Integer>(instanceMap.values());
int gcd = wrr.getGCD(instanceList);
System.out.println("**** gcd " + gcd);
/*
* for (int i=0;i<instanceList.size()-1;i++) { BigInteger bigInt = new
* BigInteger(instanceList.get(i).toString()); gcd = bigInt.gcd(new
* BigInteger(instanceList.get(i+1).toString())).intValue(); }
*/
Collections.sort(instanceList);
int max = instanceList.get(instanceList.size() - 1);
int previous = -1;
for (int i = 1; i <= 3; i++) {
// System.out.println("previous "+previous);
Map.Entry<Integer, Integer> entry = wrr.scheduleNext(instanceMap, gcd, max, previous);
if (entry != null) {
previous = entry.getKey();
System.out.println("selected instance:" + previous + " weight:" + entry.getValue());
} else {
System.out.println("Entry is null");
}
}
/*
* System.out.println(gcd); System.out.println(wrr.getGCD(instanceList));
*/
}
private Map.Entry<Integer, Integer> scheduleNext(Map<Integer, Integer> instanceMap, int gcd, int max,
int previous) {
int i = previous;
int cw = (previous == -1) ? 0 : instanceMap.get(previous);
while (true) {
i = (i + 1) % instanceMap.size();
if (i == 0) {
cw = cw - gcd;
if (cw <= 0) {
cw = max;
if (cw == 0)
return null;
}
}
if (instanceMap.get(i) >= cw) {
instanceMap.put(i, cw);
System.out.println("cw " + cw);
// System.out.println("i ==== "+i);
for (Map.Entry<Integer, Integer> entry : instanceMap.entrySet()) {
if (entry.getKey() == i) {
return entry;
}
}
}
}
}
private int getGCD(List<Integer> integerList) {
int result = integerList.get(0);
for (int i = 1; i < integerList.size(); i++) {
result = IntMath.gcd(integerList.get(i), result);
if (result == 1) {
return 1;
}
}
return result;
}
}
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.common.math.IntMath;
public class WeightedRRImpl {
public static void main(String[] args) {
WeightedRRImpl wrr = new WeightedRRImpl();
Map<Integer, Integer> instanceMap = new HashMap<Integer, Integer>();
instanceMap.put(0, 20);
instanceMap.put(1, 28);
instanceMap.put(2, 21);
/*instanceMap.put(3, 4);
instanceMap.put(4, 25);
instanceMap.put(5, 0);*/
/*
* instanceMap.put(3, 4); instanceMap.put(4, 25);
*/
/*
* instanceMap.put(0, 9); instanceMap.put(1, 10); instanceMap.put(2, 8);
* instanceMap.put(3, 7); instanceMap.put(4, 6);
*/
// int gcd = 1;
List<Integer> instanceList = new ArrayList<Integer>(instanceMap.values());
int gcd = wrr.getGCD(instanceList);
System.out.println("**** gcd " + gcd);
/*
* for (int i=0;i<instanceList.size()-1;i++) { BigInteger bigInt = new
* BigInteger(instanceList.get(i).toString()); gcd = bigInt.gcd(new
* BigInteger(instanceList.get(i+1).toString())).intValue(); }
*/
Collections.sort(instanceList);
int max = instanceList.get(instanceList.size() - 1);
int previous = -1;
for (int i = 1; i <= 3; i++) {
// System.out.println("previous "+previous);
Map.Entry<Integer, Integer> entry = wrr.scheduleNext(instanceMap, gcd, max, previous);
if (entry != null) {
previous = entry.getKey();
System.out.println("selected instance:" + previous + " weight:" + entry.getValue());
} else {
System.out.println("Entry is null");
}
}
/*
* System.out.println(gcd); System.out.println(wrr.getGCD(instanceList));
*/
}
private Map.Entry<Integer, Integer> scheduleNext(Map<Integer, Integer> instanceMap, int gcd, int max,
int previous) {
int i = previous;
int cw = (previous == -1) ? 0 : instanceMap.get(previous);
while (true) {
i = (i + 1) % instanceMap.size();
if (i == 0) {
cw = cw - gcd;
if (cw <= 0) {
cw = max;
if (cw == 0)
return null;
}
}
if (instanceMap.get(i) >= cw) {
instanceMap.put(i, cw);
System.out.println("cw " + cw);
// System.out.println("i ==== "+i);
for (Map.Entry<Integer, Integer> entry : instanceMap.entrySet()) {
if (entry.getKey() == i) {
return entry;
}
}
}
}
}
private int getGCD(List<Integer> integerList) {
int result = integerList.get(0);
for (int i = 1; i < integerList.size(); i++) {
result = IntMath.gcd(integerList.get(i), result);
if (result == 1) {
return 1;
}
}
return result;
}
}
No comments:
Post a Comment