天津工业大学
毕业设计(论文)
题目:汽车速度及里程电子显示系统设计
姓 名 刘志刚 学 院 机械工程学院 专 业 测控技术与仪器 指导教师 李雅峰 职 称 讲 师
2012年6月1日
天津工业大学毕业设计(论文)任务书 题目 学生姓名 课题类型 刘志刚 汽车速度及里程电子显示系统设计 学院名称 机械工程学院 设计类 专业班级 测控082 汽车是现代生活中不可或缺的一种重要交通工具,传统的指针式里程表伴随着汽车的诞生就一直为人们喜爱,不过,新生事物不会因传统的存在而停止它前进的步伐。数码科技在今天已渗透到工业,农业,民课题意义 用等产品的点点滴滴。新概念的车速里程表最直观的变化就是用大屏幕的液晶取代指针式表盘,直接用数字显示速度和里程,以及其他一些诸如油耗、时钟、环境温度等参数,直观的呈现给使用者。同时,它还具有成本低廉,显示清晰,稳定可靠等优点。 1~3周:资料调研 4~5周:分析毕业设计课题,进行总体设计 6~9周:进行具体的相关设计 任务与进10周:中期检查 度要求 10~13周:系统具体设计 14周:撰写毕业设计论文 15周:毕业设计论文修改 16周:答辩 [1] 赵负图.现代传感器集成电路[M].北京:人民邮电出版社, 2000.534-535. [2] 张毅坤 ,陈善久 ,裘雪红.单片微型计算机原理及应用[M].西安:西安电子科技大学出版社,1998年. [3] 全润,张亚凡,邓洪敏.传感器原理及应用[M].北京:清华大学出版主要参考文献 社,2008年 [4] 冯寿亭,李迪.基于嵌入式计算机和DSP的数控系统以及其通信实现[J].组合机床与自动化,2005 [5] 来清民主编.传感器与单片机接口及实例[M].北京航空航天大学出版社. [6] 何希才.传感器及其应用[M].北京:国防工业出版社,2001· [7] 万福君 凌文玉 王乃厚等.单片微机原理系统设计与开发应用[M].合肥:中国科学技术大学出版社,1995.46-52. 起止日期 备注 2012.03.01 至2012.06.06 院长 教研室主任 指导教师
毕业设计(论文)开题报告表
2012年3月10日 机械工程测控技术与仪姓名 刘志刚 学院 专业 班级 测控082 学院 器 题目 汽车速度及里程电子显示系统设计 指导教师 李雅峰 一、 与本课题有关的国内外研究情况、课题研究的主要内容、目的和意义: 概述传统的转速里程表的功能有两个,一是用指针指示汽车行驶的瞬时车速,二是用机械计数器记录汽车行驶的累计里程。 现在的汽车仪表自动化程度很高,老式的机械软轴传动的仪表已经淘汰取而代之的是现在的液晶仪表盘,不可能再通过拨数码轮调整里程表。司机又需要增加里程数时,用这种跑表器就可以很轻松地实现。这种跑表器使用中规模集成电路设计而成,体积小巧,运行稳定,适用于各种电子传感器的汽车上使用。 这种小巧的仪器有着比其它同类产品明显的优点: 1、电路设计精湛,严密。使用了高性能的集成电路和电子元件,保证电路有高度的品质;使用最优化的电路设计,考虑到尽可能多的不利的使用环境,使仪器能在电压、温度、湿度、灰尘污染等复杂的变化因素多的条件下都能稳定工作。 2、可靠性好。为了提高电路工作的可靠性,不但元件经过精心筛选,而且经过2400个小时的烤机试验,它的外壳小巧,可以方便地装在口袋中,随时可以拿出来使用。 3、操作简单,使用方便。只需要插到点烟器上,不用下车,一切OK。4、使用安全,放心。即使接反,也不会损坏仪器,更不会损坏车上任何东西. 5、速度可以调整。你可以把速度调整到最高,也可以调整到一个固定值,比如300迈. 依据车型不同,速度有所不同,可以通过调整仪器上速度旋钮改变速度 二、进度及预期结果: 起止日期 3月1日 至 6月06日 完成课题的现有条件 审查意见 主要内容 1~3周:资料调研 4~5周:分析毕业设计课题,进行总体设计 6~9周:进行具体的相关设计 10周:中期检查 10~13周:系统具体设计 14周:撰写毕业设计论文 15周:毕业设计论文修改 16周:答辩 预期结果 完成开题报告 完成合理的方案设计 完成中期检查 完成设计 基本完成论文的撰写 修改并完成论文 学校图书馆查阅的资料、上网查找的资料、指导老师提供的资料以及现场的实地参观学习。 指导教师: 年 月 日 主管领导: 年 月 日
学院意见
毕业设计 题目 学生姓名 评审项目 天津工业大学本科毕业设计(论文)评阅表
(设计类) 汽车速度及里程电子显示系统设计 刘志刚 学生班级 测控082 指标 能体现本专业培养目标,题目大小、难度适中;学生工指导教师姓名 李雅峰 满分 10 10 评分 选题 作量饱满,能得到较全面训练。 题目与生产、科研等实际问题结合紧密。 能查阅文献以及从事其它形式的调研,能较好地理课题调研 文献检索 外文应用 解课题任务并提出实施方案;有分析整理各类信息从中获取新知识的能力。 能正确引用外文文献,翻译准确,文字流畅。 设计图纸(插图)简洁、规范、无差错,设计栏目齐全合理,能正确使用国家标准单位。 设计说明书(论文)结构严谨,表达清楚,文字通顺,15 5 15 15 20 10 100 设计说明用语正确,基本无错别字和病句,书写格式符合规范。 书(论文) 能根据毕业设计目标进行实验设计,对数据的运算及处理正确无差错,对实验结果的分析准确。 设计具有创新性或实用价值。 合计 意见及建议 评阅人签名:
年 月 日 天津工业大学毕业设计(论文)成绩考核表 学生姓名 题目 刘志刚 学院名称 机械工程学院 专业班级 测控082 汽车速度及里程电子显示系统设计 1.毕业设计(论文)指导教师评语及成绩: 指导教师签字: 年 月 日 2.毕业设计(论文)答辩委员会评语及成绩: 成绩: 答辩(或组长)签字: 年 月 日 3.毕业设计(论文)总成绩: 成绩: a.指导教师 给定成绩 b.评阅教师 给定成绩 c.毕业答辩成绩 总成绩 (a×0.5+b×0.2+c×0.3)
摘 要
里程表应用广泛,机械式里程表有其不足之处。设计智能自动化的里程表,利用霍尔效应原理,在电机中嵌入霍尔传感器,采用脉冲检测的方法实现测量行驶速度与里程,使用倍频电路提高系统的测量精度。可以实现超速报警及用户自设定初始上限速度,既可以实现测速也可以利用变频器来控制电机转速,实现了自动化和智能化。
本文介绍了霍尔传感器测速的原理,设计了基于单片机STCC52RC的汽车速度及里程的电子显示系统。完成了汽车速度及里程测量系统的硬件电路设计、霍尔传感器测量电路的设计、显示电路的设计。机轴每转一周,产生一定量的脉冲个数,由霍尔器件电路部分输出幅度为12V的脉冲。经光电隔离器后成为输出幅度为5V转数计数器的计数脉冲。在显示电路设计中,通过LCD1602实现在液晶上直观地显示汽车的速度及里程的数值。并对该系统的硬件电路、显示电路进行了调试。与软件配合,实现了显示、报警功能。仿真实验表明所设计的硬件电路及软件程序是正确的,满足设计要求。
关键词:脉冲检测;霍尔传感器;单片机;STCC52RC;LCD
ABSTRACT
Intelligent odometer is designed using hall effect to avoid the defect of mechanical odometer. The speed and mileage are measured by the hall sensor by method of pulse detection. The double frequency circuit used can improve the precision of the system. User interface of the sysem is designed. It can automatic alarm if the speed exceeds the limit. The max speed can be setup by the user. The speed can be measured as well as controled in the system.
This article describes principles of Hall sensor speed, STCC52RC design based on single-chip electronic display system of vehicle speed and mileage. Completed vehicle mileage and speed measurement system of hardware circuit design, circuit design, Hall-effect sensor measurement design of display circuit. Axis every week, have a certain number of pulses, consists of Hall device circuit parts 12V pulse output range. Optical isolators as outputs over 5V revolution counter counting pulses. In the design of display circuit, in via LCD1602 on liquid crystal Visual display of speed and mileage of the car's value. Hardware circuit, display circuit and the system for debugging. With the software, enables display and alarm function. Simulation experiments show that the design of hardware circuit and software programs correctly, meet design requirements.
Keywords: pulse detection; Hall sensors; Single chip microcomputer; STCC52RC; LCD
目 录
第一章 绪论............................................................................................. 1
1.1 立题的目的和意义 ............................................................................................. 1 1.2 应用与发展前景 ................................................................................................. 1 1.3 设计任务与要求 ................................................................................................. 2 1.3.1 设计任务....................................................................................................... 2 1.3.2 设计要求....................................................................................................... 2 1.4 小结 ..................................................................................................................... 2
第二章 课题方案设计 ............................................................................ 3
2.1 系统总体设计要求 ............................................................................................. 3 2.2 系统模块结构的选择 ......................................................................................... 3 2.2.1 霍尔测速模块选择....................................................................................... 3 2.2.2 计数器模块选择........................................................................................... 3 2.2.3 显示模块选择............................................................................................... 3 2.2.4 报警模块选择............................................................................................... 3 2.2.5 电源模块选择............................................................................................... 3 2.2.6 单片机模块选择........................................................................................... 4 2.3 速度测量方案选择 ............................................................................................. 4 2.4 小结 ..................................................................................................................... 4
第三章 系统总体设计 ............................................................................ 5
3.1 总体硬件设计 ..................................................................................................... 5 3.1.1 硬件原理图................................................................................................... 5 3.1.2 硬件电路设计总图....................................................................................... 5 3.2 系统子模块简介 ................................................................................................. 6 3.2.1 传感器部分................................................................................................... 6 3.2.3 处理器........................................................................................................... 7 3.2.4 LCD显示部分............................................................................................... 8 3.2.5 外接报警部分............................................................................................... 8
第四章 软件设计 .................................................................................... 9
4.1 程序设计步骤 .......................................................................................... 9 4.2 程序流程图 ......................................................................................................... 9 4.2.1 主程序流程图............................................................................................... 9 4.2.2 中断服务流程图......................................................................................... 11 4.3 软件程序设计 ................................................................................................... 12 4.3.1 主程序设计................................................................................................. 12 4.3.2 中断服务程序设计..................................................................................... 14 4.3.3 显示程序设计............................................................................................. 14 4.3.4 报警程序设计............................................................................................. 16 4.3.5 转速程序的设计......................................................................................... 16 4.3.6 软件程序基础知识准备............................................................................. 17
第五章 软件调试 .................................................................................. 18
5.1 PROTEUS及KEIL软件简介 ............................................................................... 18 5.1.1 Proteus软件................................................................................................. 18 5.1.2 Keil软件 ...................................................................................................... 18 5.2 应用KEIL软件进行程序调试 .......................................................................... 18 5.3 PROTEUS软件仿真 ............................................................................................. 19 5.3.1仿真步骤...................................................................................................... 19 5.3.2仿真实例...................................................................................................... 19 5.4 硬件软件联合调试 ........................................................................................... 22 5.4.1 联调步骤..................................................................................................... 22 5.4.2 搭接检查步骤............................................................................................. 22
第六章 结论........................................................................................... 24
参考文献 ..................................................................................................... 25 附录1 源程序 ........................................................................................... 26 附录2 硬件实物图 .................................................................................... 30 外文文献 ..................................................................................................... 30 外文翻译 ..................................................................................................... 57
谢 辞 ........................................................................................................ 80
天津工业大学2012届本科生天津工业大学毕业设计(论文)
第一章 绪论
汽车是现代生活中不可或缺的一种重要交通工具,传统的指针式的里程表伴随着汽车的诞生就一直为人们接受,不过,新生事物不会因传统的存在而停止它前进的步伐,数码科技在今天已渗透到工业,农业,民用等产品的点点滴滴。新概念的车速里程表最直观的变化就是用大屏幕的液晶取代指针式表盘,直接用数字显示时速,里程,以及其他一些诸如油耗、时钟、环境温度等参数。直观的呈现给使用者。
由于单片机体积小,可以把它做到产品的内部,取代老式机械零件,缩小产品体积,增强功能,实现智能化。因此广泛的被用在智能产品中。
本文先对里程表设计中所需设备作了详细介绍,对设计中存在的问题进行了说明;对硬件和软件部分的设计和实现作了认真的分析;给出了系统的建模过程及相应的系统模型,并对仿真结果进行了分析。
1.1 立题的目的和意义
霍尔传感器是利用霍尔效应实现磁电转换的一种传感器,它具有灵敏度高、线性度好、稳定性高、体积小和耐高温等特点,在机车控制系统中占有非常重要的地位。
测速装置在控制系统中占有非常重要的地位,对测速装置的要求是分辨能力强、高精度和尽可能短的检测时间。在此主要介绍应用霍尔传感器通过测量磁场强度,来得到稳定的脉冲方波信号,实现电机转速的测量转速是电动机极为重要的一个状态参数,在很多运动系统的测控中,都需要对电机的转速进行测量,速度测量的精度直接影响系统的控制情况,它是关系测控效果的一个重要因素。不论是直流调速系统还是交流调速系统,只有转速的高精度检测才能得到高精度的控制系统。
1.2 应用与发展前景
随着微型计算机可靠性提高和价格的下降,用单片机测量电机转速已日趋普遍。我们知道,欲提高测量精度,必须先测出准确的转速,而原先在可控硅调速电路中采用的测速发电机方式已不能满足要求,必须采用数字测速的方法。转速的测量方法很多,根据脉冲计数来实现转速测量的方法主要有M法(测频法)、T法(测周期法)和MPT法(频率周期法),该系统采用了M法(测频法)。转速检测方式采用霍尔脉冲法测量转速有两种简单的方式。本文采用频率法,检测的是输入脉冲数,这种方式又称频率法。它测出一定时间内输入的脉冲的个数。
在控制系统中占有非常重要的地位。对测速装置的要求是分辨能力强、高精度和尽可能短的检测时间。所设计的基于霍尔元件的脉冲发生器要求成本低,构
- 1 -
天津工业大学2012届本科生天津工业大学毕业设计(论文)
造简单,性能好。在电气控制系统中存在着较为恶劣的电磁环境,因此要求产品本身要具有较强的抗干扰能力。
由于需要采用霍尔传感器的应用领域,如汽车、电机、手机和电脑都已经采用了该器件,而且这些市场在未来几年的增长较为稳定,而其他一些新的应用市场又不足以与上述几个市场相比,因此霍尔传感器在全球总的市场容量是较为稳定的,每年的增长率保持在5%到10%之间。因为各种应用电机的部件、节气门位置的检测、各种阀体位置的检测都会用到霍尔传感器。而且,在中国市场中,国外厂商为了降低成本,陆续将零部件拿到中国进行设计和生产,这也进一步提升了中国市场霍尔传感器的应用量。
随着它在消费电子市场上的应用越来越广,如何控制功耗和成本将是厂商面临的挑战。而且,它还面临生产测试技术方面的挑战。
1.3 设计任务与要求 1.3.1 设计任务
根据学校毕业设计的要求,设计一个功能满足设计要求、工作稳定、以单片机为核心的汽车速度及里程表显示系统,由于种种原因不能真正把设计放到汽车上模拟,所以只能够用简单的电机代替。能够实现在电机工作时转速的测量,并在发生故障时能及时的发出报警信号。本设计包括完整的硬件设计和相应的软件设计。
1.3.2 设计要求
根据学校毕业设计的要求,设计一个功能满足设计要求、工作稳定、以单片机为核心的测量速度和里程的显示系统。本设计要求做一个单片机最小系统,能够实现在电机工作时转速的测量,同时选用合适的显示器件,使系统具备实时显示功能,并在发生故障时能及时的发出报警信号。根据题目的要求,设计了以下方案并对各方案进行了论证与分析。本设计包括完整的硬件设计和相应的软件设计。
1.4 小结
本章主要对本设计的立题目的及意义进行了介绍并且对设计的要求进行了总结 。
- 2 -
天津工业大学2012届本科生天津工业大学毕业设计(论文)
第二章 课题方案设计
2.1 系统总体设计要求
设计一个功能满足设计要求、工作稳定、以单片机为核心的测量转速和里程的显示系统。
2.2 系统模块结构的选择 2.2.1 霍尔测速模块选择
采用霍尔元件传感器即霍尔片;霍尔片可分为贴片型和直插型。由于贴片型不常用,因此选择直插型。选型号为A3144的霍尔片作为霍尔测速模块的核心,该霍尔片体积小,安装灵活,可用于测速,且与普通的磁钢片配套使用,价格一般为2.5~3元。
2.2.2 计数器模块选择
可以采用片外计数器和片内计数器两个方案。片外计数器的方案是指采用8253等片外的专用计数芯片进行脉冲计数。片内计数方案是指采用单片机的内部计数器完成对脉冲的计数过程。
使用片内的计数器的优点在于降低单片机系统的成本。每到一个脉冲将会产生一个T1的计数,T1的中断溢出次数就是所需要计的脉冲数。特点在于:使用了内部的T1作为外部脉冲的计数器,并且为了避免计数器的溢出,将T1的初值设为0。
2.2.3 显示模块选择
数码管显示器件相对便宜,但是耗能大、编写程序相对麻烦,工作量大。 LCD显示器工作原理简单,编程方便,节能环保。所以采用LCD1602液晶显示器作为显示模块核心。
2.2.4 报警模块选择
采用电磁式蜂鸣器作为报警主要器件。该方案不论在硬件和焊接方面还是在编写软件方面都简单方便,而且成本低廉。
2.2.5 电源模块选择
采用USB供电。该方案实施简单,电路搭建方便,可作为单片机开发常备电源使用。
- 3 -
天津工业大学2012届本科生天津工业大学毕业设计(论文)
2.2.6 单片机模块选择
选用 STCC52RC的单片机速度极快、功耗低、体积小、资源丰富,有各种不同的规格,引脚还可编程确定功能
因为本次设计的目标是测速系统的应用,所以我选用了51系列的单片机,因为51的架构十分典型。选择51系列单片机我认为主要考虑以下方面:1.价格便宜;2.开发简单;3.自己动手焊接相对容易。
2.3 速度测量方案选择
转速的测量方法很多,根据脉冲计数来实现转速测量的方法主要有M法(测频法)、T法(测周期法)和MPT法(频率周期法),该系统采用了M法(测频法)。由于转速是以单位时间内转数来衡量,在变换过程中多数是有规律的重复运动。
传感器采用霍尔器件将电机的转速转化为脉冲信号,处理器采用STCC52RC单片机。计数器采用单片机片内计数器完成对脉冲的计数,显示器采用字符型液晶显示器LCD1602进行显示。
系统工作过程:机轴上安装一个一个圆盘,圆盘的边缘安有一个磁钢,测量转速的霍尔传感器靠近边缘的磁钢,机轴每转一周,产生一定的脉冲个数,霍尔器件电路部分输出,成为转速计数器的计数脉冲。控制计数时间,即可实现计数器的计数值对应机轴的转速值。单片机CPU将数据处理后,通过LCD1602显示出来。测量转速传感器由磁钢、霍尔元件组成。将一非磁性圆盘固定装在电机转轴上,再用502胶水将圆块状磁钢黏贴在圆盘靠近边缘的位置,磁钢采用永久磁。
图2-1 霍尔传感器检测信号图
2.4 小结
本章通过总体设计进行对方案选择的最终确定,研究了霍尔测速、计数器、显示、报警、电源、单片机等各部分模块的可行性方案。
- 4 -
天津工业大学2012届本科生天津工业大学毕业设计(论文)
第三章 系统总体设计
3.1 总体硬件设计
汽车转速及里程表显示系统工作过程是:机轴每转一周,产生一定量的脉冲个数,由霍尔器件电路部分输出。经光电耦合后,成为转速计数器的计数脉冲。同时传感器电路输出幅度为12v的脉冲经光电耦合后降为5v,保持同STCC52RC逻辑电平相一致。控制计数时间,即可实现计数器的计数值对应机轴的转速值。CPU将该值数据处理后,在LCD1602上显示出来。一旦超速,CPU通过蜂鸣器发出声音报警信号。
3.1.1 硬件原理图
以单片机STCC52RC为控制核心,用霍尔集成传感器作为测量转速的检测元件,最后用字符型液晶显示器LCD1602显示,用小型直流电机模拟汽车显示转速和里程的显示系统,这是一种数字式测量方法。系统硬件原理图如图3-1所示。
电机霍尔传感器声音报警 光电耦合计数器单片机LCD 显 示 器 3.1.2 硬件电路设计总图
在原理图基础上对各部分进行了详细的设计,硬件电路图如图3-2所示。
图3-1 硬件原理图
- 5 -
天津工业大学2012届本科生天津工业大学毕业设计(论文)
图3-2 硬件电路图
3.2 系统子模块简介
本文介绍一种STCC52RC单片机测量汽车速度及里程的方法。系统以单片机STCC52RC为控制核心。用霍尔集成传感器作为测量汽车速度及里程的方法检测元件,经过单片机数据处理,用字符型液晶显示器LCD1602显示汽车速度及里程。另外系统还可完成对电机的开关控制。组成单片机速度及里程测量系统的有传感器、处理器、计数器和显示器四个部分组成。
3.2.1 传感器部分
主要分为两个部分。第一部分是利用霍尔器件将电机转速转化为脉冲信号;第二个部分是使用光耦,将传感器输出的信号和单片机的计数电路两个部分隔开,减少计数的干扰。
用于测量的A3144集成霍尔开关,磁钢用直径D=5mm,长度为L=3mm的磁钢。A3144是由稳压电源,霍尔电压发生器,差分放大器,施密特触发器和输出放大器组成的磁敏传感电路,其输入为磁感应强度,输出是一个数字电压讯号。
- 6 -
天津工业大学2012届本科生天津工业大学毕业设计(论文) 它是一种单磁极工作的磁敏电路,适合于矩形或者柱形磁体下工作。可应用于汽车工业和军事工程中。管脚图和接线图如3-3所示。 (a)霍尔片管脚 (b)管脚接线 图3-3 霍尔片管脚图和管脚接线图 3.2.2 计数器 片内计数方案是指采用单片机的内部计数器完成对脉冲的计数过程。 3.2.3 处理器 处理器是单片机,采用的是STCC52RC单片机。STCC52RC是一种带4K字节闪烁可编程可擦除只读存储器(FPEROM—Falsh Programmable and Erasable Read Only Memory)的低电压、高性能的微处理器,俗称单片机。其引脚图如图3-4所示。 STCC52RC具有如下功能特性: (1)兼容MCS—51指令系统; (2)32个双向I/O口; (3)两个16位可编程定时/计数器; (4)1个串行中断; (5)两个外部中断源; (6)4k可反复擦写(>1000次)Flash ROM; (7)128x8bit内部RAM; (8)6个中断源; (9)低功耗空闲和掉电模式; (10)软件设置睡眠和唤醒功能。 图3-4 STCC52RC的引脚图 - 7 -
天津工业大学2012届本科生天津工业大学毕业设计(论文)
3.2.4 LCD显示部分
选用LCD1602。显示部分有两个功能,在正常的情况下,通过LCD显示当前的汽车速度,当汽车速度超出一定的范围后,通过蜂鸣器进行报警。实物图如图3-4所示。
图3-5 LCD1602液晶显示实物图
3.2.5 外接报警部分
在单片机应用的设计上,很多方案都会用到蜂鸣器,大部分都是使用蜂鸣器来做提示或报警。用I/O定时翻转电平来产生驱动波形的方式会比较麻烦一点,必须利用定时器来做定时,通过定时翻转电平产生符合蜂鸣器要求的频率的波形,这个波形就可以用来驱动蜂鸣器了。比如为2500Hz的蜂鸣器的驱动,可以知道周期为400μs,这样只需要驱动蜂鸣器的I/O口每200μs翻转一次电平就可以产生一个频率为2500Hz,占空比为1/2duty的方波,再通过三极管放大就可以驱动这个蜂鸣器了。由于蜂鸣器的工作电流一般比较大,以致于单片机的I/O口是无法直接驱动的,所以要利用放大电路来驱动,一般使用三极管来放大电流就可以了。电磁式蜂鸣器实物图如3-6所示。
图3-6 电磁式蜂鸣器
- 8 -
天津工业大学2012届本科生天津工业大学毕业设计(论文)
第四章 软件设计
4.1 程序设计步骤
第一步分析问题,明确任务要求,对于复杂的问题,还要讲要解决的问题抽象成数学模型,即用数学表达式来描述。
第二步确定算法,即根据实际问题和指令系统的特点确定完成这一任务须经历的步骤。
第三步根据所选择的算法,确定内存单元的分配:使用那些寄存器:程序运行中的中间数据及结果存放在那些单元,以利于提高程序的效率和运行速度:然后制定出解决问题的步骤和顺序,画出程序的流程图。
第四步根据流程图,编写源程序。 第五步上机对原程序进行编译、调试。
4.2 程序流程图
电机转速测量需要经过的4个基本步骤:1是控制方式;2是确定计数方式;3是信号输入方式;4是计数值的读取;通过STCC52RC,单片机完成对电机转速脉冲计数的控制,读取寄存器完成转速频率的确定。电机脉冲信号连到INT0引脚。
本系统采用STCC52RC中的INT0中断对转速脉冲计数。定时器T0工作于定时方式,工作于方式1。每到1s读一次外部中断INT0计数值,此值即为脉冲信号的频率,根据式(4-1)可计算出电机的转速。
当直流电机通过传动部分带圆盘旋转时,霍尔传感器根据圆盘上得磁片获得一系列脉冲信号。这些脉冲信号通过单片机系统定时/计数器INT0计数,定时器T0定时。定时器T0完成100次溢出中断的时间T除以测得的脉冲数m,经过单位换算,就可以算得直流电机旋转的速度。
汽车速度计算公式:
n=2·m/(N1·T·N)(m/sec) (4-1)
其中:n为直流电机转速,N为磁钢数,N1为T0中断次数,m为INT0在规定时间内测得的脉冲数,T为定时器T0定时溢出时间,2为假定的汽车轮子的周长。
4.2.1 主程序流程图
主程序工作过程如下。
- 9 -
天津工业大学2012届本科生天津工业大学毕业设计(论文)
先进行初始化设置各定时器初值,然后判断是否启动系统进行测量。如果是,就启动系统运行。如果不是就等待启动。启动系统后,霍尔传感器检测脉冲到来后,启动外部中断,每来一个脉冲中断一次,记录脉冲个数。同时启动T0定时器工作,每1秒定时中断一次,读取记录的脉冲个数,即电机转速。再进行数值的判断,若数值高于80m/sec则报警并返回初始化阶段,否则就进行正常速度液晶显示。
开始 初始化 是否启动 等待中断 数据滤波处理 是否达到报警器的值 显示处理
图4-1 主流程图
- 10 -
天津工业大学2012届本科生天津工业大学毕业设计(论文)
4.2.2 中断服务流程图
在处于中断服务程序阶段,首先进行关中断设置。其次进行对INT0位进行的脉冲个数计数的数值读取。再次对INT0、T0进行赋初值并且进行关中断设置。最后进行中断返回。 一、外部计数中断
关闭中断 计数器+1 判断是否为1次,1次则为电机转一圈 初始化计数器 转圈计数器+1 开外部中断INT0 返回
图4-2 外部中断流程图
- 11 -
天津工业大学2012届本科生天津工业大学毕业设计(论文)
二、定时器中断
关T0中断 重新装入计时数值 计时数值+1 判断是否为20次,20次则为1秒 关INT0中断,把转圈数赋值给数据处理参数 LCD显示 开中断 返回
图4-3 定时器中断流程图
4.3 软件程序设计 4.3.1 主程序设计
主程序在对定时器、计数器、堆栈等进行初始化后即判断标志位是否为1,如果为1,说明要求对数据进行计算处理,首先将标志位清零,以保证下次能正常判断,然后进入数据处理程序,由于这里的闸门时间为1s,由于转轴上安装
- 12 -
天津工业大学2012届本科生天津工业大学毕业设计(论文)
有1只磁钢,每旋转一周可以得到1个脉冲,可得到每秒钟的转速。所得数据乘以汽车车轮的周长即可得到当前汽车的速度。计算得到的结果是二进制的整数,要将数据送往显示缓冲区需要将该数转化为BCD码。运算得到的是压缩BCD码,需要将其转换为非压缩BCD码,从标号CBCD开始的一段程序即作了这样的处理。
定时器T0用作20ms定时发生器,在定时中断程序中进行数码管的动态扫描,同时产生1s的闸门信号。1s闸门信号的产生是通过一个计数器Count,每次中断时间为20ms,每计50次即为1s,到了1s后,即清除计数器Count,然后关闭作为计数器用的INT0,读出TH0、TL0中的数值,分别送入SpCount和SpCount+1单元,将T0中的值清空,置标志位为1,要求主程序进行速度值的计算。这里还有一个细节,用作1s闸门信号产生的Count每次中断都会加1,而INT0却有一个周期是被关闭的,因此,计数值是51而不是50。
/*------------------------主函数-------------------------*/ void main() { }
int_all();//全局初始化 { }
disp_count();//数据处理 if(zhuan>=80) //转速警告 { }
if(zhuan<80) { }
write_command(0x80); for (i=0;i while(1) - 13 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 4.3.2 中断服务程序设计 一、外部计数中断 /*-------------------外部中断0计数程序-------------------*/ void counter(void) interrupt 0 { EX1=0; //关外部中断0 z++; //转圈计数加1 EX1=1; //开外部中断0 } 二、定时器中断 /*-----------------内部中断0计时计数程序-----------------*/ void Timer_0(void) interrupt 1 { TH0=0x65; //50ms定时 TL0=0xF3; msec++; if(msec==20) //50*20=1S { zhuan=z; x+=z; disp_count(); //数据处理 displaytolcd(); msec=0; z=0; } } 4.3.3 显示程序设计 液晶显示模块是一个慢显示器件,所以在执行每条指令之前一定要确认模块的忙标志为低电平,表示不忙,否则此指令失效。要显示字符时要先输入显示字符地址,也就是告诉模块在哪个位置显示字符。 在对液晶模块的初始化中要先设置其显示模式,在液晶模块显示字符时光标是自动右移的,无需人工干预。每次输入指令前都要判断液晶模块是否处于忙的状态。 向LCD输入的数据有两种,一种是指令,一种是数据。指令是负责初始化LCD与LCD显示字符是什么位置。命令与数据是RS端的高低电平来确定。数据开始的时候是由LCDCS高电平开始,低电平结束。 - 14 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 显示子程序 向LCD1602写命令 向LCD1602写数据 结束显示 图4-4 显示流程图 /*--------------------向LCD1602写命令--------------------*/ void write_command(uchar command) { } /*-------------------------------------------------------*/ /*--------------------向LCD1602写数据--------------------*/ void write_data(uchar data0) { } - 15 - rs=0; //选择写命令 P0=command; //向LCD写命令 lcdcs=1; //信号使能端高电平 lcdcs=0; //信号使能端低电平 rs=1; //选择写数据 P0=data0; //向LCD写数据 lcdcs=1; //信号使能端高电平 lcdcs=0; //信号使能端低电平 天津工业大学2012届本科生天津工业大学毕业设计(论文) 4.3.4 报警程序设计 蜂鸣器发声原理是电流通过电磁线圈,使电磁线圈产生磁场来驱动振动膜发声的,因此需要一定的电流才能驱动它,单片机IO引脚输出的电流较小,单片机输出的TTL电平基本上驱动不了蜂鸣器,因此需要增加一个电流放大的电路。 蜂鸣器程序设计思路:本程序通过在输出一个音频范围的方波,驱动实验板上的蜂鸣器发出蜂鸣声,其中Delay延时子程序的作用是使输出的方波频率在人耳朵听觉能力之内的20KHZ以下,如果没有这个延时程序的话,输出的频率将大大超出人耳朵的听觉能力,我们将不能听到声音。更改延时常数,可以改变输出频率,也就可以调整蜂鸣器的音调。 代码为: if(zhuan>=80) { } if(zhuan<80) { } warning=0; warning=1; 4.3.5 转速程序的设计 测速的方法决定了测速信号的硬件连接,测速实际上就是测频,因此,频率测量的一些原则同样适用于测速。 通常,可以用计数法、测脉宽法和等精度法来进行测试。所谓计数法,就是给定一个闸门时间,在闸门时间内计数输入的脉冲个数;测脉宽法是利用待测信号的脉宽来控制计数门,对一个高精度的高频计数信号进行计数。由于闸门与被测信号不能同步,因此,这两种方法都存在±1误差的问题,第一种方法适用于信号频率高时使用,第二种方法则在信号频率低时使用。等精度法则对高、低频信号都有很好的适应性。 /*-----------------------数据处理------------------------*/ void disp_count() { unsigned long long_x; zhuan=zhuan*2; display[7]=(zhuan/1000+'0'); //转换转速的千位 display[8]=(zhuan/100%10+'0'); //转换转速的百位 display[9]=(zhuan/10%10+'0'); //转换转速的十位 display[10]=(zhuan%10+'0'); //转换转速的个位 long_x=x*2; - 16 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) } display_up[6]=(long_x/100000000+'0'); //里程的亿位 display_up[7]=(long_x/10000000%10+'0'); //里程的千万位 display_up[8]=(long_x/1000000%10+'0'); //里程的百万位 display_up[9]=(long_x/100000%10+'0'); //里程的十万位 display_up[10]=(long_x/10000%10+'0'); //里程的万位 display_up[11]=(long_x/1000%10+'0'); //里程的千位 display_up[12]=(long_x/100%10+'0'); //里程的百位 display_up[13]=(long_x/10%10+'0'); //里程的十位 display_up[14]=(long_x%10+'0'); //里程的个位 4.3.6 软件程序基础知识准备 针对STCC52RC单片机,头文件STCx52.h给出了特殊功能寄存器SFR所有端口的定义。其次,C语言编程基础: 十六进制表示字节0x5a:二进制为01011010B;0x6E为01101110。 如果将一个16位二进数赋给一个8位的字节变量,则自动截断为低8位而丢掉高8位。 TMOD=(TMOD&0xf0)|0x05;表示给变量TMOD的低四位赋值0x5,而不改变TMOD的高四位。 While(1);表示无限执行该语句,即死循环。语句后的分号表示空循环体,也就是{;} 在引脚输出方波编程方法:(比如P3.2引脚) #include//该头文档中有单片机内部资源的符号化定义,其中包含P3.2// void main(void)//void表示没有输入参数,也没有函数返值,这入单片机运行的复位入口// { While(1)//非零表示真,如果为真则执行下面循环体的语句// { P3_2=1;//给P3_2赋值1,引脚P3.2就能输出高电平VCC// P3_2=0;//给P3_2赋值0,引脚P3.2就能输出低电平GND// }//由于一直为真,所以不断输出高、低、高、低……,从而形成方波// } - 17 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 第五章 软件调试 5.1 Proteus及Keil软件简介 5.1.1 Proteus软件 Proteus软件是一种低投资的电子设计自动化软件,提供可仿真数字和模拟、交流和直流等数千种元器件和多达30多个元件库。Proteus软件提供多种现实存在的虚拟仪器仪表。此外,Proteus还提供图形显示功能,可以将线路上变化的信号,以图形的方式实时地显示出来。这些虚拟仪器仪表具有理想的参数指标,例如极高的输入阻抗、极低的输出阻抗,尽可能减少仪器对测量结果的影响,Proteus软件提供丰富的测试信号用于电路的测试。这些测试信号包括模拟信号和数字信号。提供Schematic Drawing、SPICE仿真与PCB设计功能,同时可以仿真单片机和周边设备,可以仿真51系列、AVR、PIC等常用的MCU,并提供周边设备的仿真,例如373、led、示波器等。Proteus提供了大量的元件库,有RAM、ROM、键盘、马达、LED、LCD、AD/DA、部分SPI器件、部分IIC器件,编译方面支持Keil和MPLAB等编译器。 一台计算机、一套电子仿真软件,在加上一本虚拟实验教程,就可相当于一个设备先进的实验室。以虚代实、以软代硬,就建立一个完善的虚拟实验室。在计算机上学习电工基础,模拟电路、数字电路、单片机应用系统等课程,并进行电路设计、仿真、调试等。 5.1.2 Keil软件 KeilC51是美国Keil Software公司出品的51系列兼容单片机C语言软件开发系统。与汇编相比,C语言在功能上、结构性、可读性、可维护性上有明显的优势,因而易学易用。用过汇编语言后再使用C来开发,体会更加深刻。 KeilC51软件提供丰富的库函数和功能强大的集成开发调试工具,全Windows界面。另外重要的一点,只要看一下编译后生成的汇编代码,就能体会到KeilC51生成的目标代码效率非常之高,多数语句生成的汇编代码很紧凑,容易理解。在开发大型软件时更能体现高级语言的优势。 5.2 应用Keil软件进行程序调试 软件的调试必须在开发系统的支持下进行。先分别调试通过各个模块程序,然后调试中断服务程序,最后调试主程序,将各部分连接进行调试。调试的范围可以由小到大,逐步增加,必要的中间信号可以先做设定。通常交叉使用单步运行,断点运行,连续运行等多种方式,每次执行完毕后,检查CPU执行现场,RAM的有关内容,I/O接口的状态等。发现一个问题,解决一个问题,直至全部通过。 - 18 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 首先新建一个工程项目文件;其次为工程选择目标器件;再次为工程项目设置软硬件调试环境;并创建源程序文件并输入程序代码,及保存创建的源程序项目文件;最后把源程序文件添加到项目中。 5.3 Proteus软件仿真 在Proteus软件中画出原理图,向单片机中加入需要调试的程序的HEX文件,便可以进行调试了。 5.3.1仿真步骤 利用Proteus实现单片机系统开发过程一般分为四步: 1.在Proteus平台上进行单片机系统电路设计、选择元器件、接插件、连接电路和电气检测等(简称Proteus电路设计); 2.在Keil平台上进行单片机系统源程序设计、编辑、汇编编译、调试,最后生成目标代码文件(*.hex)(简称Proteus软件设计); 3.再次在Proteus平台上将目标代码文件加载到单片机系统中,并实现单片机系统的实时交互、协同仿真(简称Proteus仿真); 最后仿真正确后,安装实际单片机系统电路,并将目标代码文件(*.hex)下载到实际单片机中运行、调试。若出现问题,可与Proteus设计与仿互配合调试,直至运行成功(简称实际产品安装、运行与调试)。实践证明:按照Proteus仿真通过的设计来安装的实际系统,只要安装正确、元器件无误,焊接牢靠,基本都能顺利通过。 5.3.2仿真实例 首先,进行参数的选定。本系统主芯片采用的STCC52RC,由于Keil中没有该芯片,所以用ATC51代替。因此选定该型号。 - 19 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 图5-1 芯片型号选择 其次,在Option For Target Target 1中选择生成HEX文件。 图5-2 HEX文件生成 运行的第一步,点击运行程序。确认运行程序无误,即没有错误和警告。 图5-3 确认程序无误 - 20 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 点击生成工程文件并生成HEX文件。 图5-4 已生成HEX文件 可通过Debug中的Run和Step来对程序的对应代码进行一一调试。 图5-5 程序运行调试 - 21 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 通过程序加入芯片,在Proteus软件里仿真的效果图。 图5-6 仿真显示图 5.4 硬件软件联合调试 5.4.1 联调步骤 第一步安装Keil与Proteus; 第二步把Proteus安装目录下VDM51.dll文件复制到Keil安装目录的C51BIN目录中; 第三步修改Keil安装目录下Tools.ini文件,在C51字段加入TDRV5=BINVDM51.DLL (\"Proteus VSM Monitor-51 Driver\")打开Proteus,画出相应电路。在Proteus的Tools菜单中选中Use remote debug monitor; 第四步在Keil中编写MCU的程序及进入Keil的Proteus菜单Option for target 工程名。在Debug选项中右栏上部的下拉菜选中 Proteus VSM Monitor-51 Driver。 第五步即最后在Keil中进行Debug,同时在Proteus中查看直观的结果。 5.4.2 搭接检查步骤 首先检查元件的好坏;按电路图买好元件后首先检查买回元件的好坏,按各元件的检测方法分别进行检测,一定要仔细认真。 其次放置各元件;按电路图的位置将各元件安置好,首先放置核心元件,然 - 22 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 后再放其他元件,特别注意顺序不能颠倒。 再次电路接线;在保证电路元器件完好及各元器件放置无误合理的情况下,开始对电路连接布线,由于本设计用面包板搭件,所以布线要无跨线并且工整。 - 23 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 第六章 结论 本文给出了一种用单片机和霍尔传感器测量汽车速度及里程的测量系统,克服了传统方法测量的不足,可以实现每秒对汽车速度进行测量。该测速系统具有测量速度快,测量精度高的优点。不但可应用于汽车的速度测量,而且可应用于其它要求转速精确测量系统中。 主要通过学习了霍尔传感器、STCC52RC单片机、LCD1602显示等知识,查阅了相关资料,实现了“汽车速度及里程电子显示系统”的基本设计要求。本系统实现了题目基本部分以及扩展部分的要求,可达到设计的基本条件要求。所设计的系统具有以下功能: 1.对于设计采用STCC52RC单片机作为测量转速和里程的主CPU芯片,系统硬件设备结构简单合理,成本低,实时性好。 2.测速系统采用霍尔传感器作为敏感速率信号,具有频率响应快,抗干扰能力强等特点。霍尔传感器的输出信号经信号调理后,通过单片机对连续脉冲记数来实现转速测量,充分利用了单片机的内部资源,有很高的性价比。并且在测量范围内转速越高测量精度越高。所以该系统在一般的转速检测和控制中均可应用。 3.针对采用LCD1602显示测速值和里程,直观、稳定,易于实现,并应用KEIL进行了软仿真,调试结果表明所设计的软件程序正确。 4.测速系统的功能还有待进一步扩充,如判别转速方向的能力;电路布局、和抗干扰方面还有很大的提升空间。 正是这一次设计让我积累了无数实际经验,使我的头脑更好的被知识武装了起来,也必然会让我在未来的工作学习中表现出更高的应变能力,更强的沟通力和理解力。顺利如期的完成本次毕业设计给了我很大的信心,让我了解专业知识的同时也对本专业的发展前景充满信心。 - 24 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 参考文献 [1] 赵负图.现代传感器集成电路[M].北京:人民邮电出版社, 2000.534-535. [2] 张毅坤 ,陈善久 ,裘雪红.单片微型计算机原理及应用[M].西安:西安电子科技大学 出版社,1998年. [3] 全润,张亚凡,邓洪敏.传感器原理及应用[M].北京:清华大学出版社,2008年 [4] 冯寿亭,李迪.基于嵌入式计算机和DSP的数控系统以及其通信实现[J].组合机床与 自动化,2005 [5] 来清民主编.传感器与单片机接口及实例[M].北京航空航天大学出版社. [6] 何希才.传感器及其应用[M].北京:国防工业出版社,2001· [7] 万福君 凌文玉 王乃厚等.单片微机原理系统设计与开发应用[M].合肥:中国科学技术大 学出版社,1995.46-52. [8] 冯雷星.基于单片机高性价比频率计的设计与实现[J].微计算机信息,2007,20(7): 85-86. [9] 冯夏勇,蔡建国.实用微机转速测量方法的研究[J].宇航测量技术,1997,17(6):49-25. [10] 胡汉才.单片机原理及其接口技术[M].北京:清华大学出版社.1996. [11] 杨宁 王吴 田蔚风等.高精度飞轮控制系统方案分析研究[J].航天控制,2004,22(3): 50一53. [12] 彭为,黄科,雷道仲.单片机典型系统设计实例精讲[M].北京:电子工业出版社.2007 年 [13] 李学礼.基于Proteus的8051单片机实例教程[M].北京:电子工业出版社,2008年 [14] 李 华.单片机通用接口技术[M]. 北京:北京航空航天大学出版社,1999. [15] 陈伯时.电力拖动自动控制系统[M].北京:机械工业出版社,2003:103—107。 [16] 张毅刚 彭喜元. MCS-51单片机应用设计[M].哈尔滨工业大学出版社.2003年 [17] 周航慈.单片机应用程序设计技术[M].北京航空航天大学出版社,2002(11) [18] 徐爱钧.单片机原理实用教程—基于Proteus虚拟仿真[M].北京:电子工业出版社.2009 年 [19] Texas Instruments Incorporated TMS320c54x DSP[J].BI0SUsers Guide SPRU326C,2000.5. [20] 刘涳主编.毕业设计宝典. 西安:西安电子科技大学出版社.2008年 - 25 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 附录1 源程序 源程序: #include #define y_long 2 //周长(米) /*-----------------------端口命名------------------------*/ sbit rs=P2^7; //LCD的数据/命令选择端 sbit rw=P2^6; //LCD的读写选择端 sbit lcdcs=P2^5; //LCD的使能信号端 sbit warning=P3^1; //蜂鸣器端 /*-------------------------------------------------------*/ /*-----------------------定义参数------------------------*/ uint i,z,count,zhuan,msec; //定义参数 uchar display[]={\" speed= r/sec \ //定义显示参数 uchar display_up[]={\" long= m \ //6-14数据 unsigned long x; /*-------------------------------------------------------*/ /*-----------------------毫秒延时------------------------*/ void delay(uint ms) { uint i,j; //为延时引入i,j两参数 for (j=0;j /*--------------------向LCD1602写命令--------------------*/ void write_command(uchar command) { rs=0; //选择写命令 P0=command; //向LCD写命令 lcdcs=1; //信号使能端高电平 lcdcs=0; //信号使能端低电平 } /*-------------------------------------------------------*/ /*--------------------向LCD1602写数据--------------------*/ void write_data(uchar data0) { rs=1; //选着写数据 P0=data0; //向LCD写数据 lcdcs=1; //信号使能端高电平 lcdcs=0; //信号使能端低电平 - 26 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) } /*-------------------------------------------------------*/ /*-------------------外部中断0计数程序-------------------*/ void counter(void) interrupt 0 { EX1=0; //关外部中断0 z++; //转圈计数加1 EX1=1; //开外部中断0 } /*-------------------------------------------------------*/ /*------------------------LCD显示------------------------*/ void displaytolcd() { write_command(0x80); //向LCD1602写命令80H for (i=0;i - 27 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) display_up[12]=(long_x/100%10+'0'); //里程的百位 display_up[13]=(long_x/10%10+'0'); //里程的十位 display_up[14]=(long_x%10+'0'); //里程的个位 } /*-------------------------------------------------------*/ /*-----------------内部中断0计时计数程序-----------------*/ void Timer_0(void) interrupt 1 { TH0=0x65; //50ms定时 TL0=0xF3; msec++; if(msec==20) //50*20=1S { zhuan=z; x+=z; disp_count(); //数据处理 displaytolcd(); msec=0; z=0; } } /*-------------------------------------------------------*/ /*----------------------端口初始化-----------------------*/ void int_all() { warning=0; //关蜂鸣器 z=0; //初始化z的值 count=0; //初始化count的值 zhuan=0; //初始化转的值 rw=0; //选择LCD写数据指令 delay(15); //延时15ms write_command(0x38); //向LCD1602写命令38H delay(5); //延时5ms write_command(0x0e); //向LCD1602写命令0EH delay(5); //延时5ms write_command(0x06); //向LCD1602写命令06H TMOD=0x01; //内部中断定时器选择 TH0=0x66; //50ms定时 TL0=0x50; EA=1; //开中断总开关 ET0=1; //开内部中断0 TR0=1; //计时器开始工作 IT0=1; //外部中断0为下降沿触发 EX0=1; //开外部中断0 - 28 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) } /*-------------------------------------------------------*/ /*-------------------转速过高警告程序--------------------*/ void warning_speed() { if(zhuan>=80) //高于80转打开警告 { warning=1; } if(zhuan<80) //低于80转关闭警告 { warning=0; } } /*-------------------------------------------------------*/ /*------------------------主函数-------------------------*/ void main() { int_all(); //全局初始化 while(1) { warning_speed(); //转速警告 } } /*-------------------------------------------------------*/ - 29 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 附录2 硬件实物图 外文文献 - 30 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 18.2.2 Algebraic Operators Besides the basic operations on fuzzy sets given above, namely intersection, union and complement we can define a large number of other algebraic operations which we now summarize. Definition. The algebraic sum, CAB,of two fuzzy sets with membership ~ is a fuzzy set with membership function functions A and B AB(x)A(x)B(x)A(x)B(x), xX. We write CAB=(x,AB(x))|xX. Definition.The algebraic product, CAB, of two fuzzy sets with membership ~ is a fuzzy set with membership function function A and B AB(x)A(x)B(x), xX. We write C(x,AB(x))|xX. Definition. The bounded sum, CAB, of two fuzzy sets with membership ~ is a fuzzy set with membership function function A and B AB(x)min1,A(x)B(x). We write C(x,AB(x))|xX. Definition. The bounded difference, C=AB, of two fuzzy sets with ~ is a fuzzy set with membership function membership function A and B AB(x)max0,A(x)B(x)1 We write C(x,AB(x))|xX The Cartesian product of fuzzy sets is defined as follows. - 31 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) Defintion. Let A1,…,An be fuzzy sets in X1,…, Xn.The Cartesian product is then a fuzzy set in the space X1…Xn with a membership function A...A(x)minA(xi)|x(x1,x2,...xn),xiXi. 1niiDefintion. The mth power of a fuzzy set A1 is a fuzzy set with the membership function Am(x)(A(x))m, xiXi. The Fuzzy AND and Fuzzy OR operators combine the logical AND and OR operators with the arithmetic norm. Defintion.The fuzzy AND of two fuzzy sets A and B is defined as CAB with membership function 1 C(x)ABminA(x),B(x)(1)(A(x)B(x)) 2where can be varied between 0 and 1 in order to weight the logical AND against the arithmetic mean. For =1 the fuzzy AND reduces to the logical AND and for =0 the fuzzy AND operator reduces to the arithmetic mean. Defintion.The fuzzy OR of two fuzzy sets A and B is defined as CAB with menbership function 12C(x)ABmaxA(x),B(x)(1)(A(x)B(x)),[0,1] 18.2.3 Defuzzification Operations In many practical applications we would like to obtain a crisp decision from the fuzzy analysis of the problem. For example, in a problem where a company uses fuzzy logic to decide on one of many marketing companies the result of the fuzzy analysis should be exactly one company.Similarly, in a control problem (e.g. the classic pole-balancing problem) we would like a crisp decision for the force which should be applied to the cart. The following operators are commonly used to extract a crisp decision from a fuzzy set. - 32 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) Definition. The minimum grade operator returns that support value which has the maximum grade (degree of truth). If there is no unique support value corresponding to be the maximum grade then it returns any one of these. Definition. The minimum grade operator returns that support value which has the minimum grade.If there is no unique support value corresponding to be the maximum grade then it returns any one of these. In many cases a small variation in the membership function can cause a very big variation in the decision (conclusion). For example , if we have a non-convex fuzzy set, then a small variation might change the decision from the one maximum to the other. This can be problematic, especially in the case of control problems where it can be a major cause of instability. Hence, for control problems one often uses the centroid of the fuzzy set for the crisp control strategy. Definition. The centroid c (or centre of mass) of a fuzzy set, F with membership function F(x) for xX is defined by c:xXxF(x)xXF(x) in the case of a discrete membership function and by c:xXxF(x)xXF(x) in the case where F(x) is continuous. For some applications it can be useful to obtain a crisp set from a fuzzy set. For example, when trading with stocks, one might want to use fuzzy logic inference to decide which stocks should be sold. Definition. The -cut operator returns a crisp set with a grade of 0 for support values with grade less than and 1 for support values which have a grade larger than or equal to . Definition. The -cut operator returns a crisp set with a grade 1 for the support values which have the highest grade and 0 for the remaining support values. Example. Consider the discrete fuzzy set - 33 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) F(1,0.85),(2,0.7),(3,0.1),(4,0.44),(5,0.87),(6,0.2),(7,0.19). The maximum grade is 5, the minimum grade is 3, the centroid is 3.343, the -cut with =0.7 returns the crisp set {(1,1),(2,0),(3,0),(4,0),(5,1),(6,0),(7,0)} and the -cut with =0.3 returns the crisp set {(1,1),(2,1),(3,0),(4,0),(5,1),(6,0),(7,0)}. There are a number of other defuzzification operators described in the literature, for example the weighted average method (only applies to symmetrical membership functions), the centre of sums, centre of largest area and frist (or last) maxima.For details and examples we refer to the literature (Ross [175]). 18.2.4 Fuzzy Concepts as Fuzzy Sets It is often convenient to have implemented fuzzy concepts like large, small, near, greater than and less than as fuzzy sets on a given universe. For example, the profit made on a product might, for arguments sake, be between 0% and 100%. We would like to implement the concept large as a fuzzy set. A simple way of doing this is to assume that the fuzzy set has points on a straight line with grade 1 for 100% profit and grade 0 for 0% profit. Similarly, we could define the concept, small, as a fuzzy set with a straight line as the menbership function which gives grade 1 for 0% profit and grade 0 for 100% profit. Definition. The concept small is represented by a fuzzy set, S with membership function S(x)xxmax xminxmaxand the concept large is represented by a fuzzy set L with membership function L(x)xxmin. xmaxxmin- 34 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) Another useful concept is near. For example, the profit on a certain product might be near 40%. We choose to implement near, as a fuzzy set whose membership function is given by a normal distribution with width 10% of the size of the universe. Definition. Consider a universe, X. The concept near p, with pX, is represented by a fuzzy set, N, with membership function 1(xp)2exp() N(x)222where the variance, , is chosen by default as (xmaxxmin). If 0,then we find the crisp is equal to the Dirac-Delta function. Gaussian membership function are also used in fuzzy pattern recognition. Suppose we have a one-dimensional universe on the real line, i.e.,X=.Consider two fuzzy sets A and B having normal Gaussian membership functions A(x)exp((xa)2a2), B(x)exp((xb)2b2) An inner product can be defined (Ross [175]) yielding (ab)2 ABexp()A(x0)B(x0) 2(ab)where x0:abba abIf the two fuzzy sets are identical, then the inner product equals one. Finally we define the fuzzy concepts greater than less than. For example , the profit on a certain product might be greater than 20%. We use a Fermi-Dirac distribution whose transition is at 40% to represent this fuzzy concept. Definition. Consider a universe,X. The concept, greater than p, with pX is represented by a fuzzy set, G, with menbership gunction - 35 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) G(x)[1exp((xp))]1 where those slope is chosen by default as 0.1(xmaxxmin). For 0 we recover the crisp greater than which is the step function 1forall G(x)|00forall18.2.5 Hedging xpxp A linguistic hedge or a modifier is an operation which modifies the meaning of a term. Concentration and dilation hedges are frequently used in fuzzy inference. Typical linguistic terms for concentrators are extremely and very. This shifts the emphasis towards larger support values,i.e. they increase the restrictiveness of the fuzzy set. Definition. A linguistic hedge is an operation that modifies the meaning of a term or, more generally, of a fuzzy set. If A is a fuzzy set then the modifier m generates the (composite) term Bm(A). Mathematical models frequently used for modifiers are concentration conA(x)(A(x))2 dilation dilA(x)(A(x))2 Example. Assume we choose the following membership function for the fuzzy set, 1H, corresponding to the linguistic term the weather is hot H(T)T,T[0:40]. 40The grade of membership (degree of truth) given to various temperatures is given in the following table T 0 0 5 0.125 10 0.25 15 0.375 20 0.5 25 0.625 30 0.75 35 0.825 40 1 H(T) - 36 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) This membership function will give a support of 0.5 to a temperature of 200C, 0.75 to 300C and 0.825 to 350C. The set of very temperatures should give only higher temperatures significant support. The membership function for the fuzzy set very hot, very H, can be obtained from the membership function of H by using a hedge veryH(T)(H(T))2 The grade of membership for very hot is given in the following table T 0 0 5 10 15 20 0.25 25 30 35 40 1 veryH(T) 0.016 0.063 0.141 0.391 0.563 0.681 Only the temperatures 350C and 400C have a 0.6-levvel support for very hot while 250C,300C,350Cand 400C all have a 0.6-level support for hot. The hedging thus restricts(or compressed) the fuzzy set around high temperatures. A commonly used mathematical expression for hedging is hedgeH(T)(H) with >1 for compression and <1 for dilation. For example, we could use the following hedge allocation for compression and dilation: hedge vaguely 0.05 slightly 0.25 somewhat 0.5 very 2 extremely 3 exactly Another hedge commonly used is a contrast enhancer - 37 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) enhH22(H(T))212(1H(T))forotherwiseH(T)[0,]12 From information theory we knonw that we can define the missing information of a probability by distribution by the shannon entropy S(x):(p(x)logp(x)) xX18.2.6 Quantifying Fuzzyness where the base of the logarithm, , determines the units of information. If =2, then the unit of information is the bit (binary digit). We would expect this since in this case we know with certainty that the solution is x0 (with probability 1) and hence there is no missing information. Similarly, if the probability distribution is flat,i.e. p(x)1sizeofX for all x then the missing information is a maximum. We can define the entropy of a fuzzy set F(x,F(x))|xX by (F):S(F)S(F) with S(F)F(x)log(F(x)) xXwhere is a positive constant. Thus the entropy is a suitable measure for fuzzyness. Example. Consider the following two discrete fuzzy sets A(1,0.4),(2,0.8),(3,1.0),(4,0.7),(5,0.3) and B(1,0.0),(2,0.1),(3,1.0),(4,0.2),(5,0.1) - 38 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) Then the 2(A)=3.46 bits, while 2(B)=1.66 bits. As we would expect, A is much more fuzzy than B. 18.2.7 C++ Implementation of Discrete Fuzzy Sets In the following header file fuzzy.h we implement the function described above as methods within the class Fuzzy. The function name near is replaced by closeTo since near is used in some C++ compilers as keyword. We supply 3 constructors. The default constructor Fuzzy() which allows the user to create an array of fuzzy sets, a constructor allowing the user to create a fuzzy set of a certain size and covering a range from xMax to xMin on the support axis, If the user does not specify the support-range then it is assumed to be [0,1] as specified by the defult valuses of the corresponding arguments. The support range can be altered at a later stage via the methed setDomain. The last constructor is the copy constructor. The method fillGrades sets all grades equal to the supplied fill-value. The method normalize normalizes the fuzzy set (i.e. the largest grade will be 1). The method rectangle fills all the grades of the fuzzy set whose support is between left and right with 1 and all others with zero. For example, the concept crisp faster than 100 for a universe of cruising speeds between say 100 and 200 could be defined by the rectangle Fuzzy fasterThan100(201,1,200); fasterThan100.rectangle(100,200); The methods triangle() and trapezoid() can be used to define a discrete fuzzy set with trianglar and trapezoidal membership functions, respectively. For example,we could define the fuzzy value for optimal cruising speed by a triangle and the range of acceptable cruising speed by a trapezoid Fuzzy optimalSpeed(201,0,200), acceptablrSpeed(201,200); optimalSpeed.triangle(100,110,120); - 39 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) acceptableSpeed.trapezoid(80,100,120,130); The methods small and large,near,lessThan and greaterThan are a direct implementation of the corresponding fuzzy concepts. The methods minGrade and maxGrade return the minimum and maximum grade of the fuzzy set, the methods minGradeSupport and maxGradeSupport the corresponding support value and the methods minGradeSupportIndex maxGradeSupportIndex the array index for these support values. The method centroid is usually used for defuzzification. It returns the centroid of the fuzzy set as a crisp value. The cardinality and relative cardinality are returned by the corresponding member functions. The if … then … rules require us to limit the degree to which a consequence is true to the degree that the antecedent is true. For this purpose we supply the member function, limit, which causes the set to saturate at the ceiling level. For example Fuzzy optimalSpeed(201,0,200); optimalSpeed.triangle(100,110,120); optimalSpeed.limti(0.5); The final membership function of the set optimalSpeed is a trapezoidal with corner points(100,0),(105,.05),(115,0.5),(120,0). The method alphaCut and betaCut implement the -cut and -cut operators. the method entropy estimates the entropic fuzzyness measure. By default the base of the logarithm is taken as 2. - 40 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) The methods xMin(),xMax() and domainSize() are simple query functions, the latter returning the size of the support domain. Since this is an implemetation of a discrete fuzzy set we have a finite resolution on the support domain. The resolution can be queried via the method resolution. The method isSubSet returns true,i.e. 1, if the set for which the method is called is a subset of the set supplied as argument to the method. We supply two operators for querying the grade for a given for a given support value. The frist is the array element access operator [] which takes an integer as argument. It simply returns the grade for the support value at the specified array index, hence at the discretized points. This operator can also be used to change the grade at that point. The second operator is the function call operator (). This operator can be used to query the grade at any support value between xMin and xMax, not only at the discretized grid points. The method uses linear interpolation to obtain the grade values between grid points. The assignment operator allows us to assign one fuzzy set to another. It returns the object itself by reference to allow concatenated standard C-style assignments set1 = set2 = set3 = set4; Most of the remaining operators are a direct implementation of the fuzzy-set operators. The relational operators < > = = >= <= != each return a crisp set (the grades are all either zero or one). For example, the greater-than operator, >,returns a fuzzy set with grade one for all support values for which the current set, the set for which the method was called, has greater support than the set supplied as argument to the method. The grades for all other support values are zero. The remaining relational operators act in a similar fashion. For the fuzzy-AND and the fuzzy-OR operators we have to specify a value for . The value of can be queried and set via the methods gamma and setGamma. - 41 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) The input/output stream operators, >> and << are used to read a fuzzy set from an input stream(e.g. a file or the keyboard) or to an output stream (e.g. a file or the screen). The static member function mean() calculates the weighted mean of array of fuzzy sets. If the array of weights is not supplied, the standard arithmetic mean is returned. The fact that the member function is static implies that it can be called without referring to an object of the class. It is effectively a global member function with the scope of the class. We can call it as follows Fuzzy::mean(setsArray,weights); Note that both arguments are as constant as can be,i.e. all pointers and what they point to are declared constant. Finally we implement linguistic hedging and contrast enhancement. We allow hedging with any continuous hedge-value. In order to introduce linguistic hedges we define static class constants for the terms extremely, very, substantially, somewhat, slightly, vaguely. The use could define the linguistic concept, large and then use hedging to introduce the concept verylarge: Fuzzy large(50); large.large(); Fuzzy veryLarge = largr.hedge(Fuzzy::very); - 42 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) Application 1. The following C++ program mfuzzy.cpp illustrates the usage of the discrete fuzzy set class Fuzzy in the file fuzzy.h. We used the following input file, fuzzy.dat 5 1 5 0.4 0.8 1.0 0.7 0.7 5 1 5 0.0 0.1 1.0 0.2 0.1 5 1 5 0.1 0.2 0.5 0.2 0.4 // mfuzzy.cpp #include int main(void) { ifstream fin(“fuzzy.dat”); if(fin == NULL){ cout << “file cannot be opened”; exit(0);} Fuzzy set1,set2; fin >> set1 >> set2; cout << “ s1 = ” << set1 << end1; cout << “ !s1 = ” << set1 << end1; - 43 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) cout << “ s1 && s2 = ” << (set1 && set2) << endl; cout << “ s1 | | s2 = ” << (set1 | | set2) << endl; cout << “ s1 + s2 = ” << (set1 + set2) << endl; cout << “ s1 - s2 = ” << (set1 - set2) << endl; cout << “ s1 & s2 = ” << (set1 & set2) << endl; cout << “ s1 | s2 = ” << (set1 | set2) << endl; Fuzzy set3; fin >> set3; set3.normalize(); cout << “ s3 = ” << set3 << endl; cout << “s3.centroid/cardinality/relativeCardinality/entropy = ” << set3.centroid() << “ ” << set3.entropy()<< endl; << set3.relativeCardinality() << “ ” << set3.entropy()<< endl; int nSets2 = 2; Fuzzy** sets2 = new Fuzzy*[nSets2]; double* weights = new double[2]; weights[0] = 0.2; weights[1] = 0.8; sets2[0] = &set1; set2[1] = &set2; cout << “ mean(s1..s2,weights[0.2,0.8]) = ” << Fuzzy::mean(set2,nSet2,weights) << endl; int nSets3 = 3; Fuzzy** sets3 = new Fuzzy*[nSets3]; sets3[0] = &set1; sets3[1] = &set2; sets3[2] = &set3; cout << “mean(s1..s3) = ” << Fuzzy::mean(sets3,nSets3) << endl; - 44 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) cout << “ s3.hedge(Fuzzy::very) = ” << set3.hedge(Fuzzy::very) << endl; cout << “ s3.enhanceContrast() = ” << set3.enhanceContrast() << endl; cout << “ s3.alphaCut(0.65) = ” << set3.alphaCut(0.65) << endl; cout << “ s3.betaCut(3) = ” << set3.betaCut(3) < << set3.enhanceContrast().entropy() << endl; Fuzzy small(5,0.2); small.small(); Fuzzy large(5,0.2); large.large(); cout << “ small: ” << small << endl; cout << “ large: ” << large << endl; Fuzzy legalSpeed(7,80,140); legalSpeed.lessThan(120); cout << “ legalSpeed = ” << legalSpeed << endl; Fuzzy optimumSpeed(7,80,140); optimumSpeed.closeTo(110); cout << “ optimumSpeed = ” << optimumSpeed << endl; fin.close(); return 0; } The output of the program is given below s1 = [(1,0.4)(2,0.8)(3,1)(4,0.7)(5,0.7)] !s2 = [(1,0.6)(2,0.2)(3,0)(4,0.3)(5,0.3)] s2 = [(1,0)(2,0.1)(3,1)(4,0.2)(5,0.1)] s1 && s2 = [(,10)(2,0.1)(3,1)(4,0.2)(5,0.1)] s1 | | s2 = [(1,0.4)(2,0.8)(3,1)(4,0.7)(5,0.7)] - 45 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) s1 + s2 = [(1,0.4)(2,0.9)(3,1)(4,0.9)(5,0.8)] s1 - s2 = [(1,0)(2,0)(3,1)(4,0)(5,0)] s1 & s2 = [(1,0.l)(2,0.275)(3,1)(4,0.325)(5,0.25)] s1 | s2 = [(1,0.3)(2,0.625)(3,1)(4,0.575)(5,0.55)] s3 = [(1,0.2)(2,0.4)(3,1)(4,0.4)(5,0.8)] s3.centroid/cardinality/relativeCardinality/entropy = 3.26087 2.8 0.7 3.38576 mean(s1…s2,weights[0.2,0.8]) = [(1,0.08)(2,0.24)(3,1)(4,0.3)(5,0.22)] mean(s1…s3) = [(1,0.2)(2,0.433333)(3,1)(4,0.433333)(5,0.533333)] s3.hedge(Fuzzy::very) = [(1,0.04)(2,0.16)(3,1)(4,0.16)(5,0.)] s3.enhanceContrast() = [(1,0.08)(2,0.32)(3,1)(4,0.32)(5,0.92)] s3.alphaCut(0.65) = [(1,0)(2,0)(3,1)(4.0)(5,1)] s3.betaCut(3) = [(1,0)(2,0)(3,1)(4,1)(5,1)] s3.entropy() <=> s3.enhanceContrast().entropy() = 3.38576 <=> 2.61312 small: [(0,1)(0.5,0.75)(1,0.5)(1.5,0.25)(2,0)] large:[(0,0)(0.5,0.25)(1,0.5)(1.5,0.75)(2,1)] legalSpeed =[(80,0.998729)(90,0.993307)(100,0.965555)(110,0.841131)(120,0.5)(130,0.158869)(140,0.0344452)] optimumSpeed 592)(140,3.72665e-06)] Application 2. When buying a pair of shoes there are certain crisp boundary conditions determined by the anatomy of one’s feet and by the balance of one’s bank account . We have a fuzzy concept of what we find good-looking and what we find - 46 - = [(80,3.72665e-06)(90,0.00386592)(100,0.249352)(110,1)(120,0.249352)(130,0.00386 天津工业大学2012届本科生天津工业大学毕业设计(论文) comfortable. The problem is thus one of finding a solution which, subject to certain crisp boundary conditions, optimizes some fuzzy objectives. Take the simple example of having to choose between N pairs of shoes, some of which are too small and some of which are too expensive. For this we use two crisp sets, tooSmall and tooExpensive. The grades of these crisp sets are either 0 or 1. We implement the fuzzy concepts of looks and comfort by the corresponding two fuzzy sets whose membership value is now a grade between zero and 1, representing our fuzzy evaluation of the looks and comfort of each pair of shoes . Consider now the code given below: // shoe.cpp #include int main(void) { ifstream infile(“shoe.dat”); Fuzzy tooSmall, tooExpensive, looks, comfort; infile tooSmall>> tooExpensive >>looks>> comfort; ofstream fout(“shoe.out”); fout << “ Select a comfortable good-looking shoe which” << “ is big enough and affordable:” << endl; fout << “= = = = = = = = = = = = = = = = = = = = = = = = = = =” “= = = = = = = = = = = = =” << endl; fout << “ Too-small constraint: ” << tooSmall << “ ( crisp set )” << endl; - 47 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) fout << “ Too expensive constraint: ” << tooExpensive << “ ( crisp set )” << endl << endl; fout << “ Look preferences: ” << looks << “(fuzzy set)” << endl; fout << “Comfort prefenences: ” << comfort << “(fuzzy set)” << endl << endl; Fuzzy satisfyConstraints =! ( tooSmall | | tooExpensive); fout << “ Constraints satisfied by :” << satisfyConstraints << endl; int nPreferences = 2; Fuzzy** preferences = new Fuzzy*[nPreferences]; preferences[0] = &looks; preferences[1] = &comfort; Fuzzy objective = Fuzzy::mean(preferences,nPreferences); fout << “Objective = ” << objective << endl << endl; Fuzzy shoeScores = (satisfyConstraints && objective); fout << “ Shoe scores = ” << shoeScores << endl; fout << “ Best shoe = ” << shoeScores.supportMaxGrade() << “ which has a grade of ” << shoeSores.maxGrade() << “.” << endl << endl; fout << “Select a shoe which is somewhat good-looking” << “ and very comfortable” << endl <<”(still not too small and not too expensive):” << endl; fout << “= = = = = = = = = = = = = = = = = = = = = = = = =” << endl; Fuzzy somewhatGoodLooking = looks.hedge(Fuzzy::somewhat); Fuzzy veryComfortable = comfort.hedge(Fuzzy::very); fout << “someshatGoodLooking = ” << somewhatGoodLooking << endl; - 48 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) fout << “veryComfortable = ” << veryComfortable << endl << endl; prefenerces[0] = &somewhatGoodLooking; preferences[1] = &veryComfortable; objective = Fuzzy::mean(preferences,nPreferences); fout << “Objective = ” << objective << endl << endl; shoeScroes = (satisfyContraints && objective); fout << “Shoe scores = ” << shoeScroes << endl; fout << “Best shoe = ” << shoeScroes.supportMaxGrade() << “which has a grade of ” << shoeScroes.maxGrade()<< “.” << endl; infile.close(); fout.close(); return 0; } The subset of which satisfies the crisp constraints is determined via Fuzzy satisfyConstraints = !(tooSmall | | tooExpensive); We have to somehow take a weighted average of the fuzzy preferences. This is done via Fuzzy objective = FuzzySize::mean(preferences,nPrefenences); which weight the two preferences equally. We determine now the confidence with which we select the various shoes by taking a logical AND (in the fuzzy sense) - 49 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) between the constraints and the preferences, i.e. the ideal shoe must satisfy the constraints AND the preferences: FuzzySet shoeScroes = (satisfyConstraints && objective); This is the fuzzy output of the fuzzy inference engine. At the end of the day we have to make a crisp choice between the shoes. For this purpose we have to defuzzify the output. In this simple example (which is naturally discrete) we select that particular shoe which obtains the highest confidence level, i.e. has the maximum grade. This is done by the supportMaxGrade() defuzzifier, which returns the support for that menber of the fuzzy set which received the highest confidence level (grade): int choiceOfShoe = shoeScroes.supportMaxGrade(); An example of an input file shoe.dat is 5 0 4 0 1 0 0 0 5 0 4 0 0 0 0 1 5 0 4 0.7 1.0 0.4 0.7 0.5 5 0 4 0.4 0.0 0.8 0.6 0.9 In this example we have 5 pairs of shoes to choose form . Hence each of the fuzzy sets has length 5 and has a domain ranging from 0 to 4 . The first set is actually a crisp set specifying that the second shoe is the only shoe which is too small. The second set specifies the crisp too-expensive boundary condition (only the last shoe is too - 50 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) expensive). The following two sets represent the fuzzy evaluation of the looks and the comfort for the various pairs of shoes. The output shoe.out of the first part of the program is given below: Select a comfortable, good-looking shoe which is big enough and affordable: = = = = = = = = = = = = = = = = = = = = = Too-small constraint: [(0,0)(1,1)(2,0)(3,0)(4,0)] (crisp set) Too expensive constraint: [(0,0)(1,0)(2,0)(3,0)(4,1)] (crisp set) Looks preferences:[(0,0.7)(1,1)(2,0.4)(3,0.7)(4,0.5)] (fuzzy set) Comfert preferences:[(0,0.4)(1,0)(2,0.8)(3,0.6)(4,0.9)] (fuzzy set) Constraints satisfied by:[(0,1)(1,0)(2,1)(3,1)(4,0)] Objective = [(0,0.55)(1,0.5)(2,0.6)(3,0.65)(4,0.7)] Shoe scores = [(0,0.55)(1,0)(2,0.6)(3,0.65)(4,0)] Best shoe = 3 which has a grade of 0.65. Select a shoe which is somewhat good-looking and very comfortable (still not too small and not too expensive) = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = somewhatGoodLooking = [(0,0.83666)(1,1)(2,0.632456)(3,0.83666)(4,0.707107)] veryComfortable = [(0,0.16)(1,0)(2,0.)(3,0.26)(4,0.81)] - 51 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) Objective = [(0,0.49833)(1,0.5)(2,0.636228)(3,0.59833)(4,0.758553)] Shoe scores = [(0,0.49833)(1,0)(2,0.636228)(3,0.59838)(4,0)] Best shoe = 2 which has a grade of 0.636228. We select a comfortable, good-looking shoe which is neither too small, nor too expensive. The “best” shoe in this case is shoe number 3(the fourth shoe). If we use hedging to select a shoe which is somewhat comfortable, but very good looking, then the “best” shoe is number 2 (the third shoe). Next we look at a simplified version of an optimal pricing problem. We have a number of possibly overlapping or even conflicting objectives. For example, the shareholders might push for a high price for the product, while salespersons might want a law price so that they can sell a large quantity. Finally there might be a third objective of having a profit margin of around 30% which ensures that the company does not come into bad light with the consumer watchdogs and hence has to face bad publicity. We might also have an absolute minimum below which the producer absolutely refuses to produce and an absolute maximum above which the salespersons are to make a profit(i.e. that the retail price must be above the cost of the product) and to underbid the opposition. Application 3. The following C++ code asks the user for the minimum and maximum price, the manufacturing cost and the competitor’s price. It also asks the user to rate the various objectives according to their relative importance. It then uses fuzzy logic to determine the optimum retail price for the product. // price.cpp #include - 52 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) using namespace std; int main(void) { int minPrice, maxPrice, manufacturingCost, competitorsPrice; cout << “ Enter minimum price (to nearest Rand)”; cin >> minPrice; cout << “ Enter maximum price (to nearest Rand)”; cin >> maxPrice; cout << “ Enter manufacturing cost (to nearest Rand)”; cin >> manufacturingCost; cout << “ Enter competitors price (to nearest Rand)”; cin >> competitorsPrice; int nPointsOnGrid = maxPrice – minPrice +1; const int nObjectives = 5; Fuzzy** objectives = new Fuzzy*[nObjectives]; //Shareholders objective of a very high price: objectives[0] = new Fuzzy(nPointsOnGrid,minPrice,MaxPrice); objectives[0] -> large(); //Salesperson’s objecitve of a low price: objectives[1] = new Fuzzy(nPointsOnGrid,minPrice,MaxPrice); objectives[1] -> small(); //Moralists objective of a 30% profit: objectives[2] = new Fuzzy(nPointsOnGrid,minPrice,MaxPrice); objectives[2] -> closeTo(manufacturingCost*1.3) ; //Objective of making a profit: - 53 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) objectives[3] = new Fuzzy(nPointsOnGrid,minPrice,MaxPrice); objectives[3] -> greaterThan(manufacturingCost); //Objective for competitiveness: objectives[4] = new Fuzzy(nPointsOnGrid,minPrice,MaxPrice); objectives[4] -> lessThan(competitiorsPrice); int importance[nObjectives]; cout << “Enter the importance of each of the following criteria:” << endl; cout << “(0->vaguely,1->slightly,2->somewhat,3->substantially,” << endl; << “4->very,5->extremely)” << endl; cout << “= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =” << endl; cout << “Shareholder’s desire for high price: ”; cin >> importance[0]; cout << “Shareholder’s desire for a low price: ”; cin >> importance[1]; cout << “Moralist’s objective of a 30% profit: ”; cin >> importance[2]; cout << “Retail price > manufactueing price: ”; cin >> importance[3]; cout << “Retail price < competitors price : ”; cin >> importance[4]; for(int nOb=0;nOb 天津工业大学2012届本科生天津工业大学毕业设计(论文) switch(importance[nOb]) { case 0: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::vaguely); break; case 1: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::slightly); break; case 2: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::somewhat); break; case 3: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::substantially); break; case 4: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::very); break; case 5: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::extremely); break; } } Fuzzy fuzzyPrice = (*objectives[0]) && (*objectives[1]) && (*objectives[2]) && (*objectives[3]) &&(*objectives[4]) cout << endl << “Recommended retail price: R” << fuzzyPrice.supportMaxGrade()<< endl; return 0; } The user dialogue could be as follows - 55 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) Enter minimum and maximum price (to nearest Rand): 2000 5000 Enter manufacturing cost (to nearest Rand): 2500 Enter competitors price (to nearest Rand): 4500 Enter the importance of each of the following criteria: (0 -> vaguely,1 -> slightly, 2 -> somewhat,3 -> substantially, 4 -> very, 5 -> extremely ) = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Shareholder’s desire for high price: 4 Shareholder’s desire for a low price: 3 Moralist’s objective of a 30% profit: 1 Retail price > manufacturing price: 5 Retail price < competitors price: 4 Recommended retail price : R39 Note that each of the fuzzy sets has 3000 elements. Assume now that the need to achieve turn-over becomes high and thus the salesperson’s desire for a low price is given more importance . The shareholders still refuse to budge from rating their importance of a high price as very important, but the turn-over becomes now extremely important. Enter minimum price (to nearest Rand): 2000 Enter maximum price (to nearest Rand): 5000 Enter manufacturing cost (to nearest Rand): 2500 Enter competitors price (to nearest Rand): 4500 Enter the relative importance of each of the following criteria: - 56 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) (0 -> vaguely,1 -> slightly,2 -> somewhat,3 -> substantially, 4 -> very, 5 -> extremely ) Shareholder’s desire for high price: 4 Shareholder’s desire for a low price: 5 Moralist’s objective of a 30% profit: 1 Retail price > manufacturing price: 5 Retail price < competitors price: 5 Recommended retail price : R3146 Our fuzzy inference engine suggests now a significantly lower price. 外文翻译 18.2.2 代数运算 除了上述模糊集的基本操作,即交集,并集和补集,我们可以定义我们总结的其它的大量的代数运算。 - 57 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) ~,定义:代数和, CAB 的两个模糊集的隶属函数A和B是一个模糊集的 隶属函数。 AB(x)A(x)B(x)A(x)B(x), xX. 我们写作 CAB=(x,AB(x))|xX. ~,是一个模糊集定义:代数的乘积,CAB的两个模糊集的隶属函数A和B的隶属函数 AB(x)A(x)B(x), xX. 我们写作 C(x,AB(x))|xX. ~,是一个定义:有界变量的总和,CAB的两个模糊集的隶属函数A和B模糊集的隶属函数 AB(x)min1,A(x)B(x) 我们写作 C(x,AB(x))|xX ~,是一个模定义:有界变量的差,C=AB的两个模糊集的隶属函数A和B糊集的隶属函数 AB(x)max0,A(x)B(x)1 我们写作 C(x,AB(x))|xX 模糊集的笛卡儿乘的定义如下。 定义:让A1,…,An在模糊集X1,…, Xn中。笛卡儿乘则在空间X1…Xn的模糊集隶属函数 A...A(x)minA(xi)|x(x1,x2,...xn),xiXi. 1nii定义:第m个模糊集A1的幂数,是一个模糊集的隶属函数 Am(x)(A(x))m, xiXi. - 58 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 运算符模糊与和模糊或是运算符逻辑与和或与算术规范相结合的。 定义:模糊和,两个模糊集A和B定义为 CAB 隶属函数 1 C(x)ABminA(x),B(x)(1)(A(x)B(x)) 2 可变化是0到1之间,为了衡量逻辑和针对的算术平均数。对于= 1模糊和 化简为逻辑和,对于= 0模糊和运算符化简为算术平均数。 定义:模糊或,两个模糊集A和B定义为 CAB 隶属函数 C(x)ABmaxA(x),B(x)(1)(A(x)B(x)),[0,1] 1218.2.3 去模糊化操作 在许多实际应用中,我们想从问题的模糊分析中获得一个刚性系统定论。例如,公司采用模糊逻辑在问题上进行模糊分析的结果应该是只有一个公司。同样,在控制问题(如经典的极平衡的问题),我们应该有一个刚性系统定论,就是力量应适用于车子。以下运算符常用于从一个模糊集中提取一个刚性系统定论。 定义:最低级的运算符返回支集值为最高等级(真度)。如果没有唯一的最高级的支集值与之对应,然后返回其中的任何一个。 定义:最低级运算符返回支集值为最低等级。如果没有唯一的支集值对应的是最高等级,然后返回其中任何一个。 在许多情况下在隶属函数微小变化可能会导致一个非常大变化的定论(结论)。例如,如果我们有一个非凸模糊集,那么小变化可能改变定论从一个最大到其他的。这可能会产生问题,特别是在控制问题的情况下它可以是一个不稳定重要原因。因此,控制问题人们常常用控制理论中的模糊集重心来解决。 定义:一个模糊集重心C(或重心),F与隶属函数F(x) 为 xX的定义 c:在离散隶属函数情况由 - 59 - xXxF(x)xXF(x) 天津工业大学2012届本科生天津工业大学毕业设计(论文) c:xXxF(x)xXF(x) 在这种情况下F(x)是连续的。 对于某些应用程序从一个刚性系统集合中获得一个模糊集是有用的。例如,股票买卖时,可能需要使用模糊逻辑推理来决定哪些股票应该卖出。 定义:运算符-cut返回一个刚性系统,支集值为0级小于,支集为1级大于或等于。 定义:运算符-cut返回的一个刚性系统,支集值为最高级1级,余下的支集值为0。 例子:研究离散模糊集 F(1,0.85),(2,0.7),(3,0.1),(4,0.44),(5,0.87),(6,0.2),(7,0.19). 最高级是5,最低级是3,重心是3.343。-cut当=0.7返回集 {(1,1),(2,0),(3,0),(4,0),(5,1),(6,0),(7,0)} -cut 当=0.3 返回集 {(1,1),(2,1),(3,0),(4,0),(5,1),(6,0),(7,0)}. 有一些文献中描述的其他模糊化运算符,例如加权平均法(只适用于对称隶属函数),该中心的总和,面积最大的中心,第一个(或最后一个)最大值。对于详细信息和例子,我们参考文献(高斯[175])。 18.2.4 模糊集的模糊概念 通常在一个给定的区域定义模糊概念像大、小、近似、大于、小于这些作为模糊集合是很方便的。例如,在讨论产品的利润问题上,利润会是0%和100%之间。我们可以作为一个模糊集的概念处理。这样做的一个简单的类函数是假设模糊集有0%的利润和100%的利润分别为0级与1级直线上的点。同样,我们可以定义的概念小,作为模糊集与隶属函数已给出了0%的利润和100%的利润为0级和1级的直线。 定义:隶属函数S代表一个模糊集的概念小 S(x)xxmax xminxmax- 60 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 隶属函数L代表一个模糊集的概念大 xxmin. xmaxxmin L(x)另外一个有用的概念是近似。例如,对某种产品的利润可能会接近40%。我们选择近似作为一个模糊集,其隶属函数度被定义为区域大小为10%的正态分布。 定义:考虑一个区域,X。概念近似于p,pX,是一个模糊集的表示。用隶属函数N 1(xp)2exp() N(x)222方差 默认选择(xmaxxmin)。如果0,我们发现,刚性等于Dirac-Delta函数。 高斯隶属函数也可用于在模糊模式识别。假设我们在实线的一维的区域,即X = R。考虑两个模糊集A和B 有正常的高斯隶属函数 A(x)exp((xa)2a2), B(x)exp((xb)2b2) 可以定义一个内积(高斯[175])产生 (ab)2 ABexp()A(x0)B(x0) (ab)2这里 x0:abba ab如果两个模糊集是相同的,那么他们的内积等于一。 最后,我们定义模糊的概念大于小于。例如,对某种产品的利润可能超过20%。我们用Fermi-Dirac分布的过渡是40%以上代表这个模糊的概念。 定义:考虑到一个宇区域X。概念大于p, pX代表一个模糊集。隶属函数G为 - 61 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) G(x)[1exp((xp))]1 其中的斜坡默认选择0.1(xmaxxmin)。对于0我们返回阶跃函数 1forall G(x)|00forallxpxp 18.2.5对冲 一种语言的对冲或修饰符是一种用来修饰术语意思的工具。模糊推理中经常使用的浓度和扩张对冲。集中的典型语言方面是极和非常。这将实现更大的支集值,即强调了他们增加性的模糊集。 定义:一种语言的对冲操作更普遍的意思是修饰一个名词或一个模糊集。如果A是一个模糊集,然后修饰符m生成(复合)项Bm(A) 修饰符经常使用的数学模型 浓度 conA(x)(A(x))2 扩张 dilA(x)(A(x))2 例子:假设我们选择下面的隶属函数H模糊集 H(T) 在下面的表给出给予不同温度下的隶属度(真度) T 0 0 5 0.125 10 0.25 15 0.375 20 0.5 25 0.625 30 0.75 35 0.825 40 1 T,T[0:40]. 401H(T) 这个隶属函数将给予200C温度的支集为0.5, 300C的为0.75,350C的为0.825。非常温度应该给出更高的温度显着的支集。很热的模糊集的隶属函数,通过使用对冲可以得到非常H这个隶属函数。 veryH(T)(H(T))2 下表给出很热级隶属 - 62 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) T 0 0 5 10 15 20 0.25 25 30 35 40 1 veryH(T) 0.016 0.063 0.141 0.391 0.563 0.681 只有温度是350C和400C有一个很热的0.6级支集,当250C,300C,350C和400C都为支集为0.6级。因此,对冲(或压缩)模糊集接近高温。 通常用于对冲的数学表达式是 hedgeH(T)(H) 当>1压缩,当<1扩张。例如,我们可以使用以下的对冲分配,压缩和扩张: 对冲 有点 0.05 稍 0.25 略 0.5 非常 2 极 3 完全 另一个常用的对冲是对比度增强 enhH2(H(T))2212(1(T))HforotherwiseH(T)[0,]12 18.2.6 量化模糊性 从信息理论,我们知道,我们可以定义缺少的信息由Shannon熵的概率分布 S(x):(p(x)logp(x)) xX其中对数的底数决定信息的单位。如果=2,信息的单位是位(二进制位)。在这种情况下,我们肯定知道该解决方案 x0是(概率为1),因此有没有丢失的 信息。同样,如果是概率分布是平均分布,即 - 63 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) p(x)1sizeofX for all x 丢失的信息是最大的。我们可以定义一个模糊集的熵 F(x,F(x))|xX (F):S(F)S(F) S(F)F(x)log(F(x)) xX是一个正的常数。因此熵是一个衡量模糊的适当措施。 例子:考虑以下两个离散的模糊集 A(1,0.4),(2,0.8),(3,1.0),(4,0.7),(5,0.3) 和B(1,0.0),(2,0.1),(3,1.0),(4,0.2),(5,0.1) 然后2(A)=3.46位,而2(B)=1.66位,正如我们所期望的,A比B更加模糊。 18.2.7 C + +实现离散模糊集 在下面的头文件fuzzy.h我们实现上述类函数在类模糊描述的功能。在一些C + +编译器作为关键字使用。 我们提供3个构造函数。默认的构造模糊(),它允许用户创建一个模糊集的阵列,允许用户创建一个具有一定规模的模糊集和支集轴从XMAX来XMIN的范围涵盖了一个构造器,如果用户不指定的支集范围,然后它被认为是[0,1]作为指定相应的参数的默认值。通过求setDomain的根可以改变支集范围。 在类函数fillGrades中设置所有级别等于提供的默认值。类函数规范化规范模糊集(即最大的等级将是1)。该类函数的矩形默认模糊集的支集在左右之间的用1级,其他的是0级。 Fuzzy fasterThan100(201,1,200); fasterThan100.rectangle(100,200); 类函数三角形()和梯形()可以分别用来定义三边和梯形隶属函数的离散模糊集。例如,我们可以把最佳巡航速度用三角形,巡航速度范围用矩形分别来定义。 - - 天津工业大学2012届本科生天津工业大学毕业设计(论文) Fuzzy optimalSpeed(201,0,200), acceptablrSpeed(201,200); optimalSpeed.triangle(100,110,120); acceptableSpeed.trapezoid(80,100,120,130); 类函数minGrade和maxGrade返回最低和最高等级模糊集的类函数minGradeSupport和maxGradeSupport相应的支集值和类函数 minGradeSupportIndex maxGradeSupportIndex 这些支集值的数组索引。该类函数的质心常常用于模糊化。它作为刚性系统返回模糊集的重心。基数和相对基数通过相应的成员函数返回。 如果......那么......规则要求结果的条件是真实的程度。例如 Fuzzy optimalSpeed(201,0,200); optimalSpeed.triangle(100,110,120); optimalSpeed.limti(0.5); 最后的的集合optimalSpeed的隶属函数是一个梯形用角点(100,0),(105,.05),(115,0.5),(120,0)表示。类函数alphaCut和betaCut用运算符-cut 和 -cut。衡量模糊的熵。默认情况下,对数的底数为2。 类函数XMIN(),XMAX()和domainSize()是简单的查询功能,后者是返回的支集域的大小。由于这是一个离散模糊集,在支持域上我们有一个有限的分辨率,这个分辨率可以通过类函数分辨率查到。类函数isSubSet返回,即1。 我们两家运营商提供查询为一个给定的支集值的档次。第一是数组元素的访问操作符[]需要一个整数作为参数。它只是返回指定数组索引的支集值的档次,因此在离散点这个操作符也可以被用来改变在这一点上档次。第二个运算符是函数调用操作符()。这个操作符可以用来查询档次不仅在离散网格点之间的任何支集Xmin和Xmax值。该类函数采用线性插值获得网格点之间的等级值。赋值运算符允许我们指定一个模糊集到另一个。它返回引用对象本身允许串联的标准C语言风格的分配 - 65 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) set1 = set2 = set3 = set4; 其余的运算符大多是一个模糊集运算符的直接执行。关系运算符 < > = = >= <= != 每个返回刚性系统(成绩是所有零或一个)。例如,大于运算符>,当前集合的所有支集值将返回1级的模糊集,类函数被称为集。所有其他的支集值的等级是零。剩余的关系运算符以类似的方式行事。 模糊与模糊或运算符,我们必须指定一个值。的值是可以通过伽玛和函数查询和设置的类函数。 输入/输出操作符,>>和<<通常用作从一个输入(如文件或键盘)或一个输出(如文件或屏幕)读取一个模糊集。 静态成员函数的平均值()计算的加权平均模糊集的数组。如果没有提供阵列的重量、标准的算术平均数,则返回。成员函数是静态的事实意味着它可以被称为不是一类的对象。它实际上是一个全球范围类的成员函数。我们可以调用如下: Fuzzy::mean(setsArray,weights); 请注意,这两个参数可以是常数,即所有指针和指向常量。 最后,我们注意语言对冲和对比度增强。我们允许任何连续的对冲价值对冲。下面引入语言中静态类对冲中的常量定义 极,非常,相当,略,稍,有点。 可以定义使用的语言的概念,大,然后用对冲引进的概念verylarge: Fuzzy large(50); large.large(); Fuzzy veryLarge = largr.hedge(Fuzzy::very); 应用:1。下面的C+ +程序mfuzzy.cpp说明使用的离散模糊集模糊类文件fuzzy.h的。我们用下面的输入文件,fuzzy.dat 5 1 5 - 66 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 0.4 0.8 1.0 0.7 0.7 5 1 5 0.0 0.1 1.0 0.2 0.1 5 1 5 0.1 0.2 0.5 0.2 0.4 // mfuzzy.cpp #include int main(void) { ifstream fin(“fuzzy.dat”); if(fin == NULL){ cout << “file cannot be opened”; exit(0);} Fuzzy set1,set2; fin >> set1 >> set2; cout << “ s1 = ” << set1 << end1; cout << “ !s1 = ” << set1 << end1; cout << “ s1 && s2 = ” << (set1 && set2) << endl; cout << “ s1 | | s2 = ” << (set1 | | set2) << endl; cout << “ s1 + s2 = ” << (set1 + set2) << endl; cout << “ s1 - s2 = ” << (set1 - set2) << endl; cout << “ s1 & s2 = ” << (set1 & set2) << endl; cout << “ s1 | s2 = ” << (set1 | set2) << endl; - 67 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) Fuzzy set3; fin >> set3; set3.normalize(); cout << “ s3 = ” << set3 << endl; cout << “s3.centroid/cardinality/relativeCardinality/entropy = ” << set3.centroid() << “ ” << set3.entropy()<< endl; << set3.relativeCardinality() << “ ” << set3.entropy()<< endl; int nSets2 = 2; Fuzzy** sets2 = new Fuzzy*[nSets2]; double* weights = new double[2]; weights[0] = 0.2; weights[1] = 0.8; sets2[0] = &set1; set2[1] = &set2; cout << “ mean(s1..s2,weights[0.2,0.8]) = ” << Fuzzy::mean(set2,nSet2,weights) << endl; int nSets3 = 3; Fuzzy** sets3 = new Fuzzy*[nSets3]; sets3[0] = &set1; sets3[1] = &set2; sets3[2] = &set3; cout << “mean(s1..s3) = ” << Fuzzy::mean(sets3,nSets3) << endl; cout << “ s3.hedge(Fuzzy::very) = ” << set3.hedge(Fuzzy::very) << endl; cout << “ s3.enhanceContrast() = ” << set3.enhanceContrast() << endl; cout << “ s3.alphaCut(0.65) = ” << set3.alphaCut(0.65) << endl; cout << “ s3.betaCut(3) = ” << set3.betaCut(3) < - 68 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) << set3.enhanceContrast().entropy() << endl; Fuzzy small(5,0.2); small.small(); Fuzzy large(5,0.2); large.large(); cout << “ small: ” << small << endl; cout << “ large: ” << large << endl; Fuzzy legalSpeed(7,80,140); legalSpeed.lessThan(120); cout << “ legalSpeed = ” << legalSpeed << endl; Fuzzy optimumSpeed(7,80,140); optimumSpeed.closeTo(110); cout << “ optimumSpeed = ” << optimumSpeed << endl; fin.close(); return 0; } The output of the program is given below s1 = [(1,0.4)(2,0.8)(3,1)(4,0.7)(5,0.7)] !s2 = [(1,0.6)(2,0.2)(3,0)(4,0.3)(5,0.3)] s2 = [(1,0)(2,0.1)(3,1)(4,0.2)(5,0.1)] s1 && s2 = [(,10)(2,0.1)(3,1)(4,0.2)(5,0.1)] s1 | | s2 = [(1,0.4)(2,0.8)(3,1)(4,0.7)(5,0.7)] s1 + s2 = [(1,0.4)(2,0.9)(3,1)(4,0.9)(5,0.8)] s1 - s2 = [(1,0)(2,0)(3,1)(4,0)(5,0)] s1 & s2 = [(1,0.l)(2,0.275)(3,1)(4,0.325)(5,0.25)] s1 | s2 = [(1,0.3)(2,0.625)(3,1)(4,0.575)(5,0.55)] s3 = [(1,0.2)(2,0.4)(3,1)(4,0.4)(5,0.8)] - 69 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) s3.centroid/cardinality/relativeCardinality/entropy = 3.26087 2.8 0.7 3.38576 mean(s1…s2,weights[0.2,0.8]) = [(1,0.08)(2,0.24)(3,1)(4,0.3)(5,0.22)] mean(s1…s3) = [(1,0.2)(2,0.433333)(3,1)(4,0.433333)(5,0.533333)] s3.hedge(Fuzzy::very) = [(1,0.04)(2,0.16)(3,1)(4,0.16)(5,0.)] s3.enhanceContrast() = [(1,0.08)(2,0.32)(3,1)(4,0.32)(5,0.92)] s3.alphaCut(0.65) = [(1,0)(2,0)(3,1)(4.0)(5,1)] s3.betaCut(3) = [(1,0)(2,0)(3,1)(4,1)(5,1)] s3.entropy() <=> s3.enhanceContrast().entropy() = 3.38576 <=> 2.61312 small: [(0,1)(0.5,0.75)(1,0.5)(1.5,0.25)(2,0)] large:[(0,0)(0.5,0.25)(1,0.5)(1.5,0.75)(2,1)] legalSpeed =[(80,0.998729)(90,0.993307)(100,0.965555)(110,0.841131)(120,0.5)(130,0.158869)(140,0.0344452)] optimumSpeed 592)(140,3.72665e-06)] 应用2。当买了一双鞋,有一定的刚性系统边界条件确定一个人的脚的解剖结构,并通过自己的银行帐户余额。我们有一个模糊的概念,我们觉得好看,我们觉得舒适。因此,问题是找到一个解决办法,受某些刚性边界条件约束,优化了一些模糊的目标。进行鞋子的选择,其中有一些是太小了,其中一些是太昂贵的。为此,我们使用两个刚性系统,tooSmall和tooExpensive。这些刚性系统是0或1。其成员值是一个0和1之间的档次,每双鞋的外观和舒适度评价模糊集的外观和舒适性的模糊概念。现在考虑下面的代码: // shoe.cpp #include - 70 - = [(80,3.72665e-06)(90,0.00386592)(100,0.249352)(110,1)(120,0.249352)(130,0.00386 天津工业大学2012届本科生天津工业大学毕业设计(论文) #include “fuzzy.h” using namespace std; int main(void) { ifstream infile(“shoe.dat”); Fuzzy tooSmall, tooExpensive, looks, comfort; infile tooSmall>> tooExpensive >>looks>> comfort; ofstream fout(“shoe.out”); fout << “ Select a comfortable good-looking shoe which” << “ is big enough and affordable:” << endl; fout << “= = = = = = = = = = = = = = = = = = = = = = = = = = =” “= = = = = = = = = = = = =” << endl; fout << “ Too-small constraint: ” << tooSmall << “ ( crisp set )” << endl; fout << “ Too expensive constraint: ” << tooExpensive << “ ( crisp set )” << endl << endl; fout << “ Look preferences: ” << looks << “(fuzzy set)” << endl; fout << “Comfort prefenences: ” << comfort << “(fuzzy set)” << endl << endl; Fuzzy satisfyConstraints =! ( tooSmall | | tooExpensive); fout << “ Constraints satisfied by :” << satisfyConstraints << endl; int nPreferences = 2; Fuzzy** preferences = new Fuzzy*[nPreferences]; preferences[0] = &looks; preferences[1] = &comfort; Fuzzy objective = Fuzzy::mean(preferences,nPreferences); - 71 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) fout << “Objective = ” << objective << endl << endl; Fuzzy shoeScores = (satisfyConstraints && objective); fout << “ Shoe scores = ” << shoeScores << endl; fout << “ Best shoe = ” << shoeScores.supportMaxGrade() << “ which has a grade of ” << shoeSores.maxGrade() << “.” << endl << endl; fout << “Select a shoe which is somewhat good-looking” << “ and very comfortable” << endl <<”(still not too small and not too expensive):” << endl; fout << “= = = = = = = = = = = = = = = = = = = = = = = = =” << endl; Fuzzy somewhatGoodLooking = looks.hedge(Fuzzy::somewhat); Fuzzy veryComfortable = comfort.hedge(Fuzzy::very); fout << “someshatGoodLooking = ” << somewhatGoodLooking << endl; fout << “veryComfortable = ” << veryComfortable << endl << endl; prefenerces[0] = &somewhatGoodLooking; preferences[1] = &veryComfortable; objective = Fuzzy::mean(preferences,nPreferences); fout << “Objective = ” << objective << endl << endl; shoeScroes = (satisfyContraints && objective); fout << “Shoe scores = ” << shoeScroes << endl; fout << “Best shoe = ” << shoeScroes.supportMaxGrade() << “which has a grade of ” << shoeScroes.maxGrade()<< “.” << endl; infile.close(); - 72 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) fout.close(); return 0; } 满足刚性系统约束决定通过 Fuzzy satisfyConstraints = !(tooSmall | | tooExpensive); 我们必须以某种方式采取模糊偏好的加权平均。这是通过 Fuzzy objective = FuzzySize::mean(preferences,nPrefenences) 这同样重量的两个喜好。现在我们确定,我们选择以逻辑之间的约束和偏好(意识模糊)的各种鞋的信心,即理想的鞋必须满足的约束和偏好: FuzzySet shoeScroes = (satisfyConstraints && objective); 这是一个模糊的模糊推理引擎的输出。在一天结束时,我们一定要刚性系统选择之间的鞋。为此,我们必须defuzzify输出。 (这是自然离散)在这个简单的例子,我们选择特定的鞋,其中获得最高的信心水平,即最高等级。这样做的supportMaxGrade()defuzzifier的,它返回的支集,获得了最高的信任级别(档次)的模糊集会员专区: int choiceOfShoe = shoeScroes.supportMaxGrade(); 一个输入文件shoe.dat的例子是 5 0 4 0 1 0 0 0 5 0 4 0 0 0 0 1 5 0 4 0.7 1.0 0.4 0.7 0.5 5 0 4 0.4 0.0 0.8 0.6 0.9 - 73 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 在这个例子中,我们有5双鞋作为选择。因此每个模糊集的长度为5和域范围从0到4。第一组实际上是一个刚性系统,第二只鞋是唯一太小的鞋。第二组刚性系统的边界条件是太昂贵(只有最后的鞋太贵)。以下两套代表的各种双鞋子的外观和舒适性的模糊评价。 该计划的第一部分的输出shoe.out如下: 选择一个舒适,好看的鞋子,这是足够大的和负担得起的: Too-small constraint: [(0,0)(1,1)(2,0)(3,0)(4,0)] (crisp set) Too expensive constraint: [(0,0)(1,0)(2,0)(3,0)(4,1)] (crisp set) Looks preferences:[(0,0.7)(1,1)(2,0.4)(3,0.7)(4,0.5)] (fuzzy set) Comfert preferences:[(0,0.4)(1,0)(2,0.8)(3,0.6)(4,0.9)] (fuzzy set) Constraints satisfied by:[(0,1)(1,0)(2,1)(3,1)(4,0)] Objective = [(0,0.55)(1,0.5)(2,0.6)(3,0.65)(4,0.7)] Shoe scores = [(0,0.55)(1,0)(2,0.6)(3,0.65)(4,0)] Best shoe = 3 which has a grade of 0.65. Select a shoe which is somewhat good-looking and very comfortable (still not too small and not too expensive) = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = somewhatGoodLooking = [(0,0.83666)(1,1)(2,0.632456)(3,0.83666)(4,0.707107)] veryComfortable = [(0,0.16)(1,0)(2,0.)(3,0.26)(4,0.81)] Objective = [(0,0.49833)(1,0.5)(2,0.636228)(3,0.59833)(4,0.758553)] - 74 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) Shoe scores = [(0,0.49833)(1,0)(2,0.636228)(3,0.59838)(4,0)] Best shoe = 2 which has a grade of 0.636228. 我们选择一个舒适,好看的既不是太小也不是太贵了的鞋子。 在这种情况下“最好”的鞋子是鞋3号(第四双鞋)。如果我们用对冲选择的鞋是有点舒服,但很好看,那么“最好的”鞋是2号(鞋)。 接下来我们看看一个最优定价问题。我们有一些可能重叠甚至冲突的目标。例如,股东可能会推高了产品的价格,而营业员可能需要法定的价格,使他们能够大量出售。最后有可能是30%左右的利润率的第三个目标。 应用3。下面的C+ +代码,用户要求的最低和最高价格,生产成本和竞争对手的价格。它还要求用户根据目标的相对重要性评价各项目标。然后使用模糊逻辑,确定最佳的产品的零售价。 // price.cpp #include int main(void) { int minPrice, maxPrice, manufacturingCost, competitorsPrice; cout << “ Enter minimum price (to nearest Rand)”; cin >> minPrice; cout << “ Enter maximum price (to nearest Rand)”; cin >> maxPrice; cout << “ Enter manufacturing cost (to nearest Rand)”; cin >> manufacturingCost; cout << “ Enter competitors price (to nearest Rand)”; - 75 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) cin >> competitorsPrice; int nPointsOnGrid = maxPrice – minPrice +1; const int nObjectives = 5; Fuzzy** objectives = new Fuzzy*[nObjectives]; //Shareholders objective of a very high price: objectives[0] = new Fuzzy(nPointsOnGrid,minPrice,MaxPrice); objectives[0] -> large(); //Salesperson’s objecitve of a low price: objectives[1] = new Fuzzy(nPointsOnGrid,minPrice,MaxPrice); objectives[1] -> small(); //Moralists objective of a 30% profit: objectives[2] = new Fuzzy(nPointsOnGrid,minPrice,MaxPrice); objectives[2] -> closeTo(manufacturingCost*1.3) ; //Objective of making a profit: objectives[3] = new Fuzzy(nPointsOnGrid,minPrice,MaxPrice); objectives[3] -> greaterThan(manufacturingCost); //Objective for competitiveness: objectives[4] = new Fuzzy(nPointsOnGrid,minPrice,MaxPrice); objectives[4] -> lessThan(competitiorsPrice); int importance[nObjectives]; cout << “Enter the importance of each of the following criteria:” << endl; cout << “(0->vaguely,1->slightly,2->somewhat,3->substantially,” << endl; << “4->very,5->extremely)” << endl; cout << “= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =” - 76 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) << endl; cout << “Shareholder’s desire for high price: ”; cin >> importance[0]; cout << “Shareholder’s desire for a low price: ”; cin >> importance[1]; cout << “Moralist’s objective of a 30% profit: ”; cin >> importance[2]; cout << “Retail price > manufactueing price: ”; cin >> importance[3]; cout << “Retail price < competitors price : ”; cin >> importance[4]; for(int nOb=0;nOb case 0: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::vaguely); break; case 1: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::slightly); break; case 2: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::somewhat); break; case 3: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::substantially); break; case 4: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::very); break; - 77 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) case 5: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::extremely); break; } } Fuzzy fuzzyPrice = (*objectives[0]) && (*objectives[1]) && (*objectives[2]) && (*objectives[3]) &&(*objectives[4]) cout << endl << “Recommended retail price: R” << fuzzyPrice.supportMaxGrade()<< endl; return 0; } 用户对话可能如下 输入最低和最高价格(最近兰德):2000 5000 进入生产成本(最近兰德):2500 输入竞争对手的价格(最近兰特):4500 输入每个下列条件的重要性: (0 - >依稀,1 - >略, 2 - 有些,3 - >大幅 4 - >非常,5 - >极) ======================================= 股东的愿望高价格:4 股东的愿望,以低廉的价格:3 道德的目标,30%的利润:1 零售价>制造价格:5 零售价<竞争对手的价格:4 建议零售价:R39 输入最低价格(最近兰德):2000 进入最高价格(最近兰德):5000 进入生产成本(最近兰德):2500 输入竞争对手的价格(最近兰特):4500 - 78 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 输入每个下列条件的相对重要性: (0 - >模糊地,1 - >略,2 - 有些,3 - >大幅 4 - >非常,5 - >极) 股东的愿望高价格:4 股东的愿望,以低廉的价格:5 道德的目标,30%的利润:1 零售价>制造价格:5 零售价<竞争对手的价格:5 建议零售价:R3146 现在我们的模糊推理建议价格降低。 - 79 - 天津工业大学2012届本科生天津工业大学毕业设计(论文) 谢 辞 四年的学习和生活就要随着这篇论文的答辩而结束了。有许许多多的舍不得,也有许许多多的感谢要说。毕业论文的完成也随之进入了尾声。当我在电脑上敲出了最后一个字,心中涌现的不是想象已久的欢欣,却是难以言喻的失落。是的,随着论文的终结,意味着我生命中最纯美的学生时代即将结束,尽管百般不舍,这一天终究会在熙熙攘攘的喧嚣中来临。 在本论文撰写完成之际,衷心感谢所有给予我指导和帮助的老师、同学。感谢测控技术与仪器系的领导及各实验室的老师给予我们热情的指导和帮助,为我们投入了大量的财力、物力和实验设施。 - 80 -
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- 7swz.com 版权所有 赣ICP备2024042798号-8
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务