当前位置: 首页 > news >正文

决策树方法根据指定条件筛选方案

代码功能说明

  1. 条件类Condition 类用于定义每个条件的范围,并提供一个方法 is_satisfied 来检查输入值是否满足该条件。

  2. 算法选择器类AlgorithmSelector 类负责应用条件并记录不满足的条件。它提供方法 apply_condition 用于更新可用算法,get_selected_algorithm 用于返回选中的算法,以及 log_failed_conditions 方法用于输出失败的条件信息。

  3. 规则定义rules 字典中定义了每个条件的范围以及对应的适用算法。通过这种方式,您可以轻松扩展或修改条件和算法的对应关系。

  4. 选择算法select_algorithm 函数根据输入值和启用的开关选择合适的算法。它将输入值传递给 AlgorithmSelector 进行处理,并最终返回符合条件的算法编号。

可扩展性

  • 添加新条件:要添加新条件,只需在 rules 字典中添加新的条件范围和适用算法。
  • 修改条件:可以直接修改 rules 中的条件范围或对应的算法集合。
  • 启用/禁用条件:可以通过开关参数灵活地启用或禁用某些条件。
class Condition:def __init__(self, min_value, max_value, name):self.min_value = min_valueself.max_value = max_valueself.name = namedef is_satisfied(self, input_value):return self.min_value <= input_value < self.max_valueclass AlgorithmSelector:def __init__(self):self.algorithms = set(range(8))  # 假设有8个算法self.failed_conditions = []def apply_condition(self, condition, input_value, applicable_algorithms):if condition.is_satisfied(input_value):self.algorithms.intersection_update(applicable_algorithms)else:self.failed_conditions.append(f"Condition '{condition.name}' failed. "f"Input value: {input_value}, "f"expected range: [{condition.min_value}, {condition.max_value})")def get_selected_algorithm(self):return next(iter(self.algorithms), -1)  # 返回第一个符合条件的算法,若无则返回-1def log_failed_conditions(self):if not self.failed_conditions:print("All conditions passed.")else:print("Failed conditions:")for condition in self.failed_conditions:print(f" - {condition}")# 定义每个条件的范围和适用的算法
rules = {"ISO": [(Condition(0, 200, "ISO 0-200"), {0}),(Condition(200, 800, "ISO 200-800"), {1, 2}),(Condition(800, float('inf'), "ISO 800+"), {2}),],"Facelv": [(Condition(0, 2, "Facelv 0-2"), {0, 3}),(Condition(2, 5, "Facelv 2-5"), {3}),],"Zoom": [(Condition(0, 2.0, "Zoom 0-2.0"), {1}),(Condition(2.0, 3.0, "Zoom 2.0-3.0"), {4}),(Condition(3.0, float('inf'), "Zoom 3.0+"), {4, 5}),],"Face Count": [(Condition(0, 2, "Face Count 0-2"), {2}),(Condition(3, 5, "Face Count 3-5"), {2, 4}),(Condition(6, float('inf'), "Face Count 6+"), {4, 6}),],"Face Size": [(Condition(0, 50, "Face Size 0-50"), {3}),(Condition(50, 100, "Face Size 50-100"), {5}),(Condition(100, float('inf'), "Face Size 100+"), {3, 7}),],
}def select_algorithm(iso, facelv, zoom, face_count, face_size,use_iso=True, use_facelv=True, use_zoom=True, use_face_count=True, use_face_size=False):selector = AlgorithmSelector()# 输入值列表和条件启用的开关inputs = [iso, facelv, zoom, face_count, face_size]switches = [use_iso, use_facelv, use_zoom, use_face_count, use_face_size]# 遍历每个条件for i, (condition_name, conditions) in enumerate(rules.items()):if switches[i]:  # 如果该条件启用for condition, applicable_algorithms in conditions:selector.apply_condition(condition, inputs[i], applicable_algorithms)# 输出不满足的条件selector.log_failed_conditions()# 返回选择的算法return selector.get_selected_algorithm()# 示例输入
iso = 300
facelv = 3
zoom = 2.5
face_count = 4
face_size = 75# 启用/禁用各条件的开关
selected_algorithm = select_algorithm(iso, facelv, zoom, face_count, face_size,use_iso=True,use_facelv=True,use_zoom=True,use_face_count=True,use_face_size=False)if selected_algorithm != -1:print(f"根据条件,选择的算法是: {selected_algorithm}")
else:print("未找到符合条件的算法。")

