Advertise here

..Back

×

Fuzzy algorithm example in js

Author: Win Aung Cho
2025-05-06 07:39:13PM

Fuzzy logic

Fuzzy logic is logic where state membership is, essentially, a float with range 0..1 (truth value) instead of an int 0 or 1.
<body>
	
	<script>
	class FuzzySet {
		constructor(name, values) {
			this.name = name;
			this.values = values;
		}
		membership(x) {
			if(x <= this.values[0]) return 0;
			if(x >= this.values[this.values.length - 1]) return 0;
			if(this.values.length === 3) { // Triangular
				const [a, b, c] = this.values;
				if(x > a && x <= b) return(x - a) / (b - a);
				if(x > b && x < c) return(c - x) / (c - b);
			}
			return 0;
		}
	}
	class FuzzyRule {
		constructor(antecedent, consequent) {
			this.antecedent = antecedent;
			this.consequent = consequent;
		}
	}
	class FuzzyThermostat {
		constructor() {
			this.temperatureSets = {
				cold: new FuzzySet("cold", [10, 15, 20]),
				comfortable: new FuzzySet("comfortable", [18, 22, 26]),
				hot: new FuzzySet("hot", [24, 30, 35])
			};
			this.temperatureChangeSets = {
				decrease: new FuzzySet("decrease", [-6, -3, 0]),
				noChange: new FuzzySet("noChange", [-1, 0, 1]),
				increase: new FuzzySet("increase", [1, 4, 7])
			};
			this.rules = [
				new FuzzyRule({
					temperature: this.temperatureSets.cold
				}, {
					change: this.temperatureChangeSets.increase
				}),
				new FuzzyRule({
					temperature: this.temperatureSets.comfortable
				}, {
					change: this.temperatureChangeSets.noChange
				}),
				new FuzzyRule({
					temperature: this.temperatureSets.hot
				}, {
					change: this.temperatureChangeSets.decrease
				})
			];
		}
		determineChange(temperature) {
			let aggregatedOutput = {};
			for(const rule of this.rules) {
				const antecedentMembership = rule.antecedent.temperature.membership(temperature);
				if(antecedentMembership > 0) {
					const consequentKey = rule.consequent.change.name;
					if(!aggregatedOutput[consequentKey] || antecedentMembership > aggregatedOutput[consequentKey]) {
						aggregatedOutput[consequentKey] = antecedentMembership;
					}
				}
			}
			let defuzzifiedValue = 0;
			let sumOfMemberships = 0;
			for(const key in aggregatedOutput) {
				const set = this.temperatureChangeSets[key];
				const membershipValue = aggregatedOutput[key];
				const representativeValue = set.values[1]; // Use the center value for simplicity
				defuzzifiedValue += representativeValue * membershipValue;
				sumOfMemberships += membershipValue;
			}
			return sumOfMemberships === 0 ? 0 : defuzzifiedValue / sumOfMemberships;
		}
		adjustTemperature(currentTemperature, changeValue) {
			return currentTemperature + changeValue;
		}
	}
	// Example Usage
	const thermostat = new FuzzyThermostat();
	let currentTemperature = 12;
	for(let i = 0; i < 5; i++) {
		const change = thermostat.determineChange(currentTemperature);
		showlog(`Current Temperature: ${currentTemperature}, Recommended Change: ${change}`);
		currentTemperature = thermostat.adjustTemperature(currentTemperature, change);
	}
	
	function showlog(){
		var str="";
		for (var i = 0; i < arguments.length; i++) {
	    	str += arguments[i];
		}
		var div=document.getElementById("info");
		if (!div){
			div = document.createElement("div");
			div.setAttribute("id", "info");
			document.body.appendChild(div);
		}
		div.innerHTML += "<br/>" + str;
	}

	</script>
</body>



Author: Win Aung Cho

Comments and Answers

Recent Posts or Questions