1414import java .util .stream .IntStream ;
1515
1616public class Main {
17-
17+ private static int coupleIndex = 0 ;
1818 public static InputData INPUT_DATA ;
1919
2020 public static void main (String [] args ) throws IOException {
@@ -59,29 +59,29 @@ protected static void initData(final String filename) throws FileNotFoundExcepti
5959
6060 final List <Cache > caches = IntStream .range (0 , nbCaches ).mapToObj (Cache ::new ).collect (Collectors .toList ());
6161 final List <Video > videos = IntStream .range (0 , nbVideos )
62- .mapToObj ((i ) -> new Video ().setId (i ).setWeight (in .nextInt ()))
63- .collect (Collectors .toList ());
62+ .mapToObj ((i ) -> new Video ().setId (i ).setWeight (in .nextInt ()))
63+ .collect (Collectors .toList ());
6464
6565 final List <Endpoint > endpoints = new ArrayList <>();
6666 for (int endpointId = 0 ; endpointId < nbEndpoints ; endpointId ++) {
6767 final int datacenterLatency = in .nextInt ();
6868 final int numberConnectedCaches = in .nextInt ();
6969
7070 final Endpoint endpoint = new Endpoint ()
71- .setId (endpointId )
72- .setDatacenterLatency (datacenterLatency )
73- .setNumberConnectedCaches (numberConnectedCaches );
71+ .setId (endpointId )
72+ .setDatacenterLatency (datacenterLatency )
73+ .setNumberConnectedCaches (numberConnectedCaches );
7474
7575 for (int i = 0 ; i < numberConnectedCaches ; i ++) {
7676 final int cacheId = in .nextInt ();
7777 final int cacheLatency = in .nextInt ();
7878 System .out .println ("cacheId: " + cacheId );
7979 System .out .println ("cacheLatency: " + cacheLatency );
8080 caches .stream ()
81- .filter ((c ) -> c .id == cacheId )
82- .findFirst ()
83- .orElseThrow (() -> new RuntimeException ("Cache with id " + cacheId + " not found" ))
84- .addEnPoint (endpoint , cacheLatency );
81+ .filter ((c ) -> c .id == cacheId )
82+ .findFirst ()
83+ .orElseThrow (() -> new RuntimeException ("Cache with id " + cacheId + " not found" ))
84+ .addEnPoint (endpoint , cacheLatency );
8585 }
8686
8787 endpoints .add (endpoint );
@@ -96,29 +96,29 @@ protected static void initData(final String filename) throws FileNotFoundExcepti
9696 int nbRequest = in .nextInt ();
9797
9898 final Video video = videos .stream ()
99- .filter ((v ) -> v .id == videoId )
100- .findFirst ()
101- .orElseThrow (() -> new RuntimeException ("Video with id " + videoId + " not found" ));
99+ .filter ((v ) -> v .id == videoId )
100+ .findFirst ()
101+ .orElseThrow (() -> new RuntimeException ("Video with id " + videoId + " not found" ));
102102
103103 final Endpoint endpoint = endpoints .stream ()
104- .filter ((e ) -> e .id == endpointId )
105- .findFirst ()
106- .orElseThrow (() -> new RuntimeException ("Endpoint with id " + endpointId + " not found" ));
104+ .filter ((e ) -> e .id == endpointId )
105+ .findFirst ()
106+ .orElseThrow (() -> new RuntimeException ("Endpoint with id " + endpointId + " not found" ));
107107 final Request request = new Request (requestId , video , endpoint , nbRequest );
108108 requests .add (request );
109109
110110 }
111111
112112 INPUT_DATA = new InputData (
113- nbVideos ,
114- nbEndpoints ,
115- nbRequestDescriptions ,
116- nbCaches ,
117- cacheSize ,
118- videos ,
119- caches ,
120- Collections .unmodifiableList (endpoints ),
121- Collections .unmodifiableList (requests ));
113+ nbVideos ,
114+ nbEndpoints ,
115+ nbRequestDescriptions ,
116+ nbCaches ,
117+ cacheSize ,
118+ videos ,
119+ caches ,
120+ Collections .unmodifiableList (endpoints ),
121+ Collections .unmodifiableList (requests ));
122122 System .out .println ("initData END" );
123123 }
124124
@@ -127,11 +127,16 @@ public static void computeByEnpointCacheCouples() {
127127
128128 List <CacheEnpointCouple > cacheEnpointCouples = new ArrayList <>();
129129 caches .forEach (cache -> cache .endpoints .forEach ((endpoint , cacheEnpointLatency ) -> cacheEnpointCouples
130- .add (new CacheEnpointCouple (cache , endpoint , cacheEnpointLatency ))));
130+ .add (new CacheEnpointCouple (cache , endpoint , cacheEnpointLatency ))));
131131
132132 cacheEnpointCouples .sort (Comparator .comparingInt (c -> -c .latencyGain ));
133133
134- cacheEnpointCouples .forEach (Main ::storeVideosInCache );
134+ int cacheEnpointCouplesSize = cacheEnpointCouples .size ();
135+ for (CacheEnpointCouple cacheEnpointCouple : cacheEnpointCouples ) {
136+ coupleIndex ++;
137+ System .out .println ("Remaining couples: " + (cacheEnpointCouplesSize - coupleIndex ));
138+ storeVideosInCache (cacheEnpointCouple );
139+ }
135140 }
136141
137142 private static void storeVideosInCache (CacheEnpointCouple cacheEnpointCouple ) {
@@ -146,19 +151,22 @@ private static void storeVideosInCache(CacheEnpointCouple cacheEnpointCouple) {
146151
147152 if (cacheServer .getAvailableSpace (INPUT_DATA .cacheSize ) != 0 ) {
148153 final List <Request > filteredRequests = requests .stream ()
149- .filter (request -> request .endpoint .id == endpoint .id )
150- .collect (Collectors .toList ());
154+ .filter (request -> request .endpoint .id == endpoint .id )
155+ .collect (Collectors .toList ());
151156 while (true ) {
152157 Optional <Request > requestToCache = filteredRequests .stream ()
153- .filter (request -> cacheServer .videos .stream ().mapToInt (video -> video .id )
154- .noneMatch (value -> value == request .video .id ))
155- .filter (request -> request .video .weight <= cacheServer .getAvailableSpace (INPUT_DATA .cacheSize ))
156- .sorted (Comparator .comparingInt (r -> r .nbRequest ))
157- .findFirst ();
158+ .filter (request -> cacheServer .videos .stream ().mapToInt (video -> video .id )
159+ .noneMatch (value -> value == request .video .id ))
160+ .filter (request -> request .video .weight <= cacheServer .getAvailableSpace (INPUT_DATA .cacheSize ))
161+ .sorted (Comparator .comparingInt (r -> r .nbRequest * r . video . weight ))
162+ .findFirst ();
158163
159164 if (!requestToCache .isPresent ()) {
160165 break ;
161166 }
167+
168+ System .out .println ("Nb request: " + requestToCache .get ().nbRequest + " | " + "Video weight: " + requestToCache .get ().video .weight );
169+
162170 // System.out.println("Request:" + requestToCache.get().id);
163171
164172 // Add video in cacheServer
0 commit comments