Fuzzy logic in AI

I tried to enchance our simple AI with somewhat more sophisticated algorithms, such as fuzzy logic. Here’s FuzzyLite library I used. It needs to be somehow integrated into our code, but my MVS projects are quite different than proposed, so I leave it for you to play.

Check the newest code submission for a piece of code that uses the library.

It’s quite promising thanks to open, human-like architecture of current adventure AI.

Forget about it, there was a major bug in the library itself. Here’s the fixed package:

4shared.com/rar/ak0SMy23/fuzzyLite.html

Fuzzy.h:3:23: fatal error: FuzzyLite.h: No such file or directory

Fix it, please.

FuzzyLite.h is part of mentioned library
you just need one library more to compile it.

My build steps:
1)download fuzzylite-1.03.tar.gz: code.google.com/p/fuzzy-lite/
2)download fuzzylite.rar: 4shared.com/rar/ak0SMy23/fuzzyLite.html
3)unrar fuzzylite.rar to fuzzyLite
4)copy unix-shared folder from fuzzylite-1.03.tar.gz to fuzzyLite
5) add “-fPIC” to unix-shared/src/subdir.mk like here:

g++ -I.. -O3 -Wall -c -fPIC -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"

PS I do it, becouse of this error on my Ubuntu 11.10 amd64:

/usr/bin/ld: ./src/AreaCentroidAlgorithm.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
./src/AreaCentroidAlgorithm.o: could not read symbols: Bad value
collect2: execution of ld failed, exit code 1 

May be x86 systems don’t need “-fPIC” option.
5)mv 38 files from fuzzyLite root to folder src like in fuzzylite-1.03.tar.gz
6)run “make” in fuzzyLite/unix-shared and got libfuzzylite.so.

So, main question is: Should i add lib and headers to system? Or fuzzylite will be added to vcmi source code?
As i know, fuzzylite licensed on Apache License 2.0 terms… so, there is no license trouble or?

Well, the main page of library clearly states that

That is why original library can’t be used. However, I’m hoping for someone else to decide what to do with it, I’m not quite experienced with adding libraries or makefiles :stuck_out_tongue:

Does r2574 build fine? Where should i place headers of fuzzylite?

I tried to copy all folder fuzzylite to vcmi/AI/VCAI and single FuzzyLite.h to vcmi/AI/VCAI but i get such errors:

