惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - mahope

一个算法题解 在ASP.NET web 站点中使用log4net (1.2.9) 在.NET 中实现 AOP 解决Web Service中传递子类实例时,序列化的问题。 Q & A:Does ASP.NET support one-way Web Service operations? openwave:Malformed server response web 项目的 csproj 文件要有对应的.webinfo文件才能在vs里面打开 - mahope 软件需求规范(SRS)指南 写需求文档的一般原则 删除everyone对c:的 访问权限后,运行asp.net出现DirectoryNotFoundException未找到路径“C:\”的一部分 - mahope - 博客园 NHibernate Mapping文件中如何指定类的字节数组属性 NHibernate.ADOException : Unable to perform find 对于事件不能调用BeginInvoke,可改用另外一层包装 IBM面试题试解(关于50条狗、50个人、病狗) Artificial intelligence: Solving problems for the real world 一些面向对象的设计法则 重构、分支语句、虚函数、抽象函数与多态--《重构:改善既有代码设计》之读书心得 NHibernate 执行内嵌类(Nested Class)查询 为内嵌类(Nested Class)配置NHibernate的O/R Mapping文件
Einstein's Riddle 爱因斯坦出的智力题?
mahope · 2005-09-17 · via 博客园 - mahope

现有题号称爱因斯坦出的智力题全世界只有2%能够做出。
------------------------------------------------
1、在一条街上,有5座房子,喷了5种颜色。
2、每个房里住着不同国籍的人
3、每个人喝不同的饮料,抽不同品牌的香烟,养不同的宠物
问题是:谁养鱼?
提示:
1、英国人住红色房子
2、瑞典人养狗
3、丹麦人喝茶
4、绿色房子在白色房子左面
5、绿色房子主人喝咖啡
6、抽Pall Mall 香烟的人养鸟
7、黄色房子主人抽Dunhill 香烟
8、住在中间房子的人喝牛奶
9、 挪威人住第一间房
10、抽Blends香烟的人住在养猫的人隔壁
11、养马的人住抽Dunhill 香烟的人隔壁
12、抽Blue Master的人喝啤酒
13、德国人抽Prince香烟
14、挪威人住蓝色房子隔壁
15、抽Blends香烟的人有一个喝水的邻居

为了便于消除翻译的误差,先将英文版本贴出:

Let us assume that there are five houses of different colors next to each other on the same road. In each house lives a man of a different nationality. Every man has his favorite drink, his favorite brand of cigarettes, and keeps pets of a particular kind.
  1. The Englishman lives in the red house.
  2. The Swede keeps dogs.
  3. The Dane drinks tea.
  4. The green house is just to the left of the white one.
  5. The owner of the green house drinks coffee.
  6. The Pall Mall smoker keeps birds.
  7. The owner of the yellow house smokes Dunhills.
  8. The man in the center house drinks milk.
  9. The Norwegian lives in the first house.
  10. The Blend smoker has a neighbor who keeps cats.
  11. The man who smokes Blue Masters drinks bier.
  12. The man who keeps horses lives next to the Dunhill smoker.
  13. The German smokes Prince.
  14. The Norwegian lives next to the blue house.
  15. The Blend smoker has a neighbor who drinks water.

The question to be answered is: Who keeps fish?

~

