1.代码实现python
import numpy as np
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
for person in range(1, 41):
folder_name = r"H:\tuning\head-tracking\Subject_" + str(person)
for file_name in os.listdir(folder_name):
img_path = r"H:\tuning\48Sequences" + "\\" + file_name[0:-4] + r"\0.jpg"
img = mpimg.imread(img_path)
length = img.shape[1]
highth = img.shape[0]
data = np.loadtxt(folder_name + '\\' + file_name)
data_num = len(data) # 数据数目
high = [0 for x in range(data_num)] # 初始化列表
leng = [0 for x in range(data_num)]
for i in range(data_num):
high[i] = ((90 - data[i][0]) / 90) * highth / 2
leng[i] = ((180 + data[i][1]) / 180) * length / 2
new_folder_name = "new_Subject_" + str(person)
new_file_name = "new_" + file_name
isExists = os.path.exists(new_folder_name)
if not isExists:
os.mkdir(new_folder_name)
else:
print("文件夹已存在")
with open(new_folder_name + '\\' + new_file_name, "w") as f:
for j in range(data_num):
f.write(str(high[j]))
f.write(' ')
f.write(str(leng[j]))
f.write('\n')
2.Java实现
2.1Person 实现类:
package com.tulun.people;
import lombok.*;
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
public class Person {
private int id; // ID
private int appearance; // 样貌
private int treasure; // 品格
private int character; // 财富
private int appearanceLook; // 样貌期望
private int treasureLook; // 品格期望
private int characterLook; // 财富期望
}
2.2工具类 Run:
package com.tulun.main;
import com.tulun.people.Person;
import java.io.*;
import java.util.*;
public class Run<K, V> {
private ArrayList<Person> maleList;
private ArrayList<Person> femaleList;
private ArrayList<Person> playersList;
private ArrayList<Person> man;
private ArrayList<Person> mon;
int id;
public Run(String malePath, String femalePath, String playersPath) throws IOException {
this.mon = new ArrayList<>();
this.man = new ArrayList<>();
this.maleList = new ArrayList<>();
this.femaleList = new ArrayList<>();
this.playersList = new ArrayList<>();
this.mon.addAll(saveCount(femalePath));
this.man.addAll(saveCount(malePath));
}
}
3.C++实现
设计一个流调度算法,将调度区中的流合理分配到各个端口,目标是尽量减少发送全部流的总时间
3.1阶段一代码实现
阶段一的代码主要实现了流调度算法,将流按进入设备时间排序,并分配到剩余容量最大的端口。
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <list>
struct Flow {
int id;
int enterTime;
int sendTime;
int bandwidth;
};
// 比较函数,按发送所需时间递减排序,相同则按带宽递减排序
bool compareFlows(const Flow& a, const Flow& b) {
if (a.sendTime != b.sendTime) return a.sendTime > b.sendTime;
return a.bandwidth > b.bandwidth;
}
void scheduleFlows(std::vector<Flow>& flows, int numPorts, int portCapacity) {
// 按进入设备时间排序
std::sort(flows.begin(), flows.end(), [](const Flow& a, const Flow& b) {
return a.enterTime < b.enterTime;
});
std::list<Flow> schedulingQueue; // 流调度区
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> ports; // 端口优先队列
// 初始化端口优先队列
for (int i = 0; i < numPorts; ++i) {
ports.push({0, i});
}
int currentTime = 0;
int flowIndex = 0;
while (flowIndex < flows.size() || !schedulingQueue.empty()) {
// 将当前时间的流加入调度区
while (flowIndex < flows.size() && flows[flowIndex].enterTime <= currentTime) {
schedulingQueue.push_back(flows[flowIndex++]);
}
// 按发送所需时间递减排序
schedulingQueue.sort(compareFlows);
// 分配流到端口
while (!schedulingQueue.empty()) {
Flow currentFlow = schedulingQueue.front();
schedulingQueue.pop_front();
auto port = ports.top();
ports.pop();
if (port.first + currentFlow.bandwidth <= portCapacity) {
port.first += currentFlow.bandwidth;
ports.push(port);
currentTime += currentFlow.sendTime;
} else {
// 如果当前端口无法容纳,尝试下一个端口
ports.push(port);
}
}
// 更新当前时间
if (flowIndex < flows.size()) {
currentTime = flows[flowIndex].enterTime;
}
}
std::cout << "Total time: " << currentTime << std::endl;
}
int main() {
std::vector<Flow> flows = {
{1, 0, 10, 100},
{2, 5, 15, 150},
{3, 10, 20, 200},
{4, 15, 25, 250}
};
int numPorts = 2;
int portCapacity = 500;
scheduleFlows(flows, numPorts, portCapacity);
return 0;
}
3.2阶段二代码实现
阶段二的代码在阶段一的基础上,增加了流调度区和端口排队区流数量的限制,并处理了丢弃流的情况。
阶段二代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <list>
struct Flow {
int id;
int enterTime;
int sendTime;
int bandwidth;
};
// 比较函数,按发送所需时间递减排序,相同则按带宽递减排序
bool compareFlows(const Flow& a, const Flow& b) {
if (a.sendTime != b.sendTime) return a.sendTime > b.sendTime;
return a.bandwidth > b.bandwidth;
}
void scheduleFlows(std::vector<Flow>& flows, int numPorts, int portCapacity, int maxQueueSize) {
// 按进入设备时间排序
std::sort(flows.begin(), flows.end(), [](const Flow& a, const Flow& b) {
return a.enterTime < b.enterTime;
});
std::list<Flow> schedulingQueue; // 流调度区
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> ports; // 端口优先队列
// 初始化端口优先队列
for (int i = 0; i < numPorts; ++i) {
ports.push({0, i});
}
int currentTime = 0;
int flowIndex = 0;
int totalPenalty = 0;
while (flowIndex < flows.size() || !schedulingQueue.empty()) {
// 将当前时间的流加入调度区
while (flowIndex < flows.size() && flows[flowIndex].enterTime <= currentTime) {
if (schedulingQueue.size() < maxQueueSize) {
schedulingQueue.push_back(flows[flowIndex++]);
} else {
// 丢弃流,增加罚时
totalPenalty += flows[flowIndex++].sendTime * 2;
}
}
// 按发送所需时间递减排序
schedulingQueue.sort(compareFlows);
// 分配流到端口
while (!schedulingQueue.empty()) {
Flow currentFlow = schedulingQueue.front();
schedulingQueue.pop_front();
auto port = ports.top();
ports.pop();
if (port.first + currentFlow.bandwidth <= portCapacity) {
port.first += currentFlow.bandwidth;
ports.push(port);
currentTime += currentFlow.sendTime;
} else {
// 如果当前端口无法容纳,尝试下一个端口
ports.push(port);
}
}
// 更新当前时间
if (flowIndex < flows.size()) {
currentTime = flows[flowIndex].enterTime;
}
}
std::cout << "Total time: " << currentTime << std::endl;
std::cout << "Total penalty: " << totalPenalty << std::endl;
}
int main() {
std::vector<Flow> flows = {
{1, 0, 10, 100},
{2, 5, 15, 150},
{3, 10, 20, 200},
{4, 15, 25, 250}
};
int numPorts = 2;
int portCapacity = 500;
int maxQueueSize = 3;
scheduleFlows(flows, numPorts, portCapacity, maxQueueSize);
return 0;
}
3.3总结
以上代码提供了“中兴捧月”杯校园赛事嘉年华中流调度算法的C++实现。阶段一代码实现了基本的流调度逻辑,阶段二代码在此基础上增加了流调度区和端口排队区的限制,并处理了丢弃流的情况。你可以根据具体需求进一步优化和调整代码。