13693261870
2025-07-02 6708810c4de34dfb9513061432d656f91d56ee3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.ruoyi.buss;
 
import java.util.*;
 
class Port {
    String name;
    int berths;
    int supplyCapacity;
    int x, y; // Coordinates for distance calculation
 
    public Port(String name, int berths, int supplyCapacity, int x, int y) {
        this.name = name;
        this.berths = berths;
        this.supplyCapacity = supplyCapacity;
        this.x = x;
        this.y = y;
    }
}
 
class Ship {
    String name;
    int supplyNeed;
    int x, y; // Current coordinates
 
    public Ship(String name, int supplyNeed, int x, int y) {
        this.name = name;
        this.supplyNeed = supplyNeed;
        this.x = x;
        this.y = y;
    }
}
 
class Assignment {
    List<Integer> assignments; // Index of port for each ship
    double fitness;
 
    public Assignment(List<Integer> assignments) {
        this.assignments = assignments;
        this.fitness = 0.0;
    }
}
 
public class GeneticFleetAssignment {
 
    private static final int POPULATION_SIZE = 50;
    private static final int GENERATIONS = 100;
    private static final double MUTATION_RATE = 0.1;
 
    public static double calculateDistance(Ship ship, Port port) {
        return Math.sqrt(Math.pow(ship.x - port.x, 2) + Math.pow(ship.y - port.y, 2));
    }
 
    public static double evaluateFitness(Assignment assignment, List<Ship> ships, List<Port> ports) {
        double totalDistance = 0.0;
        int[] portBerths = new int[ports.size()];
        int[] portSupply = new int[ports.size()];
 
        for (int i = 0; i < assignment.assignments.size(); i++) {
            int portIndex = assignment.assignments.get(i);
            Ship ship = ships.get(i);
            Port port = ports.get(portIndex);
 
            totalDistance += calculateDistance(ship, port);
            portBerths[portIndex]++;
            portSupply[portIndex] += ship.supplyNeed;
        }
 
        for (int i = 0; i < ports.size(); i++) {
            if (portBerths[i] > ports.get(i).berths || portSupply[i] > ports.get(i).supplyCapacity) {
                return Double.MAX_VALUE; // Invalid assignment
            }
        }
 
        return totalDistance;
    }
 
    public static Assignment crossover(Assignment parent1, Assignment parent2) {
        Random rand = new Random();
        int crossoverPoint = rand.nextInt(parent1.assignments.size());
        List<Integer> childAssignments = new ArrayList<>(parent1.assignments.subList(0, crossoverPoint));
        childAssignments.addAll(parent2.assignments.subList(crossoverPoint, parent2.assignments.size()));
        return new Assignment(childAssignments);
    }
 
    public static void mutate(Assignment assignment, int numPorts) {
        Random rand = new Random();
        if (rand.nextDouble() < MUTATION_RATE) {
            int index = rand.nextInt(assignment.assignments.size());
            assignment.assignments.set(index, rand.nextInt(numPorts));
        }
    }
 
    public static Assignment geneticAlgorithm(List<Ship> ships, List<Port> ports) {
        Random rand = new Random();
        List<Assignment> population = new ArrayList<>();
 
        // Initialize population
        for (int i = 0; i < POPULATION_SIZE; i++) {
            List<Integer> assignments = new ArrayList<>();
            for (int j = 0; j < ships.size(); j++) {
                assignments.add(rand.nextInt(ports.size()));
            }
            population.add(new Assignment(assignments));
        }
 
        for (int generation = 0; generation < GENERATIONS; generation++) {
            // Evaluate fitness
            for (Assignment assignment : population) {
                assignment.fitness = evaluateFitness(assignment, ships, ports);
            }
 
            // Sort by fitness
            population.sort(Comparator.comparingDouble(a -> a.fitness));
 
            // Selection and reproduction
            List<Assignment> newPopulation = new ArrayList<>();
            for (int i = 0; i < POPULATION_SIZE / 2; i++) {
                Assignment parent1 = population.get(i);
                Assignment parent2 = population.get(rand.nextInt(POPULATION_SIZE / 2));
                Assignment child = crossover(parent1, parent2);
                mutate(child, ports.size());
                newPopulation.add(child);
            }
 
            population = newPopulation;
        }
 
        // Return the best solution
        return population.get(0);
    }
 
    public static void main(String[] args) {
        List<Port> ports = Arrays.asList(
            new Port("Port A", 2, 100, 0, 0),
            new Port("Port B", 1, 50, 5, 5),
            new Port("Port C", 3, 150, 10, 10)
        );
 
        List<Ship> ships = Arrays.asList(
            new Ship("Ship 1", 30, 0, 0),
            new Ship("Ship 2", 70, 0, 0),
            new Ship("Ship 3", 50, 9, 9)
        );
 
        Assignment bestAssignment = geneticAlgorithm(ships, ports);
 
        for (int i = 0; i < ships.size(); i++) {
            System.out.println("Ship " + ships.get(i).name + " assigned to port " + ports.get(bestAssignment.assignments.get(i)).name);
        }
    }
}