How to stop jsprit and save current best solution when VM get's shutdown

I’m running jsprit on Google Compute Engine’s Preemptible VM instances. This means that the VM instance can be shut down at any time. When preemption happens the VM instance will receive a soft off signal and after 30 seconds it will shutdown.

The potential issue here is that when jsprit is still in the process of solving the vrp and preemption happens everything will be lost.

I’ve added a shutdown hook to my code, so when it receives a shutdown event, it will run that code. Here’s what it looks like:

Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
        // 1) Tell jsprit to stop and return the current best solution
        // 2) Store current best solution somewhere
        // 3) Shut down
    }
});

What I’m looking for is a way to tell jsprit to stop and return the current best solution. Is this possible?

Hiya, check this;

Unfortunately premature termination does not give me the option to stop jsprit at any time I want (in this case when a shutdown event happens). Premature termination gives you the option for jsprit to terminate based on time itreations, variation coefficient.

Or is there something I’m missing?

 @Test
    public void whenSettingPrematureTermination_itIsExecutedCorrectly() {
        SearchStrategyManager stratManager = mock(SearchStrategyManager.class);
        VehicleRoutingAlgorithm algorithm = new VehicleRoutingAlgorithm(mock(VehicleRoutingProblem.class),
            stratManager);
        when(stratManager.getRandomStrategy()).thenReturn(mock(SearchStrategy.class));
        when(stratManager.getWeights()).thenReturn(Arrays.asList(1.0));
        algorithm.setMaxIterations(1000);
        PrematureAlgorithmTermination termination = new PrematureAlgorithmTermination() {

            private int nuOfIterations = 1;

            @Override
            public boolean isPrematureBreak(SearchStrategy.DiscoveredSolution discoveredSolution) {
                if (nuOfIterations == 50) return true;
                nuOfIterations++;
                return false;
            }
        };
        CountIterations counter = new CountIterations();
        algorithm.addListener(counter);
        algorithm.setPrematureAlgorithmTermination(termination);
        algorithm.searchSolutions();
        assertEquals(50, counter.getCountIterations());
    }

The way I read this, if Number of Iterations = 50 then return true. So you would check if shutdown has been triggered.

@Override
            public boolean isPrematureBreak(SearchStrategy.DiscoveredSolution discoveredSolution) {
                if (nuOfIterations == 50) return true;
                nuOfIterations++;
                return false;
            }
Powered by Discourse