Skip to content

Instantly share code, notes, and snippets.

@TechplexEngineer
Created April 18, 2015 03:23
Show Gist options
  • Select an option

  • Save TechplexEngineer/cb2841cf6f39ed219267 to your computer and use it in GitHub Desktop.

Select an option

Save TechplexEngineer/cb2841cf6f39ed219267 to your computer and use it in GitHub Desktop.
Code to add fuzzy numbers
#Input fuzzy number. Possibility distribution
pos = {
156: 0,
157: 0.25,
158: 0.5,
159: 0.75,
160: 1,
161: 1,
162: 1,
163: 1,
164: 1,
165: 1,
166: 0.75,
167: 0.5,
168: 0.25,
169: 0
};
FuzzyAdd = (a, b, collapsed) ->
out = {};
if not collapsed?
collapsed = true;
for ka,va of a
for kb,vb of b
kai = parseInt(ka);
kbi = parseInt(kb);
key = kai+kbi;
key = key.toString();
if(collapsed)
if(key of out)
out[key] = Math.max(Math.min(va,vb), out[key])
else
out[key] = Math.min(va,vb)
else
if(key not of out)
out[key] = [];
out[key].push(Math.min(va,vb));
return out
FuzzyPrettyPrint = (result) ->
collapsed = false;
keys = Object.keys(result);
if (typeof result[keys[0]] == "number")
collapsed = true
keys = keys.map (i) ->
+i #parseint
min = Math.min.apply(null, keys)
max = Math.max.apply(null, keys)
mid = min+(max-min)/2
prevlen = undefined;
counter = 2;
for k, v of result
sp = ""
if (v.length <= prevlen and not collapsed)
sp = Array(counter++).join("\t");
prevlen = v.length
console.log("%d%s\t%s", k, sp, if collapsed then v else v.join('\t'));#.join('\t')
# We are doing a tripple add here pos + pos + pos
first = FuzzyAdd(pos, pos, true)
secon = FuzzyAdd(pos, first, false)
FuzzyPrettyPrint(secon)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment