Software Development with Fuzzy Technique
Tutorial
29
White

Nobody viết ngày 21/03/2022

Hi

Today I show you how to develop Software using FUZZY Technique without digging deeply into FUZZY Logic.

A brief FUZZY logic introduction: In real life everything is ANALOGUE. DIGITAL is a human invention. Today everyone talks about DIGITAL and believes that DIGITAL is superior to ANALOGUE. Really? No. It's wrong!. DIGITAL is just the reduced number of samples. A picture is more and more brillant if it has more and more PIXELS (Picture Elements) per quadrat centimeter. On the other hand an ANALOGUE picture has infinite PIXELS per quadrat centimeter. And that is the reason why ANALOGUE is an insurmountable challenge for human. Digital is CRISP Logics while Analogue is FUZZY logics. A CRISP object always has a distinctive value (such as either ONE or ZERO) while a FUZZY object always has a range of infinite values (e.g a range from 0 to ONE).

An infinite range of values tells you the impossibility to implement an algorithm to render all the values. Unlike CRISP object FUZZY object always suggests a value that depends on the observer, not on the reality or real validity. The very first ANALOGUE Computer is the SLIDERULE (or slipstick) which was invented by William Oughtred in 1622 and then continuously used and developed by Isaac Newton (1675), etc.

alt text

But the truest ancient ANALOGUE computer was invented by the unknown in Greece
alt text
(more: read HERE or HERE)

As an IT developer you know that computer is digital and all programming languages (PL) have problems with the precision (or accuracy) when it is about the infinity. The primitives in whatever PL are limited by the implementation. For example:

  • float (4 bytes) represents a range of values between -3.4E+38 to +3.4E+38, Accuracy ~7 decimal digits
  • double (8 bytes) represents a range of values between -1.7E+308 to +1.7E+308, Accuracy ~16 decimal digits.

Nevertheless, the IT world seems to thrive well enough on such a limitation.

Then: why I want to tell you about FUZZY Technique? As mentioned, a FUZZY value is perceived "correct" by observing, not by measuring. You know such perceptions in Math. For example: Math.ceil(0.55) yields 1.0 because you want to round up a number. Similar to ceil() or floor() is Fuzzy technique and it works with all cases. Especially with comparison of non-numeric or variable objects.

IT developers tend to grasp an object as exact as possible and that is in some cases a real problem. Human perception tolerates a lot and usually depends on human mood and human physiology. But IT usually requires precision and exactness so that some IT developers forget or ignore the "human fuzziness" and run into infinite maze of problems. Let start with some examples.

PROBLEM: a certain link must be singled out out of a list or an array of links.

CRISP way (conventional):

for (String s:list)
  if (s.equals("https://congdongjava.com/forum/")) System.out.println("https://congdongjava.com/forum/ exists in array");

Possible Problems:
what happens when cdj is given in uppercase? Loop fails.
what happens when cdj is given in mixed case? Loop fails.
what happens when ccj is given in lowercase? Loop succeeds.
Correction:

for (String s:list)
  if (s.equalsIgnoreCase("https://congdongjava.com/forum/")) System.out.println("https://congdongjava.com/forum/ exists in array");

Loop works perfectly.

FUZZY way: it's a convention that a link must be unique. And the uniqueness lies in the hostname. So we need only to focus on the unique pattern of the hostname.

for (String s:list)
  if (s.toLowerCase().indexOf("congdongjava") > 0) System.out.println("https://congdongjava.com/forum/ exists in array");

Loop works perfectly with the method indexOf() or lastIndexOf(). Btw, this piece of codes was implemented in AdsMonster loves Ads for Blacklist checking.

PROBLEM: search for a string (or a pattern) in a file or a byte array.

CRISP way (conventional):

byte[] content = Files.readAllBytes((new File(file)).toPath());
String big = (new String(content)).toLowerCase();
String cdj = "https://congdongjava.com/forum/";
int le = cdj.length();
int len = content.length -le;
for (int i = 0; i < len; ++i)
  if (big.substring(i, i+le).equals(cdj)) System.out.println(cdj+" is found");

Loop works perfectly.

FUZZY way: it's a convention that an internet link must be unique. And the uniqueness lies in the hostname. So we need only to focus on the unique pattern of the hostname.

byte[] content = Files.readAllBytes((new File(file)).toPath());
String big = (new String(content)).toLowerCase();
String cdj = "congdongjava"; // ignore the superfluous
if (big.indexOf(cdj) >= 0) System.out.println(cdj+" is found");

Loop works perfectly with the method indexOf() or lastIndexOf().

PROBLEM: verify two images whether they are similar or different.

CRISP way (conventional):

public boolean similar(String file1, String file2) throws Exception {
  img1 = ImageIO.read(new File(file1));
  img2 = ImageIO.read(new File(file2));
  if (img1.getWidth() != img2.getWidth() || img1.getHeight() != img2.getHeight()) return false;
  for (int x = 0, width = img1.getWidth(), height = img1.getHeight(); x < width; ++x)
    for (int y = 0; y < height; ++y) if (img1.getRGB(x, y) !=  img2.getRGB(x, y)) return false;
  return true;
}

Possible Problem:
what happens when the hue of only ONE pixel is for human perception the same, but physically different? Loop fails.

FUZZY way: using float to define a range of values which can be categorized into different states: equal, very similar, similar, slightly similar, different, etc. Two images are similar for human if they both look alike, not that they have all the same pixels.