现在请你用c#写出解这个问题的程序!相信不少人对此题的第一个想法是通过暴力算法穷举所有可能让计算机进行求解。如果是这样那么你不用辛苦了,下面早就有人这么干过了:
暴力算法解益智题(c#2.0版本):http://www.webasp.net/article/27/26094.htm
C#轻松解决世纪迷题 :http://tech.ccidnet.com/pub/article/c1081_a94061_p1.html
这些解法虽然都是对的,但是正如有人评论道:用笔解这题时候可比用程序看起来省力多了........算的上“轻松”吗?
在这里向大家隆重推荐香港城市大学计算机科学院的陈汉伟博士(Dr. Andy Chun, Hon Wai)的人工智能工具 NSolver
用NSolver如何解决问题?
先举一个简单的例子:
IQ 题:
    农场里有兔子和鸡若干只,知道共20个头和56条腿。问兔子和鸡各多少只?
用c#来解这个问题,可能是:

using System;
public class Rabbit 
{
    
public int rabbit = 0;
    
public int pheasant = 0;
    
public void Run() 
    
{
        
for (rabbit = 0; rabbit<=20; rabbit++)
            
for (pheasant = 0; pheasant<=20; pheasant++)
                
if (rabbit+pheasant==20
                    
&& rabbit*4+pheasant*2==56)
                    Console.WriteLine(
                        
"Rabbit["+rabbit+"] Pheasant["+pheasant+"]");
    }

    
public static void Main() {(new Rabbit()).Run();}
}

有什么不足?

    1、没有变量/未知量的概念
    2、没有什么值合法什么不合法的概念(问题域)
    3、没有变量间的关系/约束概念
    4、所有都Hardcode了,不能扩展到更复杂的问题
    5、暴力破解穷举

比较一下NSolver的解法:

using ai.net.Solver;
using System;
public class Rabbit : Solver 
{
    
public Var rabbits, pheasants;
    
public void Run() 
    
{
        rabbits 
= var(020"Rabbits");
        pheasants 
= var(020"Pheasants");
        Post(rabbits.Sum(pheasants).Eq(
20));
        Post(rabbits.Prod(
4).Sum(pheasants.Prod(2)).Eq(56));
        Console.WriteLine(rabbits
+" "+pheasants);
    }

    
public static void Main() {(new Rabbit()).Run();}
}


现在用NSolver解决爱因斯坦问题:

using System;
using System.Text;
using ai.net.Solver;
using System.Diagnostics;
using System.Collections;

class Einstein : Solver
{
    
public void Run()
    
{
        
// the 房子s
        Var 红房子 = var(04"红房子");
        Var 绿房子 
= var(04"绿房子");
        Var 白房子 
= var(04"白房子");
        Var 黄房子 
= var(04"黄房子");
        Var 蓝房子 
= var(04"蓝房子");
        VarList all房子s 
= varList(红房子, 绿房子, 白房子, 黄房子, 蓝房子);

        
// nationalities
        Var 英国人 = var(04"英国人");
        Var 瑞典人 
= var(04"瑞典人");
        Var 丹麦人 
= var(04"丹麦人");
        Var 挪威人 
= var(04"挪威人");
        Var 德国人 
= var(04"德国人");
        VarList allNationalities 
= varList(英国人, 瑞典人, 丹麦人, 挪威人, 德国人);

        
// beverages
        Var 茶 = var(04"");
        Var 咖啡 
= var(04"咖啡");
        Var 牛奶 
= var(04"牛奶");
        Var 啤酒 
= var(04"啤酒");
        Var 矿泉水 
= var(04"矿泉水");
        VarList allBeverages 
= varList(茶, 咖啡, 牛奶, 啤酒, 矿泉水);

        
// cigarettes
        Var pallMall = var(04"Pall Mall");
        Var dunhill 
= var(04"Dunhill");
        Var 混合烟 
= var(04"混合烟");
        Var BlueMasters 
= var(04"BlueMasters");
        Var Prince 
= var(04"Prince");
        VarList allCigs 
= varList(pallMall, dunhill, 混合烟, BlueMasters, Prince);

        
// pets
        Var 狗 = var(04"");
        Var 鸟 
= var(04"");
        Var 鱼 
= var(04"");// <-- Who owns the 鱼?
        Var 马 = var(04"");
        Var 猫 
= var(04""); 
        VarList allPets 
= varList(狗, 鸟, 鱼, 马, 猫);


        
// More clues
        Post(红房子.Eq(英国人));
        Post(狗.Eq(瑞典人));
        Post(茶.Eq(丹麦人));
        
//The 绿 房子 is on the left of the 白 房子 
        Var gLeft = 白房子.Diff(绿房子);
        gLeft.SetRange(
01);
        Post(gLeft.Neq(
0));


        Post(绿房子.Eq(咖啡));
        Post(pallMall.Eq(鸟));
        Post(黄房子.Eq(dunhill));
        Post(牛奶.Eq(
2));
        Post(挪威人.Eq(
0));

        Var 混合烟Vs鱼 
= 鱼.Diff(混合烟);
        混合烟Vs鱼.SetRange(
-11);
        Post(混合烟Vs鱼.Neq(
0));

        Var 马VsDunhill 
= 马.Diff(dunhill);
        马VsDunhill.SetRange(
-11);
        Post(马VsDunhill.Neq(
0));

        Post(BlueMasters.Eq(啤酒));
        Post(Prince.Eq(德国人));

        Var 蓝Vs挪威人 
= 蓝房子.Diff(挪威人);
        蓝Vs挪威人.SetRange(
-11);
        Post(蓝Vs挪威人.Neq(
0));

        Var 混合烟Vs矿泉水 
= 混合烟.Diff(矿泉水);
        混合烟Vs矿泉水.SetRange(
-11);
        Post(混合烟Vs矿泉水.Neq(
0));

        Post(AllDiff(all房子s));
        Post(AllDiff(allNationalities));
        Post(AllDiff(allBeverages));
        Post(AllDiff(allCigs));
        Post(AllDiff(allPets));

        Activate(all房子s);
        Activate(allNationalities);
        Activate(allBeverages);
        Activate(allCigs);
        Activate(allPets);

        
while (NextSolution())
        
{
            Console.WriteLine(allNationalities);
            Console.WriteLine(all房子s);
            Console.WriteLine(allBeverages);
            Console.WriteLine(allCigs);
            Console.WriteLine(allPets);
        }

    }


    
public static void Main()
    
{
        Einstein solver 
= new Einstein();
        solver.Run();
    }

}


看看这一行行代码吧,多么简洁易懂! 现在你要做的就是陈述问题,NSolver帮你做剩下的!
延伸阅读
         
http://sc.info.gov.hk/gb/www.ugc.edu.hk/rgc/rgcnews9/Pages/6%20AI-C.html
NSolver的好处就是,它不仅仅能解决这个爱因斯坦的问题,它所能解决的是属于一个叫做“Constraint Satisfaction Problems”的一大堆问题。 
      我这里再给大家出个类似的题目,请你用NSolver来解决它:
 Our problem consists of three mice living next to each other in three holes in the wall. Each mouse has a favorite cheese flavor and a favorite TV show. Here are the hints:

  1. Mickey Mouse loves Gouda
  2. Mighty Mouse's favorite TV show is Emergency Room
  3. The mouse that lives in the left hole never misses an episode of Seinfeld
  4. Mickey Mouse and Mighty Mouse have one mouse hole between them
  5. The Simpsons fan does not live on the left of the Brie lover