git reimport
This commit is contained in:
210
archive/2017.02/2017.02.23 - unsorted/Video.java
Normal file
210
archive/2017.02/2017.02.23 - unsorted/Video.java
Normal file
@@ -0,0 +1,210 @@
|
||||
package chelper;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import io.InputReader;
|
||||
import io.OutputWriter;
|
||||
|
||||
public class Video {
|
||||
int cacheSize;
|
||||
List<Cache> caches = new ArrayList<>();
|
||||
List<Endpoint> endpoints = new ArrayList<>();
|
||||
List<Integer> itemSizes = new ArrayList<>();
|
||||
List<Request> requests = new ArrayList<>();
|
||||
long totalWeight = 0;
|
||||
|
||||
OutputWriter log;
|
||||
|
||||
static boolean firstRun = true;
|
||||
|
||||
Solution makeSolution(double[] features) {
|
||||
Solution solution = new Solution(caches, cacheSize);
|
||||
|
||||
List<Request> requestsLocal = new ArrayList<>(requests);
|
||||
|
||||
Collections.sort(requestsLocal, new Comparator<Request>() {
|
||||
@Override
|
||||
public int compare(Request o1, Request o2) {
|
||||
double w1 = 1.0
|
||||
* Math.pow(o1.weight, features[0])
|
||||
* Math.pow(o1.endpoint.serverDelay, features[1])
|
||||
* Math.pow(itemSizes.get(o1.itemId), features[2]);
|
||||
double w2 = 1.0
|
||||
* Math.pow(o2.weight, features[0])
|
||||
* Math.pow(o2.endpoint.serverDelay, features[1])
|
||||
* Math.pow(itemSizes.get(o2.itemId), features[2]);
|
||||
|
||||
return Double.compare(w1, w2);
|
||||
}
|
||||
});
|
||||
Collections.reverse(requestsLocal);
|
||||
|
||||
|
||||
for (Request request : requestsLocal) {
|
||||
Endpoint endpoint = request.endpoint;
|
||||
int itemId = request.itemId;
|
||||
int weight = request.weight;
|
||||
int itemSize = itemSizes.get(itemId);
|
||||
|
||||
for (Cache cache : endpoint.cacheDelaysSorted) {
|
||||
if (solution.cacheSets.get(cache).contains(itemId)) {
|
||||
break;
|
||||
}
|
||||
|
||||
int sizeLeft = solution.sizeLeft.get(cache);
|
||||
|
||||
if (sizeLeft >= itemSize) {
|
||||
solution.cacheSets.get(cache).add(itemId);
|
||||
solution.sizeLeft.put(cache, sizeLeft - itemSize);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return solution;
|
||||
}
|
||||
|
||||
public void solve(int testNumber, InputReader in, OutputWriter out) {
|
||||
String testName = in.nextLine();
|
||||
try {
|
||||
log = new OutputWriter(new FileWriter("log", true));
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (firstRun) {
|
||||
log.println("========================================================================");
|
||||
}
|
||||
firstRun = false;
|
||||
log.println("-----");
|
||||
|
||||
log.println(testName);
|
||||
|
||||
int itemCounts = in.nextInt();
|
||||
int endpointCount = in.nextInt();
|
||||
int requestCount = in.nextInt();
|
||||
int cacheCount = in.nextInt();
|
||||
cacheSize = in.nextInt();
|
||||
|
||||
for (int i = 0; i < itemCounts; i++) {
|
||||
itemSizes.add(in.nextInt());
|
||||
}
|
||||
|
||||
for (int i = 0; i < cacheCount; i++) {
|
||||
caches.add(new Cache(i));
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < endpointCount; i++) {
|
||||
Endpoint endpoint = new Endpoint(i, in.nextInt());
|
||||
|
||||
int connectionCount = in.nextInt();
|
||||
for (int j = 0; j < connectionCount; j++) {
|
||||
int serverId = in.nextInt();
|
||||
int delay = in.nextInt();
|
||||
|
||||
Cache cache = caches.get(serverId);
|
||||
|
||||
cache.endpointDelays.put(endpoint, delay);
|
||||
endpoint.cacheDelays.put(cache, delay);
|
||||
}
|
||||
|
||||
endpoints.add(endpoint);
|
||||
}
|
||||
|
||||
for (Endpoint endpoint : endpoints) {
|
||||
endpoint.sortCacheDelays();
|
||||
}
|
||||
|
||||
for (int i = 0; i < requestCount; i++) {
|
||||
int itemId = in.nextInt();
|
||||
int endpointId = in.nextInt();
|
||||
int requestWeight = in.nextInt();
|
||||
|
||||
totalWeight += requestWeight;
|
||||
|
||||
Endpoint endpoint = endpoints.get(endpointId);
|
||||
endpoint.itemRequests.put(itemId, requestWeight);
|
||||
requests.add(new Request(itemId, endpoint, requestWeight));
|
||||
}
|
||||
|
||||
double bestScore = -1;
|
||||
Solution bestSolution = null;
|
||||
String bestDesc = "";
|
||||
|
||||
double[] features = new double[]{1, 1.5, -0.5};
|
||||
Solution solution = makeSolution(features);
|
||||
double score = solution.getScore(requests, totalWeight);
|
||||
|
||||
Random random = new Random();
|
||||
|
||||
double temp = 1;
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
double T = temp * (1 - (double)i / 100);
|
||||
|
||||
double[] newFeatures = new double[features.length];
|
||||
for (int j = 0; j < features.length; j++) {
|
||||
newFeatures[j] = features[j];
|
||||
}
|
||||
|
||||
int index = random.nextInt(features.length);
|
||||
int sign = 1;
|
||||
if (Math.random() < 0.5) {
|
||||
sign = -1;
|
||||
}
|
||||
newFeatures[index] += sign * Math.random() * T;
|
||||
|
||||
Solution newSolution = makeSolution(newFeatures);
|
||||
double newScore = newSolution.getScore(requests, totalWeight);
|
||||
System.out.println(i);
|
||||
System.out.println(Arrays.toString(features));
|
||||
System.out.println(newScore);
|
||||
System.out.flush();
|
||||
|
||||
if (newScore > bestScore) {
|
||||
bestScore = newScore;
|
||||
bestSolution = newSolution;
|
||||
}
|
||||
|
||||
double prob = Math.exp((newScore - score) / temp);
|
||||
|
||||
if (newScore > score || Math.random() < prob) {
|
||||
solution = newSolution;
|
||||
features = newFeatures;
|
||||
score = newScore;
|
||||
}
|
||||
}
|
||||
|
||||
log.println("Score: " + bestScore);
|
||||
log.println("Desc: " + Arrays.toString(features));
|
||||
log.println("TotalWeight: " + totalWeight);
|
||||
|
||||
try {
|
||||
out = new OutputWriter("hashcode/" + testName + ".out");
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
out.println(caches.size());
|
||||
for (Cache cache : caches) {
|
||||
out.print(cache.id + " ");
|
||||
for (Integer itemId : bestSolution.cacheSets.get(cache)) {
|
||||
out.print(itemId + " ");
|
||||
}
|
||||
out.println();
|
||||
}
|
||||
|
||||
out.flush();
|
||||
|
||||
log.flush();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user