0

I have a question about traveling salesman problem. I have distance matrix between cities. Each city has different amount of beers. I need to collect as many items as I can and return to the starting point. I have a car that can drive 1000km. Could someone help with this? Any ideas?

    List<BreweryCodes> breweryList = new ArrayList<>();

    for (BreweryCodes brewery : breweryCodesService.getAllBreweryCodes()) 
    {
        // we calculate distance between home and breweries
        double distanceToHome = BeerUtils.haversine(myCoordinates, brewery);

        // collecting breweries that fit by the distance
        if (distanceToHome <= BeerUtils.MAX_DISTANCE / 2d) 
        {
            breweryList.add(brewery);
        }
    }

    int beerCount = 0;
    List<Long> result = new ArrayList<>();
    TravelInfo home = new TravelInfo(Arrays.asList(0L), 0, 0);
    home.setLatitude(myCoordinates.getLatitude());
    home.setLongitude(myCoordinates.getLongitude());

    Stack<TravelInfo> stack = new Stack<>();
    stack.add(home);

    while (!stack.isEmpty()) 
    {
        TravelInfo currentPoint = stack.pop();
        List<Long> breweryIds = currentPoint.getBreweries();

        for (BreweryCodes nextBrewery : breweryList) 
        {
            if (breweryIds.contains(nextBrewery.getBreweryId())) 
            {
                continue;
            }

            double distanceToHome = BeerUtils.haversine(myCoordinates, nextBrewery);
            double distanceBetween = BeerUtils.haversine(currentPoint, nextBrewery) + currentPoint.getTraveledDistance();

            if ((distanceBetween + distanceToHome) <= BeerUtils.MAX_DISTANCE) 
            {
                List<Long> ids = new ArrayList<>(breweryIds);
                ids.add(nextBrewery.getBreweryId());

                TravelInfo travel = new TravelInfo(ids, 0, distanceBetween);
                travel.setLatitude(nextBrewery.getLatitude());
                travel.setLongitude(nextBrewery.getLongitude());

                beerCount = currentPoint.getBeerCount() + nextBrewery.getBeerCount();
                travel.setBeerCount(beerCount);

                stack.push(travel);
            } 
            else 
            {
                System.out.println(currentPoint.getBreweries() + "          " + beerCount);
            }
        }
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    from the question it hard to know where you need help is it in calculating the distance between cities? or what items are you collecting be as detailed as possible – Stephen Ngethe Feb 02 '20 at 19:53
  • This is how I imagine to solve. I need to find all possible paths between cities that fits in 1000km. Then iterate through the list of paths and calculate max items. – origamis0302 Feb 02 '20 at 20:17
  • I have a car with enough fuel to go 1000km. I have to visit as many cities within that distance as I can and collect as many items as I can. All data I have (distances between cities, what items belongs to city). In my case it does not matter what kind of item, the point is to collect as much as I can. – origamis0302 Feb 02 '20 at 20:23
  • 1
    Have you tried to solve this yourself yet? If so, great! Show some code, what you've tried, and why it didn't work and we would be more than happy to help. If not, please give this a shot on your own and come back with any more specific questions you have. – Jesse Feb 02 '20 at 20:50
  • With my coordinates I can reach 150 breweries – origamis0302 Feb 02 '20 at 21:20
  • @origamis0302 checkout https://developers.google.com/places/web-service/details for api options from google as pointed out try some code StackOverflow will help mainly with the code. – Stephen Ngethe Feb 03 '20 at 16:42
  • checkout this links for something that may help https://help.openstreetmap.org/questions/9495/calculate-route-and-the-list-of-places-along-the-route https://stackoverflow.com/questions/5983462/google-maps-find-cities-close-to-my-route All the best – Stephen Ngethe Feb 03 '20 at 16:45

0 Answers0