C++实现:

#include <iostream>
#include <vector>
#include <set>
#include <limits>
#include <string>class Condition {
public:Condition(double min_value, double max_value, const std::string& name): min_value(min_value), max_value(max_value), name(name) {}bool isSatisfied(double input_value) const {return min_value <= input_value && input_value < max_value;}const std::string& getName() const {return name;}private:double min_value;double max_value;std::string name;
};class AlgorithmSelector {
public:AlgorithmSelector() : algorithms({0, 1, 2, 3, 4, 5, 6, 7}) {}void applyCondition(const Condition& condition, double input_value, const std::set<int>& applicable_algorithms) {if (condition.isSatisfied(input_value)) {intersectAlgorithms(applicable_algorithms);} else {failed_conditions.push_back("Condition '" + condition.getName() + "' failed. ""Input value: " + std::to_string(input_value) + ", expected range: [" + std::to_string(condition.getMin()) + ", " + std::to_string(condition.getMax()) + ")");}}int getSelectedAlgorithm() const {return algorithms.empty() ? -1 : *algorithms.begin();  // 返回第一个符合条件的算法,若无则返回-1}void logFailedConditions() const {if (failed_conditions.empty()) {std::cout << "All conditions passed." << std::endl;} else {std::cout << "Failed conditions:" << std::endl;for (const auto& condition : failed_conditions) {std::cout << " - " << condition << std::endl;}}}private:std::set<int> algorithms;std::vector<std::string> failed_conditions;void intersectAlgorithms(const std::set<int>& applicable_algorithms) {std::set<int> intersection;for (const auto& algo : algorithms) {if (applicable_algorithms.count(algo) > 0) {intersection.insert(algo);}}algorithms = intersection; // 更新可用算法}
};// 定义每个条件的范围和适用的算法
const std::vector<std::pair<Condition, std::set<int>>> rules[] = {{{0, 200, "ISO 0-200"}, {0}},{{200, 800, "ISO 200-800"}, {1, 2}},{{800, std::numeric_limits<double>::infinity(), "ISO 800+"}, {2}},{{0, 2, "Facelv 0-2"}, {0, 3}},{{2, 5, "Facelv 2-5"}, {3}},{{0, 2.0, "Zoom 0-2.0"}, {1}},{{2.0, 3.0, "Zoom 2.0-3.0"}, {4}},{{3.0, std::numeric_limits<double>::infinity(), "Zoom 3.0+"}, {4, 5}},{{0, 2, "Face Count 0-2"}, {2}},{{3, 5, "Face Count 3-5"}, {2, 4}},{{6, std::numeric_limits<double>::infinity(), "Face Count 6+"}, {4, 6}},{{0, 50, "Face Size 0-50"}, {3}},{{50, 100, "Face Size 50-100"}, {5}},{{100, std::numeric_limits<double>::infinity(), "Face Size 100+"}, {3, 7}}
};int selectAlgorithm(double iso, double facelv, double zoom, double face_count, double face_size,bool use_iso = true, bool use_facelv = true, bool use_zoom = true, bool use_face_count = true, bool use_face_size = true) {AlgorithmSelector selector;// 输入值和启用的开关std::vector<double> inputs = {iso, facelv, zoom, face_count, face_size};std::vector<bool> switches = {use_iso, use_facelv, use_zoom, use_face_count, use_face_size};// 遍历每个条件for (size_t i = 0; i < switches.size(); ++i) {if (switches[i]) {  // 如果该条件启用for (const auto& rule : rules[i]) {selector.applyCondition(rule.first, inputs[i], rule.second);}}}// 输出不满足的条件selector.logFailedConditions();// 返回选择的算法return selector.getSelectedAlgorithm();
}int main() {// 示例输入double iso = 300;double facelv = 3;double zoom = 2.5;double face_count = 4;double face_size = 75;// 启用/禁用各条件的开关int selected_algorithm = selectAlgorithm(iso, facelv, zoom, face_count, face_size,true, true, true, true, false);if (selected_algorithm != -1) {std::cout << "根据条件,选择的算法是: " << selected_algorithm << std::endl;} else {std::cout << "未找到符合条件的算法。" << std::endl;}return 0;
}

比较有意思的实现:

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <iterator>// 定义规则结构
struct Rule {std::vector<std::pair<double, double>> ranges;  // 条件范围std::vector<std::set<int>> actions;  // 对应算法的集合
};// 定义规则表,适应同一条件多个范围对应多个算法的情况
std::vector<Rule> rules = {{ {{0, 200}, {200, 800}, {800, 1e9}}, {{0}, {1, 2}, {2}} },                 // iso规则{ {{0, 2}, {2, 5}}, {{0, 3}, {3}} },                                        // facelv规则{ {{0, 2.0}, {2.0, 3.0}, {3.0, 1e9}}, {{1}, {4}, {4, 5}} },                 // zoom规则{ {{0, 2}, {3, 5}, {6, 1e9}}, {{2}, {2, 4}, {4, 6}} },                      // face_count规则{ {{0, 50}, {50, 100}, {100, 1e9}}, {{3}, {5}, {3, 7}} }                    // face_size规则
};// 根据输入条件和开关选择最优算法
int select_algorithm(double iso, double facelv, double zoom, double face_count, double face_size,bool use_iso, bool use_facelv, bool use_zoom, bool use_face_count, bool use_face_size) {std::set<int> selected_actions = {0, 1, 2, 3, 4, 5, 6, 7};  // 初始假设有8种算法// 输入条件值std::vector<double> inputs = {iso, facelv, zoom, face_count, face_size};// 各条件是否启用的开关std::vector<bool> switches = {use_iso, use_facelv, use_zoom, use_face_count, use_face_size};// 遍历每个条件for (int i = 0; i < rules.size(); ++i) {if (!switches[i]) continue;  // 如果该条件未启用,跳过const Rule& rule = rules[i];std::set<int> current_matching_actions;  // 用于存储该条件下匹配的算法// 遍历每个范围,找到满足当前条件的所有算法集合for (int j = 0; j < rule.ranges.size(); ++j) {if (inputs[i] >= rule.ranges[j].first && inputs[i] < rule.ranges[j].second) {current_matching_actions.insert(rule.actions[j].begin(), rule.actions[j].end());}}// 如果当前条件有匹配的算法,取交集if (!current_matching_actions.empty()) {std::set<int> intersection;std::set_intersection(selected_actions.begin(), selected_actions.end(),current_matching_actions.begin(), current_matching_actions.end(),std::inserter(intersection, intersection.begin()));selected_actions = intersection;}}// 返回第一个符合条件的算法return selected_actions.empty() ? -1 : *selected_actions.begin();
}int main() {// 示例输入double iso = 300;double facelv = 3;double zoom = 2.5;double face_count = 4;double face_size = 75;// 启用/禁用各条件的开关bool use_iso = true;bool use_facelv = true;bool use_zoom = true;bool use_face_count = true;bool use_face_size = false;  // 例如不使用face_size条件// 调用选择函数int selected_algorithm = select_algorithm(iso, facelv, zoom, face_count, face_size,use_iso, use_facelv, use_zoom, use_face_count, use_face_size);if (selected_algorithm != -1) {std::cout << "根据条件,选择的算法是: " << selected_algorithm << std::endl;} else {std::cout << "未找到符合条件的算法。" << std::endl;}return 0;
}

相关文章:

决策树方法根据指定条件筛选方案

代码功能说明 条件类&#xff1a;Condition 类用于定义每个条件的范围&#xff0c;并提供一个方法 is_satisfied 来检查输入值是否满足该条件。 算法选择器类&#xff1a;AlgorithmSelector 类负责应用条件并记录不满足的条件。它提供方法 apply_condition 用于更新可用算法&a…...

多特征变量序列预测(四) Transformer-BiLSTM风速预测模型

往期精彩内容&#xff1a; 时序预测&#xff1a;LSTM、ARIMA、Holt-Winters、SARIMA模型的分析与比较 全是干货 | 数据集、学习资料、建模资源分享&#xff01; EMD、EEMD、FEEMD、CEEMD、CEEMDAN的区别、原理和Python实现&#xff08;一&#xff09;EMD-CSDN博客 EMD、EEM…...

【开源免费】基于SpringBoot+Vue.JS蜗牛兼职平台 (JAVA毕业设计)

本文项目编号 T 034 &#xff0c;文末自助获取源码 \color{red}{T034&#xff0c;文末自助获取源码} T034&#xff0c;文末自助获取源码 目录 一、系统介绍1.1 平台架构1.2 管理后台1.3 用户网页端1.4 技术特点 二、演示录屏三、启动教程四、功能截图五、文案资料5.1 选题背景…...

Ajax笔记

介绍 Ajax是一种网页开发技术&#xff0c;全称是Asynchronous JavaScript and XML&#xff08;异步JavaScript和XML&#xff09;。作用如下&#xff1a; 数据交换&#xff1a;可以通过Ajax给服务器发送请求&#xff0c;并获取服务器响应的数据。即前端动态的发送Ajax到服务器端…...

软考:缓存分片和一致性哈希

缓存分片技术是一种将数据分散存储在多个节点上的方法&#xff0c;它在分布式缓存系统中尤为重要。这项技术的核心目的是提高系统的性能和可扩展性&#xff0c;同时确保数据的高可用性。以下是缓存分片技术的一些关键点&#xff1a; 数据分片&#xff1a;缓存分片涉及将数据分成…...

3109 体验积分值

经验值&#xff1a;1200 时间限制&#xff1a;1000毫秒 内存限制&#xff1a;128MB 合肥市第34届信息学竞赛&#xff08;2017年&#xff09; 不许抄袭&#xff0c;一旦发现&#xff0c;直接清空经验&#xff01; 题目描述 Description 卡卡西和小朋友们做完了烧脑的数字游…...

初识jsp

学习本章节前建议先安装Tomcat web服务器&#xff1a;tomcat下载安装及配置教程_tomcat安装-CSDN博客 1、概念 我的第一个JSP程序&#xff1a; 在WEB-INF目录之外创建一个index.jsp文件&#xff0c;然后这个文件中没有任何内容。将上面的项目部署之后&#xff0c;启动服务器…...

Ansible 的脚本 --- playbooks剧本

playbooks 本身由以下各部分组成 &#xff08;1&#xff09;Tasks&#xff1a;任务&#xff0c;即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行 &#xff08;2&#xff09;Vars&#xff1a;变量 &#xff08;3&#xff09;Templates&#xff1a;模板 &a…...

Windows 死机时 系统错误日志分析与故障排除

目录 前言正文 前言 对于服务器异常重启&#xff0c;推荐阅读&#xff1a;详细分析服务器自动重启原因&#xff08;涉及Linux、Window&#xff09; 以下主要做一个总结梳理 正文 查看系统事件日志&#xff1a; 可以查看系统事件日志&#xff0c;找出可能导致系统崩溃的错误…...

基于pytorch搭建CNN

先上代码 import torch import torch.nn as nn import torch.optim as optim import torch.nn.functional as F from torchvision import datasets, transforms import matplotlib.pyplot as plt import numpy as np import pandas as pd import matplotlibmatplotlib.use(tkA…...

C#实现与Windows服务的交互与控制

在C#中&#xff0c;与Windows服务进行交互和控制通常涉及以下几个步骤&#xff1a; 创建Windows服务&#xff1a;首先&#xff0c;需要创建一个Windows服务项目。可以使用Visual Studio中的“Windows 服务 (.NET Framework)”项目模板来创建Windows服务。 配置服务控制事件&am…...

Java和Ts构造函数的区别

java中子类在使用有参构造创建对象的时候不必要必须调用父类有参构造 而js则必须用super()调用父类的有参构造,即使用不到也必须传递 Java 中的处理方式 可选择性参数: 在 Java 中&#xff0c;当子类使用父类的有参构造方法创建对象时&#xff0c;可以只传递需要的参数。如果父…...

植物健康,Spring Boot来助力

3系统分析 3.1可行性分析 通过对本植物健康系统实行的目的初步调查和分析&#xff0c;提出可行性方案并对其一一进行论证。我们在这里主要从技术可行性、经济可行性、操作可行性等方面进行分析。 3.1.1技术可行性 本植物健康系统采用SSM框架&#xff0c;JAVA作为开发语言&#…...

百度文心一言接入流程-java版

百度文心一言接入流程-java版 一、准备工作二、API接口调用-java三、百度Prompt工程参考资料: 百度文心一言:https://yiyan.baidu.com/百度千帆大模型:https://qianfan.cloud.baidu.com/百度千帆大模型文档:https://cloud.baidu.com/doc/WENXINWORKSHOP/index.html千tokens…...

Java 11 新特性深度解析与应用实践

Java 作为一种广泛应用的编程语言&#xff0c;不断演进以满足开发者日益增长的需求和适应技术的发展趋势。Java 11 带来了一系列重要的新特性和改进&#xff0c;这些变化不仅提升了语言的性能和功能&#xff0c;还为开发者提供了更好的开发体验和工具。本文将深入探讨 Java 11 …...

druid 连接池监控报错 Sorry, you are not permitted to view this page.本地可以,发布正式出错

简介&#xff1a; druid 连接池监控报错 Sorry, you are not permitted to view this page. 使用Druid连接池的时候&#xff0c;遇到一个奇怪的问题&#xff0c;在本地&#xff08;localhost&#xff09;可以直接打开Druid连接池监控&#xff0c;在其他机器上打开会报错&#…...

[RN与H5] 加载线上H5通信失败问题记录(启动本地H5服务OK)

RT: nextjs项目 在本地启动H5服务, 本地开发都OK 发布到线上后, 效果全无, 经排查发现, 写了基本配置的js脚本在挂载时机上的差异导致 根本原因是...

electron 打包

安装及配置 安装electron包以及electron-builder打包工具 # 安装 electron cnpm install --save-dev electron # 安装打包工具 cnpm install electron-builder -D 参考的package.json文件 其中description和author为必填项目 {"name": "appfile",&qu…...

ChatGLM-6B和Prompt搭建专业领域知识问答机器人应用方案(含完整代码)

目录 ChatGLM-6B部署 领域知识数据准备 领域知识数据读取 知识相关性匹配 Prompt提示工程 领域知识问答 完整代码 本文基于ChatGLM-6B大模型和Pompt提示工程搭建医疗领域知识问答机器人为例。 ChatGLM-6B部署 首先需要部署好ChatGLM-6B,参考 ChatGLM-6B中英双…...

虚拟机配置静态IP地址(人狠话不多简单粗暴)

1.先找到以下位置&#xff1a; 2. 虚拟机中执行vi /etc/sysconfig/network-scripts/ifcfg-ens33 根据上图信息修改配置文件内容&#xff1a; 静态IP地址设置不超过255就行&#xff0c;我这里弄得100&#xff0c;没毛病。 3.修改并保存文件后&#xff0c;重启网络执行&#…...

BetterGI 0.38.1版本安装失败?3步快速解决原神自动化工具启动问题

BetterGI 0.38.1版本安装失败&#xff1f;3步快速解决原神自动化工具启动问题 【免费下载链接】better-genshin-impact &#x1f368;BetterGI 更好的原神 - 自动拾取 | 自动剧情 | 全自动钓鱼(AI) | 全自动七圣召唤 | 自动伐木 | 自动派遣 | 一键强化 - UI Automation Testin…...

SDMatte模型推理性能剖析:使用Profiling工具定位计算瓶颈

SDMatte模型推理性能剖析&#xff1a;使用Profiling工具定位计算瓶颈 1. 为什么需要性能剖析 做AI模型推理优化就像修车一样&#xff0c;你得先知道哪里出了问题才能对症下药。SDMatte作为一款专业的图像抠图模型&#xff0c;在实际部署中经常会遇到推理速度慢、资源占用高等…...

家庭实验室:树莓派控制OpenClaw调用远程Qwen3-32B

家庭实验室&#xff1a;树莓派控制OpenClaw调用远程Qwen3-32B 1. 为什么选择树莓派OpenClaw组合 去年冬天&#xff0c;我在整理家庭实验室设备时发现一个闲置的树莓派4B。这台信用卡大小的电脑曾经用来跑Home Assistant控制智能家居&#xff0c;但后来换了NUC主机就被束之高阁…...

避坑指南:Python操作Word文档最常见的5个错误(python-docx实战心得)

Python-docx实战避坑指南&#xff1a;5个高频错误与解决方案 在自动化办公场景中&#xff0c;Python操作Word文档的需求日益增长&#xff0c;而python-docx库作为主流工具&#xff0c;其易用性背后隐藏着不少"暗礁"。许多开发者在基础教程阶段一帆风顺&#xff0c;却…...

量子行走:从理论到Python实现——3. 量子门、电路与编程基础

目录 3. 量子门、电路与编程基础 3.1 单量子比特门 3.1.1 泡利门与旋转门 3.1.2 哈达玛门与相位门 3.2 多量子比特门 3.2.1 受控门 3.2.2 纠缠门与SWAP操作 3.3 量子电路构建与优化 3.3.1 电路表示与DAG结构 3.3.2 变分电路 3. 量子门、电路与编程基础 量子计算体系的…...

S3 文件操作进阶实践:从基础上传到完整性保障

1. S3文件操作的核心挑战与解决方案 第一次接触AWS S3时&#xff0c;很多人会觉得文件上传下载不就是调用几个API的事&#xff1f;但真正投入生产环境后&#xff0c;各种问题就会接踵而至。我见过最典型的案例是某电商平台在促销期间&#xff0c;因为文件上传没有做完整性校验…...

学术风控新范式:陌讯 AIGC 检测论文 AI 代写识别技术详解

摘要&#xff1a;随着生成式人工智能&#xff08;AIGC&#xff09;技术的爆发式迭代&#xff0c;GPT-4、文心一言等大模型已能生成逻辑连贯、格式规范的学术论文&#xff0c;AI代写、AI润色过度等学术不端行为呈现隐蔽化、规模化趋势&#xff0c;传统查重工具难以应对这一新型学…...

SAP--S4/HANA

1、Webdispatcher 2、ASCS 全称&#xff1a;ABAP Central Services Instance&#xff08;在 Java 栈中称为 SCS - Java Central Services&#xff09;。 核心功能&#xff1a;它是 SAP 系统的“大脑”或控制中心&#xff0c;不包含处理具体业务对话&#xff08;Dialog&#xff…...

STM32实现智能酒驾监测系统设计

基于STM32的酒后驾车监测报警系统设计与实现1. 项目概述1.1 系统背景酒后驾车是全球交通事故的主要诱因之一&#xff0c;传统的人工检测方法存在效率低、覆盖范围有限等问题。随着嵌入式系统和物联网技术的发展&#xff0c;智能化的酒精监测系统成为解决这一问题的有效方案。1.…...

Linux DRM子系统深度解析:如何为240x240 SPI屏编写自定义KMS驱动?

Linux DRM子系统实战&#xff1a;为240x240 SPI屏构建原子化KMS驱动 当一块小巧的240x240 SPI屏幕遇上Linux DRM显示框架&#xff0c;开发者面临的不仅是硬件接口的适配&#xff0c;更是一场关于现代显示架构的深度对话。本文将带您穿透DRM子系统的抽象层&#xff0c;从KMS核心…...