我曾经研究了一周,找到这个算法的落地方案,在项目中实现了智能排班的功能。虽然已时隔多年虽然我知道 Deepseek 一定不会让我失望,但是技术人的傲气使我还是鼓起勇气,在 deepseek 对话框中敲下了这个回车。
以下是 deepseek 的回复:
假设我们要将一组物品分配给一组用户,目标是最大化用户的满意度。
class Item {
int id;
int size; // 物品的大小或权重
public Item(int id, int size) {
this.id = id;
this.size = size;
}
}
class User {
int id;
int capacity; // 用户的容量
int usedCapacity; // 已使用的容量
List<Item> items; // 分配给用户的物品
public User(int id, int capacity) {
this.id = id;
this.capacity = capacity;
this.usedCapacity = 0;
this.items = new ArrayList<>();
}
public boolean canFit(Item item) {
return usedCapacity + item.size <= capacity;
}
public void addItem(Item item) {
items.add(item);
usedCapacity += item.size;
}
}
public class FirstFit {
public static List<User> firstFit(List<Item> items, List<User> users) {
for (Item item : items) {
for (User user : users) {
if (user.canFit(item)) {
user.addItem(item);
break;
}
}
}
return users;
}
}
import java.util.*;
public class TabuSearch {
private static final int TABU_TENURE = 5; // 禁忌表长度
private static final int MAX_ITERATIONS = 100; // 最大迭代次数
public static List<User> tabuSearch(List<Item> items, List<User> users) {
List<User> bestSolution = new ArrayList<>(users);
int bestFitness = calculateFitness(bestSolution);
Queue<Move> tabuList = new LinkedList<>();
List<User> currentSolution = new ArrayList<>(users);
for (int iteration = 0; iteration < MAX_ITERATIONS; iteration++) {
Move bestMove = null;
int bestMoveFitness = Integer.MIN_VALUE;
// 生成邻域解
for (int i = 0; i < currentSolution.size(); i++) {
for (int j = 0; j < items.size(); j++) {
Move move = new Move(i, j);
if (!tabuList.contains(move)) {
List<User> newSolution = applyMove(currentSolution, items, move);
int newFitness = calculateFitness(newSolution);
if (newFitness > bestMoveFitness) {
bestMove = move;
bestMoveFitness = newFitness;
}
}
}
}
if (bestMove != null) {
currentSolution = applyMove(currentSolution, items, bestMove);
tabuList.add(bestMove);
if (tabuList.size() > TABU_TENURE) {
tabuList.poll();
}
if (bestMoveFitness > bestFitness) {
bestSolution = new ArrayList<>(currentSolution);
bestFitness = bestMoveFitness;
}
}
}
return bestSolution;
}
private static List<User> applyMove(List<User> users, List<Item> items, Move move) {
List<User> newUsers = new ArrayList<>();
for (User user : users) {
newUsers.add(new User(user.id, user.capacity));
}
Item item = items.get(move.itemIndex);
User user = newUsers.get(move.userIndex);
if (user.canFit(item)) {
user.addItem(item);
}
return newUsers;
}
private static int calculateFitness(List<User> users) {
int fitness = 0;
for (User user : users) {
fitness += user.items.size(); // 简单的适应度函数,可以根据需求调整
}
return fitness;
}
private static class Move {
int userIndex;
int itemIndex;
public Move(int userIndex, int itemIndex) {
this.userIndex = userIndex;
this.itemIndex = itemIndex;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Move move = (Move) o;
return userIndex == move.userIndex && itemIndex == move.itemIndex;
}
@Override
public int hashCode() {
return Objects.hash(userIndex, itemIndex);
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Item> items = Arrays.asList(
new Item(1, 3),
new Item(2, 5),
new Item(3, 2),
new Item(4, 4)
);
List<User> users = Arrays.asList(
new User(1, 10),
new User(2, 8),
new User(3, 6)
);
// First Fit
List<User> firstFitSolution = FirstFit.firstFit(items, users);
System.out.println("First Fit Solution:");
printSolution(firstFitSolution);
// Tabu Search
List<User> tabuSearchSolution = TabuSearch.tabuSearch(items, users);
System.out.println("Tabu Search Solution:");
printSolution(tabuSearchSolution);
}
private static void printSolution(List<User> users) {
for (User user : users) {
System.out.println("User " + user.id + " has items: " + user.items);
}
}
}
虽然这段代码不能工程化, 但是程序设计思想100% ok。 可以稍微完善下贫血模型就可以实现一个基础的算法了。
我有认真思考的点, 我们的 JAVA 工程师在面对 deepseek 时,护城河还剩下什么? 我们应该给自己新的定位叫:代码片段加工师,或者叫代码设计师。
对的。我们应该积极拥抱变化,做程序设计者, deepseek 暂时不能做工程师但是我们做的是一个完整交付的工程, 这点老板不一定清楚, 但是一定有专家会清楚。所以希望大家能把 “程序设计师” 的 title 挂在自己的简历上, 不再是程序员不再是码农, 从今天起请叫我们
程序设计师