【国际科研论坛】“智控未来:传感智能×机器学习×信息工程×新能源——2025年6月四大国际峰会技术融合”
import numpy as np
def weighted_fusion(sensor_data, weights):
"""动态权重分配的多传感器融合"""
normalized_weights = np.array(weights) / sum(weights)
fused_value = np.dot(sensor_data, normalized_weights)
return fused_value
# 示例:三传感器温度数据(假设传感器可靠性权重为[0.8, 0.7, 0.9])
sensor_values = [85.3, 86.1, 84.9]
weights = [0.8, 0.7, 0.9]
result = weighted_fusion(sensor_values, weights)
print(f"融合温度值:{result:.2f}℃") # 输出:融合温度值:85.04℃
import torch
import torch.nn as nn
import numpy as np
class DQN(nn.Module):
def __init__(self, state_dim, action_dim):
super(DQN, self).__init__()
self.net = nn.Sequential(
nn.Linear(state_dim, 128),
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, action_dim)
def forward(self, x):
return self.net(x)
# 引入优先级经验回放
class PrioritizedReplayBuffer:
def __init__(self, capacity):
self.capacity = capacity
self.buffer = []
self.priorities = np.zeros(capacity)
def add(self, experience, priority):
if len(self.buffer) >= self.capacity:
idx = np.argmin(self.priorities)
self.buffer[idx] = experience
self.priorities[idx] = priority
else:
self.buffer.append(experience)
self.priorities[len(self.buffer)-1] = priority
from collections import defaultdict
def build_huffman_tree(freq):
heap = [[weight, [char, ""]] for char, weight in freq.items()]
while len(heap) > 1:
left = heapq.heappop(heap)
right = heapq.heappop(heap)
for pair in left[1:]:
pair[1] = '0' + pair[1]
for pair in right[1:]:
pair[1] = '1' + pair[1]
heapq.heappush(heap, [left[0] + right[0]] + left[1:] + right[1:])
return heap[0]
# 示例:日志字符频率统计
log_data = "ERROR:404;WARN:302;INFO:200"
freq = defaultdict(int)
for char in log_data:
freq[char] += 1
huffman_tree = build_huffman_tree(freq)
print("哈夫曼编码表:", huffman_tree[1:])
import random
def pso_optimize(objective_func, bounds, n_particles=50, max_iter=200):
class Particle:
def __init__(self):
self.position = [random.uniform(b[0], b[1]) for b in bounds]
self.velocity = [0 for _ in bounds]
self.best_pos = self.position.copy()
self.best_score = float('inf')
swarm = [Particle() for _ in range(n_particles)]
global_best = [0]*len(bounds)
global_score = float('inf')
for _ in range(max_iter):
for particle in swarm:
current_score = objective_func(particle.position)
if current_score < particle.best_score:
particle.best_pos = particle.position.copy()
particle.best_score = current_score
if current_score < global_score:
global_best = particle.position.copy()
global_score = current_score
# 引入惯性权重动态调整
w = 0.9 - (0.5 * _ / max_iter)
for particle in swarm:
for i in range(len(bounds)):
r1, r2 = random.random(), random.random()
particle.velocity[i] = w * particle.velocity[i] + \
2 * r1 * (particle.best_pos[i] - particle.position[i]) + \
2 * r2 * (global_best[i] - particle.position[i])
particle.position[i] += particle.velocity[i]
return global_best
# 示例:最小化储能成本函数
def cost_function(x):
return 5*x[0]**2 + 3*x[1] + 10
solution = pso_optimize(cost_function, [(-10,10), (-5,5)])
print(f"最优储能参数:{solution}")