public float similar(String file1, String file2) throws Exception {
  int matched = 0;
  int tolerant = 10;
  int rTol = tolerant << 16;
  int gTol = toleramt << 8;
  img1 = ImageIO.read(new File(file1));
  img2 = ImageIO.read(new File(file2));
  int h = img1.getHeight(), w = img1.getWidth();
  if (w != img2.getWidth() || h != img2.getHeight()) return 0f;
  for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) {
    rgb1 = img1.getRGB(x, y); rgb2 = img2.getRGB(x, y);
    if (rgb1 == rgb2) ++matched;
    else { // +/- tolerant
      red = 0; blue = 0; green = 0;
      red1 = rgb1 & 0xFF0000; red2 = rgb2 & 0xFF0000;
      green1 = rgb1 & 0xFF00; green2 = rgb2 & 0xFF00;
      blue1 = rgb1 & 0xFF; blue2 = rgb2 & 0xFF;
      //
      if (red1 != red2) {
        int X = red2 > red1? red2 - red1 : red1 - red2;
        if (X > rTol) red = X;
      }
      if (green1 != green2) {
        int X = green2 > green1? green2 - green1 : green1 - green2;
        if (X > gTol) green = X;
      }
      if (blue1 != blue2) {
        int X = blue2 > blue1? blue2 - blue1 : blue1 - blue2;
        if (X > tolerant) blue = X;
      }
      if (red == 0 && green == 0 && blue == 0) ++matched;
    }
  }
  return ((float)matched)/(w * h);
}

Loop works perfectly with a tolerant of +/- 10 hue tolerant. The return as a float value allows you to assess a wide range of results between equal, similiar to fully different (e.g. equal: 100% .. 99.8% or 1.0 .. 0.998, similar: 99.79999% .. 98%, ... less than 10%: different). Btw, this piece of codes was implemented in Image Comparison in JAVA

MRA (MapReduce Algorithm) was introduced and used by Google for its Search Engine. The Search with indexOf (or lastIndexOf) can be seen as a simplified MRA. How that? If the string is reduced either "letter" by "letter" to a certain limit or to some certain mapped patterns then the found results of the reduced are the MRA results. The search with "mapped" patterns is implemented in AdsMonster loves Ads too. The mapped patterns are based on their uniqueness within a heap of bytes (or byte array):

  • 'C' for Connect (https link), 'P' for Post or Put (http link), 'G' for Get (http link) at the very first position.
  • '\n', 'P' and 'y' at position i, i+1 and i+5 for the String "Proxy-Connection"
// search for the word Proxy- with 3 mapped locations: 0 for '\n', 1 for 'P' and 5 for 'y'
for (i += 9; i < len; ++i) // HTTP/1.1\r -> 9 characters
  // found \n, P and y at the mapped locations
  if (buf[i] == '\n' && buf[i+1] == 'P' && buf[i+5] == 'y') {
   len -= 6; // 6 characters for 'Proxy-'
   // remove "Proxy-" from the byte array
   System.arraycopy(buf, i+7, buf, i+1, len);
   break;
}

From this viewpoint I won't need to explain more about MRA. The given WIKI link gives you an overview about MRA and how MRA is calculated. The quintessence of a MRA is to reduce the superfluousness within a pattern to the uniqueness of only the most necessary. It is NOT a trick. It is the art of IT programming.

CRISP Programming is like Newton Physics and FUZZY Programming is like Einstein Physics. Absoluteness versus Relativity. Human perception is never absolute, but always relative and that suits perfectly FUZZY programming. Artificial Intelligence or AI for short is the mimicking the human logics and human logics is relative hence AI is best with FUZZY programming. The image comparison is one of the AI reasoning: 100% ... 99.8% is seen as equal, 99.7999% ... 98% is considered as similar, etc. Of course AI and FUZZY programming still rely on CRISP logics as the calculating means to achieve the next human perception.

AI with Self-experience or Machine Learning (ML) is the specialized AI that relies more on FUZZY perception to make an own experience. Example: an obstacle on the way.

  • bump on it and fall: bad experience. Learned it.
  • jump over: good if the obstacle is small. Bad if it is too high. Learned it.
  • circumvent: mostly good. Bad if the obstacle is too large and too high. Learned it.
  • unsurmountable: if all mentioned possibilities are exploited. Learned it.

With the learned ML makes a conclusion whether it jumps over, or circumvents or simply gives up.
alt text
(More: see HERE)
Joe

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

White

Nobody

20 bài viết.
557 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
White
1 2
Chao Cac Ban I was absent for a very long time... To my wonder that Kipalog is still alive. It's a very good news. Today I show you a brief tutor...
Nobody viết 5 tháng trước
1 2
White
1 0
(Ảnh) I found this question in a Vietnamese forum ((Link)). The questioner is certainly not a man who's studied Computer Science (or in Vietnamese...
Nobody viết 4 tháng trước
1 0
White
1 0
This tutorial is a summary of the two last tutorials 1. OCR: (Link) 2. JAVA: (Link) With the knowledge we have about the way how to process an Ima...
Nobody viết 3 tháng trước
1 0
Bài viết liên quan
White
14 4
(Link) (Link) (Link) Ở 2 phần tut trước, mình đã hướng dẫn khá chi tiết cách viết một ứng dụng camera có tích hợp chức năng nhận diện khuôn mặ...
HoangPH viết gần 7 năm trước
14 4
White
24 4
(Ảnh) Nếu máy tính của bạn đã bị lây nhiễm, mã độc có thể lây lan tới trang web của bạn thông qua trình soạn thảo văn bản và (Link). Dùng các mật ...
Juno_okyo viết gần 5 năm trước
24 4
Male avatar
2 1
Bạn đang xài máy ảo Genymotion để chạy thử các ứng dụng của Android. Nếu bạn đang sử dụng Android Studio thì bạn càng dễ dàng mở máy ảo Genymotion...
pdnghiadev viết gần 7 năm trước
2 1
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


White
{{userFollowed ? 'Following' : 'Follow'}}
20 bài viết.
557 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!