在終端機下達指令: brew install scala
2016年5月18日 星期三
Scala IDE for Eclipse 安裝
Help->Install New Software->Add
Name: Scala IDE
Location: http://download.scala-ide.org/sdk/lithium/e44/scala211/stable/site
把 Scala IDE for Eclipse 和 Scala IDE for Eclipse Development Support兩個選項打勾
Name: Scala IDE
Location: http://download.scala-ide.org/sdk/lithium/e44/scala211/stable/site
把 Scala IDE for Eclipse 和 Scala IDE for Eclipse Development Support兩個選項打勾
然後等待一段時間下載
開一個Scala專案
輸入HolloWorld.scala 檔案
object HellWorld extends App {
println ( "hello,")
開一個Scala專案
輸入HolloWorld.scala 檔案
}
執行看看有沒有差
不能使用Application 因為
Application has been deprecated from scala 2.9, probably it has been deleted in scala 2.11 (it still exists in scala 2.10) even though at the moment I can't find proofs for that, use App instead.
執行看看有沒有差
不能使用Application 因為
Application has been deprecated from scala 2.9, probably it has been deleted in scala 2.11 (it still exists in scala 2.10) even though at the moment I can't find proofs for that, use App instead.
2016年5月8日 星期日
基因演算法運用及程式碼
碩士期間剛好有一堂是最佳演算法,講到基因演算法的運用和作業
剛好有機會記下來,與網友們一起討論使用
題目是:
6.挑選與複製方法
7.交配與交配機率
8.突變與突變機率
9.終止條件
1. 題目
Max f (x 1, x2
) = 21.5 + x1 sin(4πx1) + x2 sin(20πx2
)
−3. 0 ≤ x1 ≤ 12.1, 4.1 ≤
x2 ≤ 5.8
試以基因演算法求最大值f
2. 基因演算法(以流程圖或虛擬碼表示即可)
3. 設計編碼方式 (使用二進位編碼來代表 x1 與 x2
的值)
4. 決定群體規模 (族群數量)
5. 設計適應函數 (決定個體適應度的評估標準)
6. 決定挑選與複製方法
7. 定義交配與交配機率
8. 定義突變與突變機率
9. 決定終止條件
10. 結果與討論(含收斂過程圖)
我的解法
2.基因演算法流程圖
我的解法
2.基因演算法流程圖
3.編碼方式
假設某個數值x1=11.1
X1==11.1;
If(x1==11.1)
{
x1=(x1+3)*10;
x1_binary=Integer.toBinary (x1);
x1_binary=Integer.toBinary (x1);
}
x1_binary==10001101;
假設某個數值x1 二進位= 10001101
x1_binary==10001101;
If(x1_binary==10001101)
{
x1=Integer. valueOf(x1_binary);
x1=x1/10-3;
}
X1==11.1;
假設某個數值x1=-3.0
X1==0;
If(x1==-3.0)
{
x1=(x1+3)*10;
x1_binary=Integer.toBinary (x1);
x1_binary=Integer.toBinary (x1);
}x1_binary== 00000000;
假設某個數值x1 二進位= 00000000
x1_binary== 00000000;
If(x1_binary==00000000)
{
x1=Integer.valueOf(x1_binary);
x1=x1/10-3;
}
X1==0;
4.群體規模
(族群數量)
初始群體規模為
int groundCount=10;
經過第一次交配,以後規模擴增為
copulationCount=groundCount*2;
5.適應函數
直接代入
Max f (x1, x2 ) = 21.5 + x1 sin(4πx1) + x2 sin(20πx2 )
求f當適應函數
6.挑選與複製方法
樣本基因利用公式計算出來的各個f值
取出
前10%優秀f樣本基因、前30%優秀樣本基因、前70%優秀樣本基因、前90%優秀樣本基因(輪盤式選擇)
複製完後放入交配池(激增兩倍基因)
7.交配與交配機率
一開始隨機取出兩對基因
把第一對x1前四碼和第二對x2前四碼交配出新基因的x1
把第一對x2前四碼和第二對x1前四碼交配出新基因的x2
強制交配出特定數量
8.突變與突變機率
在交配的時候,有十分之一的機率發生突變,當發生突變的時候,隨機x1或x2發生突變,突變是隨機挑選binary的其中一個做改變。
e.gs., 10001101=>10101101(第三位發生突變)
9.終止條件
其繁延代數跑完,就是終止條件
10.結果與討論
因為我起始基因個數非常的低(設定為10組),所以我依靠演化(交配、突變)來使基因越來越好,但非常依靠繁延代數。
總而言之,每一代的基因都有往好的地方發展出去!所以繁延代數越高,基因品質越好。
以下是程式碼:
以下是程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gene; | |
import java.text.DecimalFormat; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.Random; | |
public class Gene | |
{ | |
private static int groundCount; //The sum of individual genes | |
private static int copulationCount; //sum of copulation is grundCount double | |
private static int period; // sum of generation | |
private static double copulationRate; //is Probability can copulation | |
private static double mutationRate;//is Probability can mutation | |
private static double max_x1; | |
private static double min_x1; | |
private static int randParameter1_x1; | |
private static int randParameter2_x1; | |
private static double max_x2; | |
private static double min_x2; | |
private static int randParameter1_x2; | |
private static int randParameter2_x2; | |
private final static DecimalFormat df = new DecimalFormat("##.#"); | |
private static Random rand=new Random(); | |
Gene(int groundCount,int period,double copulationRate,double mutationRate | |
,double max_x1,double min_x1,int randParameter1_x1,int randParameter2_x1 | |
,double max_x2,double min_x2,int randParameter1_x2,int randParameter2_x2) | |
{ | |
this.groundCount=groundCount; | |
copulationCount=groundCount*2; | |
this.period=period; | |
this.copulationRate=copulationRate; | |
this.mutationRate=mutationRate; | |
this.max_x1=max_x1; | |
this.min_x1=min_x1; | |
this.randParameter1_x1=randParameter1_x1; | |
this.randParameter2_x1=randParameter2_x1; | |
this.max_x2=max_x2; | |
this.min_x2=min_x2; | |
this.randParameter1_x2=randParameter1_x2; | |
this.randParameter2_x2=randParameter2_x2; | |
} | |
/* | |
由於主函式被宣告為static,而 | |
static method內的程式碼只能呼叫static method,因此 | |
為了要讓主函式main呼叫,故必須宣告為static | |
method。 | |
*/ | |
public ArrayList<Parameter> createListParameter() | |
{ | |
ArrayList<Parameter> listParameter=new ArrayList<Parameter>(); | |
Parameter parameter; | |
//查看亂數數值有沒有符合範圍,有的話add | |
for(int i=0;i<groundCount;i++) | |
{ | |
parameter=new Parameter(); | |
double number=100; //先設定這樣 | |
while (number>max_x1|| number<min_x1) | |
{ | |
int integer=rand.nextInt(randParameter1_x1)+randParameter2_x1; | |
double doub=rand.nextDouble(); | |
number=Double.parseDouble(df.format(integer+doub)); | |
} | |
parameter.setNumberX1(number); | |
number=100; | |
while (number>max_x2|| number<min_x2) | |
{ | |
int integer=rand.nextInt(randParameter1_x2)+randParameter2_x2; | |
double doub=rand.nextDouble(); | |
number=Double.parseDouble(df.format(integer+doub)); | |
} | |
parameter.setNumberX2(number); | |
// 十進位轉二進位 | |
parameter.setBinaryX1(getBinary(parameter.getNumberX1()));; | |
parameter.setBinaryX2(getBinary(parameter.getNumberX2()));; | |
parameter=equation(parameter); | |
listParameter.add(parameter); | |
} | |
listParameter=sortListParameter(listParameter); | |
return listParameter; | |
} | |
//輪盤式選擇(roulette wheel selection) | |
public ArrayList<Parameter> reproduction(ArrayList<Parameter> ListParameter) | |
{ | |
ArrayList<Parameter> ListParameterReproduction=new ArrayList<Parameter>() ; | |
ArrayList<Parameter> ListParameterTop10Percents=new ArrayList<Parameter>(ListParameter.subList(0, (int)(groundCount*0.1))); | |
ArrayList<Parameter> ListParameterTop30Percents=new ArrayList<Parameter>(ListParameter.subList(0, (int)(groundCount*0.3))); | |
ArrayList<Parameter> ListParameterTop70Percents=new ArrayList<Parameter>(ListParameter.subList(0, (int)(groundCount*0.7))); | |
ArrayList<Parameter> ListParameterTop90Percents=new ArrayList<Parameter>(ListParameter.subList(0, (int)(groundCount*0.9))); | |
ListParameterReproduction.addAll(ListParameterTop10Percents); | |
ListParameterReproduction.addAll(ListParameterTop30Percents); | |
ListParameterReproduction.addAll(ListParameterTop70Percents); | |
ListParameterReproduction.addAll(ListParameterTop90Percents); | |
// ArrayList<Parameter> ListParameterCreate=new ArrayList<Parameter>(createListParameter().subList(0, (int)(groundCount*0.3))); | |
// ListParameterReproduction.addAll(ListParameterCreate); | |
ListParameterReproduction=sortListParameter(ListParameterReproduction); | |
return ListParameterReproduction; | |
} | |
/* | |
* 設定交配機率,兩個基因的x1前四碼做交換,和x2的後四碼做交換,但如果交換後數值超過限定值的話,就不交配,隨機其中一組 | |
*/ | |
public ArrayList<Parameter> copulation(ArrayList<Parameter> ListParameter) | |
{ | |
//隨機挑兩個數值 | |
int option1,option2; | |
String option1_Binaryx1, option1_Binaryx2; | |
String option2_Binaryx1, option2_Binaryx2; | |
String subBinary; | |
boolean isRunning=true; | |
ArrayList<Parameter> ListParameterCopulation=new ArrayList<Parameter>(); | |
Parameter parameter=new Parameter(); | |
for(int i=0;i<copulationCount;i++) | |
{ | |
parameter=new Parameter(); | |
while(isRunning==true) | |
{ | |
int Probability=rand.nextInt(10); | |
//突變 | |
if(Probability<1) | |
{ | |
int mutationOption=rand.nextInt(ListParameter.size()); | |
parameter=mutation(ListParameter.get(mutationOption)); | |
} | |
else | |
{ | |
option1=rand.nextInt(ListParameter.size()); | |
option2=rand.nextInt(ListParameter.size()); | |
option1_Binaryx1=ListParameter.get(option1).getBinaryX1(); | |
option1_Binaryx2=ListParameter.get(option1).getBinaryX2(); | |
option2_Binaryx1=ListParameter.get(option2).getBinaryX1(); | |
option2_Binaryx2=ListParameter.get(option2).getBinaryX2(); | |
subBinary=option1_Binaryx1.substring(0, 4)+option2_Binaryx1.substring(4, 8); | |
parameter.setBinaryX1(subBinary); | |
subBinary=option2_Binaryx2.substring(0, 4)+option1_Binaryx2.substring(4, 8); | |
parameter.setBinaryX2(subBinary); | |
parameter=getDecimal(parameter); | |
} | |
if (checkRange(parameter)==true) | |
isRunning=false; | |
} | |
isRunning=true; | |
parameter=equation(parameter); | |
ListParameterCopulation.add(parameter); | |
parameter=null; | |
} | |
ListParameterCopulation.addAll(ListParameter); | |
ListParameterCopulation=sortListParameter(ListParameterCopulation); | |
return ListParameterCopulation; | |
} | |
public Parameter mutation(Parameter parameter) | |
{ | |
int mutationPart=rand.nextInt(2); | |
String binary; | |
boolean isRunning=true; | |
while(isRunning==true) | |
{ | |
mutationPart=rand.nextInt(8); | |
binary=parameter.getBinaryX1(); | |
if(mutationPart==0) | |
{ | |
mutationPart=rand.nextInt(8); | |
binary=parameter.getBinaryX1(); | |
if(binary.charAt(mutationPart)=='1') | |
{ | |
if(mutationPart==0) | |
binary='0'+binary.substring(1); | |
else if(mutationPart==7) | |
binary=binary.substring(0, 7)+'0'; | |
else | |
binary=binary.substring(0, mutationPart)+'0'+binary.substring(mutationPart+1); | |
System.out.println("原本:"+parameter.getBinaryX1()+"部分"+mutationPart+"後來:"+binary); | |
} | |
else | |
{ | |
if(mutationPart==0) | |
binary='1'+binary.substring(1); | |
else if(mutationPart==7) | |
binary=binary.substring(0, 7)+'1'; | |
else | |
binary=binary.substring(0, mutationPart)+'1'+binary.substring(mutationPart+1); | |
System.out.println("原本:"+parameter.getBinaryX1()+"部分"+mutationPart+"後來:"+binary); | |
} | |
parameter.setBinaryX1(binary); | |
parameter=getDecimal(parameter); | |
parameter=equation(parameter); | |
if(checkRange(parameter)==true) | |
{ | |
isRunning=false; | |
return parameter; | |
} | |
} | |
else | |
{ | |
mutationPart=rand.nextInt(8); | |
binary=parameter.getBinaryX2(); | |
if(binary.charAt(mutationPart)=='1') | |
{ | |
if(mutationPart==0) | |
binary='0'+binary.substring(1); | |
else if(mutationPart==7) | |
binary=binary.substring(0, 7)+'0'; | |
else | |
binary=binary.substring(0, mutationPart)+'0'+binary.substring(mutationPart+1); | |
System.out.println("原本:"+parameter.getBinaryX1()+"部分"+mutationPart+"後來:"+binary); | |
} | |
else | |
{ | |
if(mutationPart==0) | |
binary='1'+binary.substring(1); | |
else if(mutationPart==7) | |
binary=binary.substring(0, 7)+'1'; | |
else | |
binary=binary.substring(0, mutationPart)+'1'+binary.substring(mutationPart+1); | |
System.out.println("原本:"+parameter.getBinaryX1()+"部分"+mutationPart+"後來:"+binary); | |
} | |
parameter.setBinaryX1(binary); | |
parameter=getDecimal(parameter); | |
parameter=equation(parameter); | |
if(checkRange(parameter)==true) | |
{ | |
isRunning=false; | |
return parameter; | |
} | |
} | |
} | |
return parameter; | |
} | |
public void showListParameter(ArrayList<Parameter> list ,String action) | |
{ | |
System.out.println(action); | |
for(int i=0;i<list.size();i++) | |
System.out.println("第"+(i+1)+"組:x1:"+list.get(i).getNumberX1()+",xb1:"+list.get(i).getBinaryX1()+",x2:"+list.get(i).getNumberX2()+",xb2:"+list.get(i).getBinaryX2()+",f:"+list.get(i).getF()); | |
} | |
public ArrayList<Parameter> sortListParameter(ArrayList<Parameter> list) | |
{ | |
//sort array | |
Collections.sort(list,new Comparator<Parameter>() | |
{ | |
@Override | |
public int compare(Parameter o1, Parameter o2) | |
{ | |
// TODO Auto-generated method stub | |
if(Double.compare(o1.getF(), o2.getF())==-1) | |
return 1;//o1>o2 ,higher then the front | |
else if(Double.compare(o1.getF(), o2.getF())==0) | |
return 0; | |
else | |
return -1; | |
} | |
}); | |
return list; | |
} | |
public Parameter getDecimal(Parameter parameter) | |
{ | |
String binaryX1=parameter.getBinaryX1(); | |
String binaryX2=parameter.getBinaryX2(); | |
DecimalFormat df = new DecimalFormat("##.#"); | |
double numberX1=(double)(Integer.valueOf(binaryX1,2))/10-3; | |
double numberX2=(double)(Integer.valueOf(binaryX2,2))/10-3; | |
numberX1=Double.parseDouble(df.format(numberX1)); | |
numberX2=Double.parseDouble(df.format(numberX2)); | |
parameter.setNumberX1(numberX1); | |
parameter.setNumberX2(numberX2); | |
return parameter; | |
} | |
public String getBinary(double number) | |
{ | |
int binary; | |
binary=(int)((number+3)*10); | |
//二進位補零算式 | |
String binaryString=Integer.toBinaryString(0x100 | binary).substring(1); | |
return binaryString; | |
} | |
public boolean checkRange(Parameter parameter) | |
{ | |
double numberX1=parameter.getNumberX1(); | |
double numberX2=parameter.getNumberX2(); | |
if(min_x1<=numberX1 && numberX1<=max_x1 && min_x2<=numberX2 && numberX2<=max_x2) | |
return true; | |
else | |
return false; | |
} | |
public Parameter equation( Parameter parameter) | |
{ | |
double f=21.5+parameter.numberX1*Math.sin(4.0*Math.PI*parameter.numberX1)+parameter.numberX2*Math.sin(20.0*Math.PI*parameter.numberX2); | |
f=Double.parseDouble(df.format(f)); | |
parameter.setF(f); | |
return parameter; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gene; | |
import java.text.DecimalFormat; | |
import java.util.ArrayList; | |
import java.util.Random; | |
/** | |
* | |
* @author Mark | |
* topic: | |
* Max f (x1, x2 ) = 21.5 + x1 sin(4πx1) + x2 sin(20πx2 ) | |
* then −3.0 ≤ x1 ≤ 12.1, 4.1 ≤ x2 ≤ 5.8 | |
* | |
* Genetic algorithm to find the maximum value f | |
* | |
*/ | |
public class Main | |
{ | |
public final static int groundCount=20; //The sum of individual genes | |
public final static int copulationCount=groundCount*2; //sum of copulation is grundCount double | |
public final static int period=100; // sum of generation | |
public final static double copulationRate=0.5; //is 0.6 Probability can copulation | |
public final static double mutationRate=0.1;//is 0.1 Probability can mutation | |
public final static double max_x1=12.1; | |
public final static double min_x1=-3.0; | |
public final static int randParameter1_x1=16; | |
public final static int randParameter2_x1=-3; | |
public final static double max_x2=5.8; | |
public final static double min_x2=4.1; | |
public final static int randParameter1_x2=2; | |
public final static int randParameter2_x2=4; | |
public final static DecimalFormat df = new DecimalFormat("##.#"); | |
public static Random rand=new Random(); | |
public static void main(String[] args) | |
{ | |
Gene gene=new Gene(groundCount,period,copulationRate,mutationRate | |
,max_x1,min_x1,randParameter1_x1,randParameter2_x1, | |
max_x2,min_x2,randParameter1_x2,randParameter2_x2); | |
//create initial ListParameter | |
ArrayList<Parameter> ListParameter=new ArrayList<Parameter>(gene.createListParameter()); | |
gene.showListParameter(ListParameter,"createListParameter"); | |
for(int i=0;i<period;i++) | |
{ | |
ArrayList<Parameter> ListParameterReproduction=new ArrayList<Parameter>(gene.reproduction(ListParameter)); | |
ArrayList<Parameter> ListParameterCopulation=new ArrayList<Parameter>(gene.copulation(ListParameterReproduction)); | |
ListParameter.removeAll(ListParameter); | |
ListParameter.addAll(ListParameterCopulation.subList(0, copulationCount)); | |
gene.showListParameter(ListParameter,"第"+(i+1)+"代"); | |
} | |
System.out.println(Integer.toBinaryString(0x100 | 0).substring(1)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gene; | |
public class Parameter | |
{ | |
double numberX1; | |
String binaryX1; | |
double numberX2; | |
String binaryX2; | |
double f; | |
void setNumberX1(double numberX1) | |
{ | |
this.numberX1=numberX1; | |
} | |
void setBinaryX1(String binaryX1) | |
{ | |
this.binaryX1=binaryX1; | |
} | |
double getNumberX1() | |
{ | |
return numberX1; | |
} | |
String getBinaryX1() | |
{ | |
return binaryX1; | |
} | |
void setNumberX2(double numberX2) | |
{ | |
this.numberX2=numberX2; | |
} | |
void setBinaryX2(String binaryX2) | |
{ | |
this.binaryX2=binaryX2; | |
} | |
double getNumberX2() | |
{ | |
return numberX2; | |
} | |
String getBinaryX2() | |
{ | |
return binaryX2; | |
} | |
void setF(double f) | |
{ | |
this.f=f; | |
} | |
double getF() | |
{ | |
return f; | |
} | |
void clear() | |
{ | |
numberX1=0; | |
binaryX1=null; | |
numberX2=0; | |
binaryX1=null; | |
f=0; | |
} | |
} |
訂閱:
文章 (Atom)