# 谁养鱼（三）：如何让计算机认识线索

public abstract class Clue {

public abstract boolean match(Solution solution);

}
public class FixedClue extends Clue{

/*固定的房间编号*/
int seat;
Attribute attr;

@Override
public boolean match(Solution solution) {
/*获取此Solution第seat个房子*/
House house = solution.get(seat-1);
/*获取该房子和attr同类型的属性，并比较*/
if(attr==(house.get(attr.getType().getId()-1))){
return true;
}
return false;
}

public FixedClue(int seat, Attribute attr){
this.seat = seat;
this.attr = attr;
}
}
public class AdjacentClue extends Clue{

Attribute attr1;
Attribute attr2;
int dir;//attr2相对attr1的位置，负数为左相邻，正数为右相邻，0为相邻
@Override
public boolean match(Solution solution) {
int seat1=0, seat2=0;
/*获取该Solution具有属性attr1和attr2的房子的位置*/
for(int i=0; i<solution.size(); ++i){
/*获取此Solution第seat个房子*/
House house = solution.get(i);
if(house.get(attr1.getType().getId()-1)==attr1)
seat1=i+1;
if(house.get(attr2.getType().getId()-1)==attr2)
seat2=i+1;
if(seat1!=0&&seat2!=0)
break;
}
/*比较两个位置的关系*/
if(dir==0){
if(seat1-seat2==1 ||seat1-seat2==-1){
return true;
}
}else if(dir==seat2-seat1){
return true;
}
return false;
}

public AdjacentClue(Attribute attr1, Attribute attr2, int dir){
this.attr1 = attr1;
this.attr2 = attr2;
this.dir = dir;
}
}
public class BindClue extends Clue{

Attribute attr1;
Attribute attr2;
@Override
public boolean match(Solution solution) {
Iterator<House> iter = solution.iterator();
/*遍历五个房子，看是否存在房子同时具有属性attr1和attr2*/
while(iter.hasNext()){
House house = iter.next();
if(attr1==(house.get(attr1.getType().getId()-1)) &&
attr2==(house.get(attr2.getType().getId()-1))){
return true;
}
}
return false;
}

public BindClue(Attribute attr1, Attribute attr2){
this.attr1 = attr1;
this.attr2 = attr2;
}
}

import java.util.ArrayList;
import java.util.Iterator;

public class Solution extends ArrayList<House>{

public static ArrayList<Clue> clues=new ArrayList<Clue>();
int matches=0;

static{

}

public int getMatches(){
matches=0;
Iterator<Clue> iter = clues.iterator();
while(iter.hasNext()){
Clue clue = iter.next();
if(clue.match(this))
matches++;
}
/**/
if(God.better==null||matches>God.better.matches){
God.better = (Solution)this.clone();
}
return matches;
}
}

getMatches方法可以作为遗传算法适应度函数的主要组成部分，用来评价个体的生存能力。

09-29 836

02-22 279

07-07 77

02-28 72

11-06 191

01-08 9695

04-17 84

01-07 6339

11-14 149

#### 半年 5 战 5 金：Kaggle 史上最快 GrandMaster 是如何炼成的？

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。