make[3]: Enter dir `/home/1/game/vcmi/AI/VCAI'
  CXX    libVCAI_la-main.lo
  CXX    libVCAI_la-VCAI.lo
In file included from VCAI.cpp:4:0:
Fuzzy.h:1:0: warning: "INFINITY" redefined [enabled by default]
/usr/include/x86_64-linux-gnu/bits/inf.h:27:0: note: this is the location of the previous definition
Fuzzy.h:2:0: warning: "NAN" redefined [enabled by default]
/usr/include/x86_64-linux-gnu/bits/nan.h:29:0: note: this is the location of the previous definition
In file included from ../../lib/CObjectHandler.h:13:0,
                 from VCAI.cpp:5:
../../lib/GameConstants.h:15:20: error: redefinition of 'const string GameConstants::VCMI_VERSION'
../../lib/GameConstants.h:15:20: error: 'const string GameConstants::VCMI_VERSION' previously declared here
../../lib/GameConstants.h:33:21: error: redefinition of 'const string GameConstants::DATA_DIR'
...

@q4a
Will check this out. Use rev 2570 for now.

Including .cpp’s is definitely evil :slight_smile:
Can you include headers as <fuzzylite/Fuzzylite.h> instead of just <Fuzzylite.h>?
What about using it as dynamic library and including sources into our repository? This is probably the easiest way to distribute library with fixed error(s).

I opened bug 903 bug (feature request) with interesting links about fuzzylite lib and 3 proposal defining FINITY, NAN, isnan() and isinf():
bugs.vcmi.eu/view.php?id=903

Done, since no one protested.

This system alreayd works and is promising, probably can be extended further to other areas.

As I think, Inifinity/NaN definitions should bу in AI\FuzzyLite\defs.h, not in AI\VCAI\Fuzzy.h

So, I head of defs.h for me looks like:

#ifndef FL_DEFS_H
#define	FL_DEFS_H

#ifndef NAN
    static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
    #define NAN (*(const float *) __nan)
#endif

#ifdef _MSC_VER
    #ifndef INFINITY
    union MSVC_FL
    {
       unsigned __int8 Bytes[4];
       float Value;
    };
    static union MSVC_FL INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};
    #define INFINITY (INFINITY_HACK.Value)
    #endif
    #define isnan(x) ((x) != (x))
    template <typename T> bool isinf (T val)
    {
        return val == INFINITY || val == -INFINITY;
    }
    //#include <windows.h> //needs for Sleep()
#endif

#include <iostream>
#include <sstream>

Not Sure about “#include <windows.h>”, because it needs only for Sleep() from AI\FuzzyLite\test.cpp. I changed 4 functions “sleep(2);” to:

#ifdef _MSC_VER
        //Sleep(2);
#else
        sleep(2);
#endif

And if I uncomment //Sleep(2); and //#include <windows.h>, I got error in VS:

1>e:\game\vcmi\ai\vcai\vcai.cpp(270): error C2589: '(' : illegal token on right side of '::'
1>e:\game\vcmi\ai\vcai\vcai.cpp(270): error C2059: syntax error : '::'
1>e:\game\vcmi\ai\vcai\vcai.cpp(290): warning C4244: '*=' : conversion from 'float' to 'ui64', possible loss of data
1>e:\game\vcmi\ai\vcai\vcai.cpp(303): error C2589: '(' : illegal token on right side of '::'
1>e:\game\vcmi\ai\vcai\vcai.cpp(303): error C2059: syntax error : '::'

Attached file contain modified files for r2578 and vcxproj file for FuzzyLite:

AI/FuzzyLite/defs.h
AI/FuzzyLite/FuzzyLite.vcxproj
AI/FuzzyLite/FuzzyLite.vcxproj.user
AI/FuzzyLite/test.cpp
AI/VCAI/Fuzzy.h
AI/VCAI/VCAI.vcxproj
VCMI_VS10.sln

It looks fine for me on Windows and Linux, but I don’t create Linux makefiles for FuzzyLite, so it does not build yet.
And may be somebody review and commit this changes?
vcmi-r2578.7z (5.67 KB)

I’ve committed your patch in r2580 with following changes:

  • FuzzyLite is a static library (.lib) instead of (.dll).
  • VCAI links against FuzzyLite.lib instead of including its sources.

Works for me, however it will require changes to Makefiles.

As for the Sleep() — I’d suggest using boost::this_thread::sleep(boost::posix_time::milliseconds(…)). It’s portable and since we already have boost in dependencies…

Well, for me it does not - some unexpected parsing errors show up. Also, debug configuration can’t be build, reading

error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2'

I tried some solutions from the internet, but they are not applicable because we also link boost.

That happens when linking projects with different “debug levels” (ie. library built in Release mode with project in Debug).
Check your configuration settings (Build -> Configuration) if they’re set appropriately along all the projects and rebuild the whole solution.

Thanks!

Apparently not, everything is set of course to Debug.

I even tried to set _ITERATOR_DEBUG_LEVEL = 2 in preprocessor definitions for Fuzzy Lite, but it was ignored. On the other hand, I could ste it to 0 for VCAI, which however doesn’t solve the problem.

The only other info I got was

LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library

Debug definitions come from yvals.h file:

#ifdef _ITERATOR_DEBUG_LEVEL /* A. _ITERATOR_DEBUG_LEVEL is already defined. */

	/* A1. Validate _ITERATOR_DEBUG_LEVEL. */
	#if _ITERATOR_DEBUG_LEVEL > 2 && defined(_DEBUG)
		#error _ITERATOR_DEBUG_LEVEL > 2 is not supported in debug mode.
	#elif _ITERATOR_DEBUG_LEVEL > 1 && !defined(_DEBUG)
		#error _ITERATOR_DEBUG_LEVEL > 1 is not supported in release mode.
	#endif

	/* A2. Inspect _HAS_ITERATOR_DEBUGGING. */
	#ifdef _HAS_ITERATOR_DEBUGGING /* A2i. _HAS_ITERATOR_DEBUGGING is already defined, validate it. */
		#if _ITERATOR_DEBUG_LEVEL == 2 && _HAS_ITERATOR_DEBUGGING != 1
			#error _ITERATOR_DEBUG_LEVEL == 2 must imply _HAS_ITERATOR_DEBUGGING == 1 .
		#elif _ITERATOR_DEBUG_LEVEL < 2 && _HAS_ITERATOR_DEBUGGING != 0
			#error _ITERATOR_DEBUG_LEVEL < 2 must imply _HAS_ITERATOR_DEBUGGING == 0 .
		#endif
	#else /* A2ii. _HAS_ITERATOR_DEBUGGING is not yet defined, derive it. */
		#if _ITERATOR_DEBUG_LEVEL == 2
			#define _HAS_ITERATOR_DEBUGGING 1
		#else
			#define _HAS_ITERATOR_DEBUGGING 0
		#endif
	#endif /* _HAS_ITERATOR_DEBUGGING */

	/* A3. Inspect _SECURE_SCL. */
	#ifdef _SECURE_SCL /* A3i. _SECURE_SCL is already defined, validate it. */
		#if _ITERATOR_DEBUG_LEVEL > 0 && _SECURE_SCL != 1
			#error _ITERATOR_DEBUG_LEVEL > 0 must imply _SECURE_SCL == 1 .
		#elif _ITERATOR_DEBUG_LEVEL == 0 && _SECURE_SCL != 0
			#error _ITERATOR_DEBUG_LEVEL == 0 must imply _SECURE_SCL == 0 .
		#endif
	#else /* A3ii. _SECURE_SCL is not yet defined, derive it. */
		#if _ITERATOR_DEBUG_LEVEL > 0
			#define _SECURE_SCL 1
		#else
			#define _SECURE_SCL 0
		#endif
	#endif /* _SECURE_SCL */

#else /* B. _ITERATOR_DEBUG_LEVEL is not yet defined. */

	/* B1. Inspect _HAS_ITERATOR_DEBUGGING. */
	#ifdef _HAS_ITERATOR_DEBUGGING /* B1i. _HAS_ITERATOR_DEBUGGING is already defined, validate it. */
		#if _HAS_ITERATOR_DEBUGGING > 1
			#error _HAS_ITERATOR_DEBUGGING must be either 0 or 1 .
		#elif _HAS_ITERATOR_DEBUGGING == 1 && !defined(_DEBUG)
			#error _HAS_ITERATOR_DEBUGGING == 1 is not supported in release mode.
		#endif
	#else /* B1ii. _HAS_ITERATOR_DEBUGGING is not yet defined, default it. */
		#ifdef _DEBUG
			#define _HAS_ITERATOR_DEBUGGING 1
		#else
			#define _HAS_ITERATOR_DEBUGGING 0
		#endif
	#endif /* _HAS_ITERATOR_DEBUGGING */

	/* B2. Inspect _SECURE_SCL. */
	#ifdef _SECURE_SCL /* B2i. _SECURE_SCL is already defined, validate it. */
		#if _SECURE_SCL > 1
			#error _SECURE_SCL must be either 0 or 1 .
		#endif
	#else /* B2ii. _SECURE_SCL is not yet defined, default it. */
		#if _HAS_ITERATOR_DEBUGGING == 1
			#define _SECURE_SCL 1
		#else
			#define _SECURE_SCL 0
		#endif
	#endif /* _SECURE_SCL */

	/* B3. Derive _ITERATOR_DEBUG_LEVEL. */
	#if _HAS_ITERATOR_DEBUGGING
		#define _ITERATOR_DEBUG_LEVEL 2
	#elif _SECURE_SCL
		#define _ITERATOR_DEBUG_LEVEL 1
	#else
		#define _ITERATOR_DEBUG_LEVEL 0
	#endif

#endif /* _ITERATOR_DEBUG_LEVEL */

However, I have no idea what it’s exactly supposed to do and why it’s immune to preprocessor definition, constantly setting level to 0.

Set iterator debug level to 2 and debug to anything in preprocesor defs for debug build
also set any required defs for it.

It may be immune because MSVC may set them internally and disallow to override.
Note that same constants must be applied to whole project and even perhaps to some libs (which we may want recompile)

of course it may be something as trivial as linking with release version of lib while in debug mode
note also that two lib headers may want to declare some common macro in incompatible way.
Changing header order may help. Put also a press to headers like STL

Recheck all your project properties -> C/C++ -> Code Generation -> Runtime Library.
For configuration Debug it should be “Multi-threaded Debug DLL (/MDd)”. If this option is selected, compiler is obliged to define _DEBUG.

Basically you shouldn’t touch _ITERATOR_DEBUG_LEVEL at all. It should default to 2 in debug mode (#ifdef _DEBUG) and to 0 otherwise. It has to have the same value all across the solution.

If this still doesn’t help, give us more details. On linking which project the error occurs? You can set properties -> Linker -> General -> Show Progress to /VERBOSE. It may give us some clue (put the whole build output on some pastebin). Are you using vcxproj files from SVN?

channel9.msdn.com/Shows/Going+De … STL-3-of-n :slight_smile:

Runtime library is already set like that and it does not help.

Here’s an example error:

Error	101	error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in Fuzzy.obj	G:\Programowanie\VCMI\vcmi\AI\VCAI\FuzzyLite.lib(FuzzyAntecedent.obj)

Also, FuzzyLite.lib doesn’t have “Linker”, just 'Librarian" page.

Pastebin link

Your FuzzyLite builds as if it had not been using debug mode. Mine does build correct and sets . Either you changed something in project… or I don’t know. Bug in Visual?

I’ve found one minor thing — in solution the VCAI’s dependancy on FuzzyLite is not set. The fix is committed, try it first. When updating build it could’ve lead to linking VCAI with FuzzyLite from previous build… but that wouldn’t explain what had gone wrong back then.

Is your FuzzyLite.vcxproj exactly the same as the one on SVN and is it the one you have in solution? Are you sure that VCAI links to FuzzyLite.lib that is result of your build? (try deleting it: ./AI/FuzzyLite.lib).

Try defining _DEBUG in FuzzyLite properties (C/C++ -> Preprocessor -> Preprocessor definition). Rebuild FuzzyLite, then try building VCAI.

If it’s still not working, you can try adding in C/C++ -> Command -> Additional options

/D_ITERATOR_DEBUG_LEVEL=2

and then rebuild and so.

However that is rather suppressing the problem (if it works at least) instead of solving.