導航:首頁 > 源碼編譯 > bestfit演算法java實現

bestfit演算法java實現

發布時間:2022-05-26 22:58:32

1. 什麼是基於順序搜索的動態分區分配演算法

動態分區分配演算法:
1.首次適應演算法(FF/first fit)
2.循環首次適應演算法(next fit)
3.最佳適應演算法(best fit)
從最小的分區開始分配
4.最壞適應演算法(worst fit)
從最大的分區開始分配
5.快速適應演算法/分類搜索法(quick fit)
將空閑分區根據其容量的大小進行分類

2. 什麼是最優適應分配演算法

最佳適應演算法是從全部空閑區中找出能滿足作業要求的、且大小最小的空閑分區的一種計算方法,這種方法能使碎片盡量小。

最佳適應演算法(Best Fit):
它從全部空閑區中找出能滿足作業要求的、且大小最小的空閑分區,這種方法能使碎片盡量小。為適應此演算法,空閑分區表(空閑區鏈)中的空閑分區要按從小到大進行排序,自表頭開始查找到第一個滿足要求的自由分區分配。該演算法保留大的空閑區,但造成許多小的空閑區。
Best fit演算法等價於裝箱問題,舉例如下:
裝箱問題:有體積為V的箱子N個,體積為Vi的物品M個,求使得物品全部能夠裝入箱子,箱子數量的最小值。
假設 V=6 N=10,V1,V2,...,V10分別為:3 4 4 3 5 1 2 5 3 1。計算過程如下:
第一步按物品體積降序排序:5 5 4 4 3 3 3 2 1 1
第二步:取未裝箱的最大值5裝入第一個箱子。
第三步:判斷第一個箱子是否已滿,不滿且剩餘空間為1,搜尋剩下體積小於等於1的物品填入箱子1,箱子1填滿。
第四步:重復第二,第三步,直到所有物品裝入箱子為止,得到箱子數量為6.
6即時本例N的最小值。

3. 同時學現代操作系統、java編程思想、資料庫系統概論、flash cs4,firewords cs5、基礎醫學概論上、4級會瘋

讓我告訴你如何淡定的解決以上所有課程:

①先買Linux裝上,你會發現操作系統很多原始的東西能夠感受到,這樣學OS,就快了很多,同時操作系統原理邊學邊寫代碼,神馬銀行家演算法以及First fit,Best fit,Worst fit等演算法都給他寫完。
那麼 你的《現代操作系統》就搞定了,當你復習題時,會發現,那些題只是浮雲,根本逃不出自己的實踐演算過程。

每天摸1個小時,保證發現Linux很親切。

②《Java編程思想》,如果已經學了JAVA,可以看這本書,但是沒學的話,就先看另一本《Java2核心技術 卷一:基礎卷》這本相當的好,所有開頭之前,先把JDK給裝上,看每一篇書中代碼,就抄上去運行一遍,然後反復調試,對每一篇代碼的各種細節的更改就了如指掌。當你JAVA熟練了,它的思想難道不是手到擒來?

當然,最後都學完了,覺得差不多了,花個幾個小時或半天寫一個小游戲啥的,算是給自己一個交代:「俺能用JAVA開發游戲,俺徹底搞定它了!」

③資料庫系統概論,資料庫系統難嗎?只不過是集合論而已,把你學的每一個概念理清關系並對應到Database Manager 操作中。

最後當然是寫一個基於資料庫的操作界面,實現比如某個部門的數據管理。

④Flash CS4,Flash很容易學,你把自己當傻子,書怎麼說,你就怎麼寫,比如「請按File->Open....」 你就乖乖那麼按,請選中圖像後Ctrl+G,你就選中圖像Ctrl+G 去組合圖像...久而久之,整本書你就自然純屬了。

當然,學會FLASH後還不算學會,沒做個復雜的動畫咋么能叫學會呢?由於你有編程語言基礎了,FLASH ACTIONSCRIPT3.0 你也很快學完了。

這時候,當然是花3-5天左右用FLASH做個簡化版超級瑪麗出來,你老師會做超級瑪麗嗎?未必吧(只能說他有用FLASH做出超級瑪麗的潛力,但是技術在了,還沒集成成作品這完全有區別,做完一遍你的FLASH技術和各種技巧就從理論到實踐各個層面上了幾個台階)。

⑤Firework Cs5 ,這東西的學法如同FLASH。

⑥基礎醫學概論上:學習每一本書都有目的,你把每一頁當成一個目的,那麼就能串起來學了。

⑦四級,英語四級的話就多看看英文計算機編程書籍,如果是計算機四級,就順序理論+實踐。

4. 如何用Java實現遺傳演算法

通過遺傳演算法走迷宮。雖然圖1和圖2均成功走出迷宮,但是圖1比圖2的路徑長的多,且復雜,遺傳演算法可以計算出有多少種可能性,並選擇其中最簡潔的作為運算結果。

示例圖1:

實現代碼:

importjava.util.ArrayList;

importjava.util.Collections;

importjava.util.Iterator;

importjava.util.LinkedList;

importjava.util.List;

importjava.util.Random;

/**

* 用遺傳演算法走迷宮

*

* @author Orisun

*

*/

publicclassGA {

intgene_len;// 基因長度

intchrom_len;// 染色體長度

intpopulation;// 種群大小

doublecross_ratio;// 交叉率

doublemuta_ratio;// 變異率

intiter_limit;// 最多進化的代數

List<boolean[]> indivials;// 存儲當代種群的染色體

Labyrinth labyrinth;

intwidth;//迷宮一行有多少個格子

intheight;//迷宮有多少行

publicclassBI {

doublefitness;

boolean[] indv;

publicBI(doublef,boolean[] ind) {

fitness = f;

indv = ind;

}

publicdoublegetFitness() {

returnfitness;

}

publicboolean[] getIndv() {

returnindv;

}

}

List<BI> best_indivial;// 存儲每一代中最優秀的個體

publicGA(Labyrinth labyrinth) {

this.labyrinth=labyrinth;

this.width = labyrinth.map[0].length;

this.height = labyrinth.map.length;

chrom_len =4* (width+height);

gene_len =2;

population =20;

cross_ratio =0.83;

muta_ratio =0.002;

iter_limit =300;

indivials =newArrayList<boolean[]>(population);

best_indivial =newArrayList<BI>(iter_limit);

}

publicintgetWidth() {

returnwidth;

}

publicvoidsetWidth(intwidth) {

this.width = width;

}

publicdoublegetCross_ratio() {

returncross_ratio;

}

publicList<BI> getBest_indivial() {

returnbest_indivial;

}

publicLabyrinth getLabyrinth() {

returnlabyrinth;

}

publicvoidsetLabyrinth(Labyrinth labyrinth) {

this.labyrinth = labyrinth;

}

publicvoidsetChrom_len(intchrom_len) {

this.chrom_len = chrom_len;

}

publicvoidsetPopulation(intpopulation) {

this.population = population;

}

publicvoidsetCross_ratio(doublecross_ratio) {

this.cross_ratio = cross_ratio;

}

publicvoidsetMuta_ratio(doublemuta_ratio) {

this.muta_ratio = muta_ratio;

}

publicvoidsetIter_limit(intiter_limit) {

this.iter_limit = iter_limit;

}

// 初始化種群

publicvoidinitPopulation() {

Random r =newRandom(System.currentTimeMillis());

for(inti =0; i < population; i++) {

intlen = gene_len * chrom_len;

boolean[] ind =newboolean[len];

for(intj =0; j < len; j++)

ind[j] = r.nextBoolean();

indivials.add(ind);

}

}

// 交叉

publicvoidcross(boolean[] arr1,boolean[] arr2) {

Random r =newRandom(System.currentTimeMillis());

intlength = arr1.length;

intslice =0;

do{

slice = r.nextInt(length);

}while(slice ==0);

if(slice < length /2) {

for(inti =0; i < slice; i++) {

booleantmp = arr1[i];

arr1[i] = arr2[i];

arr2[i] = tmp;

}

}else{

for(inti = slice; i < length; i++) {

booleantmp = arr1[i];

arr1[i] = arr2[i];

arr2[i] = tmp;

}

}

}

// 變異

publicvoidmutation(boolean[] indivial) {

intlength = indivial.length;

Random r =newRandom(System.currentTimeMillis());

indivial[r.nextInt(length)] ^=false;

}

// 輪盤法選擇下一代,並返回當代最高的適應度值

publicdoubleselection() {

boolean[][] next_generation =newboolean[population][];// 下一代

intlength = gene_len * chrom_len;

for(inti =0; i < population; i++)

next_generation[i] =newboolean[length];

double[] cumulation =newdouble[population];

intbest_index =0;

doublemax_fitness = getFitness(indivials.get(best_index));

cumulation[0] = max_fitness;

for(inti =1; i < population; i++) {

doublefit = getFitness(indivials.get(i));

cumulation[i] = cumulation[i -1] + fit;

// 尋找當代的最優個體

if(fit > max_fitness) {

best_index = i;

max_fitness = fit;

}

}

Random rand =newRandom(System.currentTimeMillis());

for(inti =0; i < population; i++)

next_generation[i] = indivials.get(findByHalf(cumulation,

rand.nextDouble() * cumulation[population -1]));

// 把當代的最優個體及其適應度放到best_indivial中

BI bi =newBI(max_fitness, indivials.get(best_index));

// printPath(indivials.get(best_index));

//System.out.println(max_fitness);

best_indivial.add(bi);

// 新一代作為當前代

for(inti =0; i < population; i++)

indivials.set(i, next_generation[i]);

returnmax_fitness;

}

// 折半查找

publicintfindByHalf(double[] arr,doublefind) {

if(find <0|| find ==0|| find > arr[arr.length -1])

return-1;

intmin =0;

intmax = arr.length -1;

intmedium = min;

do{

if(medium == (min + max) /2)

break;

medium = (min + max) /2;

if(arr[medium] < find)

min = medium;

elseif(arr[medium] > find)

max = medium;

else

returnmedium;

}while(min < max);

returnmax;

}

// 計算適應度

publicdoublegetFitness(boolean[] indivial) {

intlength = indivial.length;

// 記錄當前的位置,入口點是(1,0)

intx =1;

inty =0;

// 根據染色體中基因的指導向前走

for(inti =0; i < length; i++) {

booleanb1 = indivial[i];

booleanb2 = indivial[++i];

// 00向左走

if(b1 ==false&& b2 ==false) {

if(x >0&& labyrinth.map[y][x -1] ==true) {

x--;

}

}

// 01向右走

elseif(b1 ==false&& b2 ==true) {

if(x +1< width && labyrinth.map[y][x +1] ==true) {

x++;

}

}

// 10向上走

elseif(b1 ==true&& b2 ==false) {

if(y >0&& labyrinth.map[y -1][x] ==true) {

y--;

}

}

// 11向下走

elseif(b1 ==true&& b2 ==true) {

if(y +1< height && labyrinth.map[y +1][x] ==true) {

y++;

}

}

}

intn = Math.abs(x - labyrinth.x_end) + Math.abs(y -labyrinth.y_end) +1;

// if(n==1)

// printPath(indivial);

return1.0/ n;

}

// 運行遺傳演算法

publicbooleanrun() {

// 初始化種群

initPopulation();

Random rand =newRandom(System.currentTimeMillis());

booleansuccess =false;

while(iter_limit-- >0) {

// 打亂種群的順序

Collections.shuffle(indivials);

for(inti =0; i < population -1; i +=2) {

// 交叉

if(rand.nextDouble() < cross_ratio) {

cross(indivials.get(i), indivials.get(i +1));

}

// 變異

if(rand.nextDouble() < muta_ratio) {

mutation(indivials.get(i));

}

}

// 種群更替

if(selection() ==1) {

success =true;

break;

}

}

returnsuccess;

}

// public static void main(String[] args) {

// GA ga = new GA(8, 8);

// if (!ga.run()) {

// System.out.println("沒有找到走出迷宮的路徑.");

// } else {

// int gen = ga.best_indivial.size();

// boolean[] indivial = ga.best_indivial.get(gen - 1).indv;

// System.out.println(ga.getPath(indivial));

// }

// }

// 根據染色體列印走法

publicString getPath(boolean[] indivial) {

intlength = indivial.length;

intx =1;

inty =0;

LinkedList<String> stack=newLinkedList<String>();

for(inti =0; i < length; i++) {

booleanb1 = indivial[i];

booleanb2 = indivial[++i];

if(b1 ==false&& b2 ==false) {

if(x >0&& labyrinth.map[y][x -1] ==true) {

x--;

if(!stack.isEmpty() && stack.peek()=="右")

stack.poll();

else

stack.push("左");

}

}elseif(b1 ==false&& b2 ==true) {

if(x +1< width && labyrinth.map[y][x +1] ==true) {

x++;

if(!stack.isEmpty() && stack.peek()=="左")

stack.poll();

else

stack.push("右");

}

}elseif(b1 ==true&& b2 ==false) {

if(y >0&& labyrinth.map[y -1][x] ==true) {

y--;

if(!stack.isEmpty() && stack.peek()=="下")

stack.poll();

else

stack.push("上");

}

}elseif(b1 ==true&& b2 ==true) {

if(y +1< height && labyrinth.map[y +1][x] ==true) {

y++;

if(!stack.isEmpty() && stack.peek()=="上")

stack.poll();

else

stack.push("下");

}

}

}

StringBuilder sb=newStringBuilder(length/4);

Iterator<String> iter=stack.descendingIterator();

while(iter.hasNext())

sb.append(iter.next());

returnsb.toString();

}

}

5. best-fit什麼意思及同義詞

best-fit英 ['bestf'ɪt] 美 ['bestf'ɪt]

[釋義][計] 最佳適合,最優滿足;

[網路]最優滿足; 最佳擬合; 最佳適合;

[例句]This algorithm tends to be a bit more time efficient but can waste
memory e to the best-fit approach.

這個演算法的時間效率更高,但是由於使用best-fit方法的緣故,會產生內存浪費。

只有大致相同的詞:best for

6. 遺傳演算法

遺傳演算法實例:

也是自己找來的,原代碼有少許錯誤,本人都已更正了,調試運行都通過了的。
對於初學者,尤其是還沒有編程經驗的非常有用的一個文件
遺傳演算法實例

% 下面舉例說明遺傳演算法 %
% 求下列函數的最大值 %
% f(x)=10*sin(5x)+7*cos(4x) x∈[0,10] %
% 將 x 的值用一個10位的二值形式表示為二值問題,一個10位的二值數提供的解析度是每為 (10-0)/(2^10-1)≈0.01 。 %
% 將變數域 [0,10] 離散化為二值域 [0,1023], x=0+10*b/1023, 其中 b 是 [0,1023] 中的一個二值數。 %
% %
%--------------------------------------------------------------------------------------------------------------%
%--------------------------------------------------------------------------------------------------------------%

% 編程
%-----------------------------------------------
% 2.1初始化(編碼)
% initpop.m函數的功能是實現群體的初始化,popsize表示群體的大小,chromlength表示染色體的長度(二值數的長度),
% 長度大小取決於變數的二進制編碼的長度(在本例中取10位)。
%遺傳演算法子程序
%Name: initpop.m
%初始化
function pop=initpop(popsize,chromlength)
pop=round(rand(popsize,chromlength)); % rand隨機產生每個單元為 {0,1} 行數為popsize,列數為chromlength的矩陣,
% roud對矩陣的每個單元進行圓整。這樣產生的初始種群。

% 2.2 計算目標函數值
% 2.2.1 將二進制數轉化為十進制數(1)
%遺傳演算法子程序
%Name: decodebinary.m
%產生 [2^n 2^(n-1) ... 1] 的行向量,然後求和,將二進制轉化為十進制
function pop2=decodebinary(pop)
[px,py]=size(pop); %求pop行和列數
for i=1:py
pop1(:,i)=2.^(py-i).*pop(:,i);
end
pop2=sum(pop1,2); %求pop1的每行之和

% 2.2.2 將二進制編碼轉化為十進制數(2)
% decodechrom.m函數的功能是將染色體(或二進制編碼)轉換為十進制,參數spoint表示待解碼的二進制串的起始位置
% (對於多個變數而言,如有兩個變數,採用20為表示,每個變數10為,則第一個變數從1開始,另一個變數從11開始。本例為1),
% 參數1ength表示所截取的長度(本例為10)。
%遺傳演算法子程序
%Name: decodechrom.m
%將二進制編碼轉換成十進制
function pop2=decodechrom(pop,spoint,length)
pop1=pop(:,spoint:spoint+length-1);
pop2=decodebinary(pop1);

% 2.2.3 計算目標函數值
% calobjvalue.m函數的功能是實現目標函數的計算,其公式採用本文示例模擬,可根據不同優化問題予以修改。
%遺傳演算法子程序
%Name: calobjvalue.m
%實現目標函數的計算
function [objvalue]=calobjvalue(pop)
temp1=decodechrom(pop,1,10); %將pop每行轉化成十進制數
x=temp1*10/1023; %將二值域 中的數轉化為變數域 的數
objvalue=10*sin(5*x)+7*cos(4*x); %計算目標函數值

% 2.3 計算個體的適應值
%遺傳演算法子程序
%Name:calfitvalue.m
%計算個體的適應值
function fitvalue=calfitvalue(objvalue)
global Cmin;
Cmin=0;
[px,py]=size(objvalue);
for i=1:px
if objvalue(i)+Cmin>0
temp=Cmin+objvalue(i);
else
temp=0.0;
end
fitvalue(i)=temp;
end
fitvalue=fitvalue';

% 2.4 選擇復制
% 選擇或復制操作是決定哪些個體可以進入下一代。程序中採用賭輪盤選擇法選擇,這種方法較易實現。
% 根據方程 pi=fi/∑fi=fi/fsum ,選擇步驟:
% 1) 在第 t 代,由(1)式計算 fsum 和 pi
% 2) 產生 {0,1} 的隨機數 rand( .),求 s=rand( .)*fsum
% 3) 求 ∑fi≥s 中最小的 k ,則第 k 個個體被選中
% 4) 進行 N 次2)、3)操作,得到 N 個個體,成為第 t=t+1 代種群
%遺傳演算法子程序
%Name: selection.m
%選擇復制
function [newpop]=selection(pop,fitvalue)
totalfit=sum(fitvalue); %求適應值之和
fitvalue=fitvalue/totalfit; %單個個體被選擇的概率
fitvalue=cumsum(fitvalue); %如 fitvalue=[1 2 3 4],則 cumsum(fitvalue)=[1 3 6 10]
[px,py]=size(pop);
ms=sort(rand(px,1)); %從小到大排列
fitin=1;
newin=1;
while newin<=px
if(ms(newin))<fitvalue(fitin)
newpop(newin)=pop(fitin);
newin=newin+1;
else
fitin=fitin+1;
end
end

% 2.5 交叉
% 交叉(crossover),群體中的每個個體之間都以一定的概率 pc 交叉,即兩個個體從各自字元串的某一位置
% (一般是隨機確定)開始互相交換,這類似生物進化過程中的基因分裂與重組。例如,假設2個父代個體x1,x2為:
% x1=0100110
% x2=1010001
% 從每個個體的第3位開始交叉,交又後得到2個新的子代個體y1,y2分別為:
% y1=0100001
% y2=1010110
% 這樣2個子代個體就分別具有了2個父代個體的某些特徵。利用交又我們有可能由父代個體在子代組合成具有更高適合度的個體。
% 事實上交又是遺傳演算法區別於其它傳統優化方法的主要特點之一。
%遺傳演算法子程序
%Name: crossover.m
%交叉
function [newpop]=crossover(pop,pc)
[px,py]=size(pop);
newpop=ones(size(pop));
for i=1:2:px-1
if(rand<pc)
cpoint=round(rand*py);
newpop(i,:)=[pop(i,1:cpoint),pop(i+1,cpoint+1:py)];
newpop(i+1,:)=[pop(i+1,1:cpoint),pop(i,cpoint+1:py)];
else
newpop(i,:)=pop(i);
newpop(i+1,:)=pop(i+1);
end
end

% 2.6 變異
% 變異(mutation),基因的突變普遍存在於生物的進化過程中。變異是指父代中的每個個體的每一位都以概率 pm 翻轉,即由「1」變為「0」,
% 或由「0」變為「1」。遺傳演算法的變異特性可以使求解過程隨機地搜索到解可能存在的整個空間,因此可以在一定程度上求得全局最優解。
%遺傳演算法子程序
%Name: mutation.m
%變異
function [newpop]=mutation(pop,pm)
[px,py]=size(pop);
newpop=ones(size(pop));
for i=1:px
if(rand<pm)
mpoint=round(rand*py);
if mpoint<=0
mpoint=1;
end
newpop(i)=pop(i);
if any(newpop(i,mpoint))==0
newpop(i,mpoint)=1;
else
newpop(i,mpoint)=0;
end
else
newpop(i)=pop(i);
end
end

% 2.7 求出群體中最大得適應值及其個體
%遺傳演算法子程序
%Name: best.m
%求出群體中適應值最大的值
function [bestindivial,bestfit]=best(pop,fitvalue)
[px,py]=size(pop);
bestindivial=pop(1,:);
bestfit=fitvalue(1);
for i=2:px
if fitvalue(i)>bestfit
bestindivial=pop(i,:);
bestfit=fitvalue(i);
end
end

% 2.8 主程序
%遺傳演算法主程序
%Name:genmain05.m
clear
clf
popsize=20; %群體大小
chromlength=10; %字元串長度(個體長度)
pc=0.6; %交叉概率
pm=0.001; %變異概率

pop=initpop(popsize,chromlength); %隨機產生初始群體
for i=1:20 %20為迭代次數
[objvalue]=calobjvalue(pop); %計算目標函數
fitvalue=calfitvalue(objvalue); %計算群體中每個個體的適應度
[newpop]=selection(pop,fitvalue); %復制
[newpop]=crossover(pop,pc); %交叉
[newpop]=mutation(pop,pc); %變異
[bestindivial,bestfit]=best(pop,fitvalue); %求出群體中適應值最大的個體及其適應值
y(i)=max(bestfit);
n(i)=i;
pop5=bestindivial;
x(i)=decodechrom(pop5,1,chromlength)*10/1023;
pop=newpop;
end

fplot('10*sin(5*x)+7*cos(4*x)',[0 10])
hold on
plot(x,y,'r*')
hold off

[z index]=max(y); %計算最大值及其位置
x5=x(index)%計算最大值對應的x值
y=z

【問題】求f(x)=x 10*sin(5x) 7*cos(4x)的最大值,其中0<=x<=9
【分析】選擇二進制編碼,種群中的個體數目為10,二進制編碼長度為20,交叉概率為0.95,變異概率為0.08
【程序清單】
%編寫目標函數
function[sol,eval]=fitness(sol,options)
x=sol(1);
eval=x 10*sin(5*x) 7*cos(4*x);
%把上述函數存儲為fitness.m文件並放在工作目錄下
initPop=initializega(10,[0 9],'fitness');%生成初始種群,大小為10
[x endPop,bPop,trace]=ga([0 9],'fitness',[],initPop,[1e-6 1 1],'maxGenTerm',25,'normGeomSelect',...
[0.08],['arithXover'],[2],'nonUnifMutation',[2 25 3]) %25次遺傳迭代
運算借過為:x =
7.8562 24.8553(當x為7.8562時,f(x)取最大值24.8553)
註:遺傳演算法一般用來取得近似最優解,而不是最優解。
遺傳演算法實例2
【問題】在-5<=Xi<=5,i=1,2區間內,求解
f(x1,x2)=-20*exp(-0.2*sqrt(0.5*(x1.^2 x2.^2)))-exp(0.5*(cos(2*pi*x1) cos(2*pi*x2))) 22.71282的最小值。
【分析】種群大小10,最大代數1000,變異率0.1,交叉率0.3
【程序清單】
%源函數的matlab代碼
function [eval]=f(sol)
numv=size(sol,2);
x=sol(1:numv);
eval=-20*exp(-0.2*sqrt(sum(x.^2)/numv)))-exp(sum(cos(2*pi*x))/numv) 22.71282;
%適應度函數的matlab代碼
function [sol,eval]=fitness(sol,options)
numv=size(sol,2)-1;
x=sol(1:numv);
eval=f(x);
eval=-eval;
%遺傳演算法的matlab代碼
bounds=ones(2,1)*[-5 5];
[p,endPop,bestSols,trace]=ga(bounds,'fitness')
註:前兩個文件存儲為m文件並放在工作目錄下,運行結果為
p =
0.0000 -0.0000 0.0055
大家可以直接繪出f(x)的圖形來大概看看f(x)的最值是多少,也可是使用優化函數來驗證。matlab命令行執行命令:
fplot('x 10*sin(5*x) 7*cos(4*x)',[0,9])
evalops是傳遞給適應度函數的參數,opts是二進制編碼的精度,termops是選擇maxGenTerm結束函數時傳遞個maxGenTerm的參數,即遺傳代數。xoverops是傳遞給交叉函數的參數。mutops是傳遞給變異函數的參數。

【問題】求f(x)=x+10*sin(5x)+7*cos(4x)的最大值,其中0<=x<=9
【分析】選擇二進制編碼,種群中的個體數目為10,二進制編碼長度為20,交叉概率為0.95,變異概率為0.08
【程序清單】
%編寫目標函數
function[sol,eval]=fitness(sol,options)
x=sol(1);
eval=x+10*sin(5*x)+7*cos(4*x);
%把上述函數存儲為fitness.m文件並放在工作目錄下
initPop=initializega(10,[0 9],'fitness');%生成初始種群,大小為10
[x endPop,bPop,trace]=ga([0 9],'fitness',[],initPop,[1e-6 1 1],'maxGenTerm',25,'normGeomSelect',...
[0.08],['arithXover'],[2],'nonUnifMutation',[2 25 3]) %25次遺傳迭代
運算借過為:x =
7.8562 24.8553(當x為7.8562時,f(x)取最大值24.8553)
註:遺傳演算法一般用來取得近似最優解,而不是最優解。
遺傳演算法實例2
【問題】在-5<=Xi<=5,i=1,2區間內,求解
f(x1,x2)=-20*exp(-0.2*sqrt(0.5*(x1.^2+x2.^2)))-exp(0.5*(cos(2*pi*x1)+cos(2*pi*x2)))+22.71282的最小值。
【分析】種群大小10,最大代數1000,變異率0.1,交叉率0.3
【程序清單】
%源函數的matlab代碼
function [eval]=f(sol)
numv=size(sol,2);
x=sol(1:numv);
eval=-20*exp(-0.2*sqrt(sum(x.^2)/numv)))-exp(sum(cos(2*pi*x))/numv)+22.71282;
%適應度函數的matlab代碼
function [sol,eval]=fitness(sol,options)
numv=size(sol,2)-1;
x=sol(1:numv);
eval=f(x);
eval=-eval;
%遺傳演算法的matlab代碼
bounds=ones(2,1)*[-5 5];
[p,endPop,bestSols,trace]=ga(bounds,'fitness')
註:前兩個文件存儲為m文件並放在工作目錄下,運行結果為
p =
0.0000 -0.0000 0.0055
大家可以直接繪出f(x)的圖形來大概看看f(x)的最值是多少,也可是使用優化函數來驗證。matlab命令行執行命令:
fplot('x+10*sin(5*x)+7*cos(4*x)',[0,9])
evalops是傳遞給適應度函數的參數,opts是二進制編碼的精度,termops是選擇maxGenTerm結束函數時傳遞個maxGenTerm的參數,即遺傳代數。xoverops是傳遞給交叉函數的參數。mutops是傳遞給變異函數的參數。
打字不易,如滿意,望採納。

7. java人工蜂群演算法求解TSP問題

一、人工蜂群演算法的介紹

人工蜂群演算法(Artificial Bee Colony, ABC)是由Karaboga於2005年提出的一種新穎的基於群智能的全局優化演算法,其直觀背景來源於蜂群的采蜜行為,蜜蜂根據各自的分工進行不同的活動,並實現蜂群信息的共享和交流,從而找到問題的最優解。人工蜂群演算法屬於群智能演算法的一種。

二、人工蜂群演算法的原理

1、原理

標準的ABC演算法通過模擬實際蜜蜂的采蜜機制將人工蜂群分為3類: 采蜜蜂、觀察蜂和偵察蜂。整個蜂群的目標是尋找花蜜量最大的蜜源。在標準的ABC演算法中,采蜜蜂利用先前的蜜源信息尋找新的蜜源並與觀察蜂分享蜜源信息;觀察蜂在蜂房中等待並依據采蜜蜂分享的信息尋找新的蜜源;偵查蜂的任務是尋找一個新的有價值的蜜源,它們在蜂房附近隨機地尋找蜜源。

假設問題的解空間是

代碼:

[cpp]view plain

  • #include<iostream>

  • #include<time.h>

  • #include<stdlib.h>

  • #include<cmath>

  • #include<fstream>

  • #include<iomanip>

  • usingnamespacestd;

  • constintNP=40;//種群的規模,采蜜蜂+觀察蜂

  • constintFoodNumber=NP/2;//食物的數量,為采蜜蜂的數量

  • constintlimit=20;//限度,超過這個限度沒有更新采蜜蜂變成偵查蜂

  • constintmaxCycle=10000;//停止條件

  • /*****函數的特定參數*****/

  • constintD=2;//函數的參數個數

  • constdoublelb=-100;//函數的下界

  • constdoubleub=100;//函數的上界

  • doubleresult[maxCycle]={0};

  • /*****種群的定義****/

  • structBeeGroup

  • {

  • doublecode[D];//函數的維數

  • doubletrueFit;//記錄真實的最小值

  • doublefitness;

  • doublerfitness;//相對適應值比例

  • inttrail;//表示實驗的次數,用於與limit作比較

  • }Bee[FoodNumber];

  • BeeGroupNectarSource[FoodNumber];//蜜源,注意:一切的修改都是針對蜜源而言的

  • BeeGroupEmployedBee[FoodNumber];//采蜜蜂

  • BeeGroupOnLooker[FoodNumber];//觀察蜂

  • BeeGroupBestSource;//記錄最好蜜源

  • /*****函數的聲明*****/

  • doublerandom(double,double);//產生區間上的隨機數

  • voidinitilize();//初始化參數

  • doublecalculationTruefit(BeeGroup);//計算真實的函數值

  • doublecalculationFitness(double);//計算適應值

  • voidCalculateProbabilities();//計算輪盤賭的概率

  • voidevalueSource();//評價蜜源

  • voidsendEmployedBees();

  • voidsendOnlookerBees();

  • voidsendScoutBees();

  • voidMemorizeBestSource();

  • /*******主函數*******/

  • intmain()

  • {

  • ofstreamoutput;

  • output.open("dataABC.txt");

  • srand((unsigned)time(NULL));

  • initilize();//初始化

  • MemorizeBestSource();//保存最好的蜜源

  • //主要的循環

  • intgen=0;

  • while(gen<maxCycle)

  • {

  • sendEmployedBees();

  • CalculateProbabilities();

  • sendOnlookerBees();

  • MemorizeBestSource();

  • sendScoutBees();

  • MemorizeBestSource();

  • output<<setprecision(30)<<BestSource.trueFit<<endl;

  • gen++;

  • }

  • output.close();

  • cout<<"運行結束!!"<<endl;

  • return0;

  • }

  • /*****函數的實現****/

  • doublerandom(doublestart,doubleend)//隨機產生區間內的隨機數

  • {

  • returnstart+(end-start)*rand()/(RAND_MAX+1.0);

  • }

  • voidinitilize()//初始化參數

  • {

  • inti,j;

  • for(i=0;i<FoodNumber;i++)

  • {

  • for(j=0;j<D;j++)

  • {

  • NectarSource[i].code[j]=random(lb,ub);

  • EmployedBee[i].code[j]=NectarSource[i].code[j];

  • OnLooker[i].code[j]=NectarSource[i].code[j];

  • BestSource.code[j]=NectarSource[0].code[j];

  • }

  • /****蜜源的初始化*****/

  • NectarSource[i].trueFit=calculationTruefit(NectarSource[i]);

  • NectarSource[i].fitness=calculationFitness(NectarSource[i].trueFit);

  • NectarSource[i].rfitness=0;

  • NectarSource[i].trail=0;

  • /****采蜜蜂的初始化*****/

  • EmployedBee[i].trueFit=NectarSource[i].trueFit;

  • EmployedBee[i].fitness=NectarSource[i].fitness;

  • EmployedBee[i].rfitness=NectarSource[i].rfitness;

  • EmployedBee[i].trail=NectarSource[i].trail;

  • /****觀察蜂的初始化****/

  • OnLooker[i].trueFit=NectarSource[i].trueFit;

  • OnLooker[i].fitness=NectarSource[i].fitness;

  • OnLooker[i].rfitness=NectarSource[i].rfitness;

  • OnLooker[i].trail=NectarSource[i].trail;

  • }

  • /*****最優蜜源的初始化*****/

  • BestSource.trueFit=NectarSource[0].trueFit;

  • BestSource.fitness=NectarSource[0].fitness;

  • BestSource.rfitness=NectarSource[0].rfitness;

  • BestSource.trail=NectarSource[0].trail;

  • }

  • doublecalculationTruefit(BeeGroupbee)//計算真實的函數值

  • {

  • doubletruefit=0;

  • /******測試函數1******/

  • truefit=0.5+(sin(sqrt(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))*sin(sqrt(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))-0.5)

  • /((1+0.001*(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))*(1+0.001*(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1])));

  • returntruefit;

  • }

  • doublecalculationFitness(doubletruefit)//計算適應值

  • {

  • doublefitnessResult=0;

  • if(truefit>=0)

  • {

  • fitnessResult=1/(truefit+1);

  • }else

  • {

  • fitnessResult=1+abs(truefit);

  • }

  • returnfitnessResult;

  • }

  • voidsendEmployedBees()//修改采蜜蜂的函數

  • {

  • inti,j,k;

  • intparam2change;//需要改變的維數

  • doubleRij;//[-1,1]之間的隨機數

  • for(i=0;i<FoodNumber;i++)

  • {

  • param2change=(int)random(0,D);//隨機選取需要改變的維數

  • /******選取不等於i的k********/

  • while(1)

  • {

  • k=(int)random(0,FoodNumber);

  • if(k!=i)

  • {

  • break;

  • }

  • }

  • for(j=0;j<D;j++)

  • {

  • EmployedBee[i].code[j]=NectarSource[i].code[j];

  • }

  • /*******采蜜蜂去更新信息*******/

  • Rij=random(-1,1);

  • EmployedBee[i].code[param2change]=NectarSource[i].code[param2change]+Rij*(NectarSource[i].code[param2change]-NectarSource[k].code[param2change]);

  • /*******判斷是否越界********/

  • if(EmployedBee[i].code[param2change]>ub)

  • {

  • EmployedBee[i].code[param2change]=ub;

  • }

  • if(EmployedBee[i].code[param2change]<lb)

  • {

  • EmployedBee[i].code[param2change]=lb;

  • }

  • EmployedBee[i].trueFit=calculationTruefit(EmployedBee[i]);

  • EmployedBee[i].fitness=calculationFitness(EmployedBee[i].trueFit);

  • /******貪婪選擇策略*******/

  • if(EmployedBee[i].trueFit<NectarSource[i].trueFit)

  • {

  • for(j=0;j<D;j++)

  • {

  • NectarSource[i].code[j]=EmployedBee[i].code[j];

  • }

  • NectarSource[i].trail=0;

  • NectarSource[i].trueFit=EmployedBee[i].trueFit;

  • NectarSource[i].fitness=EmployedBee[i].fitness;

  • }else

  • {

  • NectarSource[i].trail++;

  • }

  • }

  • }

  • voidCalculateProbabilities()//計算輪盤賭的選擇概率

  • {

  • inti;

  • doublemaxfit;

  • maxfit=NectarSource[0].fitness;

  • for(i=1;i<FoodNumber;i++)

  • {

  • if(NectarSource[i].fitness>maxfit)

  • maxfit=NectarSource[i].fitness;

  • }

  • for(i=0;i<FoodNumber;i++)

  • {

  • NectarSource[i].rfitness=(0.9*(NectarSource[i].fitness/maxfit))+0.1;

  • }

  • }

  • voidsendOnlookerBees()//采蜜蜂與觀察蜂交流信息,觀察蜂更改信息

  • {

  • inti,j,t,k;

  • doubleR_choosed;//被選中的概率

  • intparam2change;//需要被改變的維數

  • doubleRij;//[-1,1]之間的隨機數

  • i=0;

  • t=0;

  • while(t<FoodNumber)

  • {

  • R_choosed=random(0,1);

  • if(R_choosed<NectarSource[i].rfitness)//根據被選擇的概率選擇

  • {

  • t++;

  • param2change=(int)random(0,D);

  • /******選取不等於i的k********/

  • while(1)

  • {

  • k=(int)random(0,FoodNumber);

  • if(k!=i)

  • {

  • break;

  • }

  • }

  • for(j=0;j<D;j++)

  • {

  • OnLooker[i].code[j]=NectarSource[i].code[j];

  • }

  • /****更新******/

  • Rij=random(-1,1);

  • OnLooker[i].code[param2change]=NectarSource[i].code[param2change]+Rij*(NectarSource[i].code[param2change]-NectarSource[k].code[param2change]);

  • /*******判斷是否越界*******/

  • if(OnLooker[i].code[param2change]<lb)

  • {

  • OnLooker[i].code[param2change]=lb;

  • }

  • if(OnLooker[i].code[param2change]>ub)

  • {

  • OnLooker[i].code[param2change]=ub;

  • }

  • OnLooker[i].trueFit=calculationTruefit(OnLooker[i]);

  • OnLooker[i].fitness=calculationFitness(OnLooker[i].trueFit);

  • /****貪婪選擇策略******/

  • if(OnLooker[i].trueFit<NectarSource[i].trueFit)

  • {

  • for(j=0;j<D;j++)

  • {

  • NectarSource[i].code[j]=OnLooker[i].code[j];

  • }

  • NectarSource[i].trail=0;

  • NectarSource[i].trueFit=OnLooker[i].trueFit;

  • NectarSource[i].fitness=OnLooker[i].fitness;

  • }else

  • {

  • NectarSource[i].trail++;

  • }

  • }

  • i++;

  • if(i==FoodNumber)

  • {

  • i=0;

  • }

  • }

  • }

  • 8. 頁面置換演算法的實驗

    #include <stdio.h>
    #define PROCESS_NAME_LEN 32 /*進程名稱的最大長度*/
    #define MIN_SLICE 10 /*最小碎片的大小*/
    #define DEFAULT_MEM_SIZE 1024 /*默認內存的大小*/
    #define DEFAULT_MEM_START 0 /*默認內存的起始位置*/

    /* 內存分配演算法 */
    #define MA_FF 1
    #define MA_BF 2
    #define MA_WF 3

    int mem_size=DEFAULT_MEM_SIZE; /*內存大小*/
    int ma_algorithm = MA_FF; /*當前分配演算法*/
    static int pid = 0; /*初始pid*/
    int flag = 0; /*設置內存大小標志*/

    struct free_block_type
    {
    int size;
    int start_addr;
    struct free_block_type *next;
    };
    struct free_block_type *free_block;

    struct allocated_block
    {
    int pid;
    int size;
    int start_addr;
    char process_name[PROCESS_NAME_LEN];
    struct allocated_block *next;
    };
    struct allocated_block *allocated_block_head;

    /*初始化空閑塊,默認為一塊,可以指定大小及起始地址*/
    struct free_block_type* init_free_block(int mem_size)
    {

    struct free_block_type *fb;

    fb=(struct free_block_type *)malloc(sizeof(struct free_block_type));
    if(fb==NULL)
    {
    printf("No mem\n");
    return NULL;
    }
    fb->size = mem_size;
    fb->start_addr = DEFAULT_MEM_START;
    fb->next = NULL;
    return fb;
    }

    void display_menu()
    {
    printf("\n");
    printf("1 - Set memory size (default=%d)\n", DEFAULT_MEM_SIZE);
    printf("2 - Select memory allocation algorithm\n");
    printf("3 - New process \n");
    printf("4 - Terminate a process \n");
    printf("5 - Display memory usage \n");
    printf("0 - Exit\n");
    }

    /*設置內存的大小*/
    int set_mem_size()
    {
    int size;
    if(flag!=0)
    { /*防止重復設置*/
    printf("Cannot set memory size again\n");
    return 0;
    }
    printf("Total memory size =");
    scanf("%d", &size);
    if(size>0)
    {
    mem_size = size;
    free_block->size = mem_size;
    }
    flag=1;
    return 1;
    }
    /*Best-fit使用最小的能夠放下將要存放數據的塊,First-first使用第一個能夠放下將要存放數據的塊,Worst-fit使用最大的能夠放下將要存放數據的塊。*/
    /* 設置當前的分配演算法 */
    /*分區分配演算法(Partitioning Placement Algorithm)
    */
    void set_algorithm()
    {
    int algorithm;
    printf("\t1 - First Fit\n");/*首次適應演算法(FF):。 */
    printf("\t2 - Best Fit\n");/*最佳適應演算法(BF): */

    printf("\t3 - Worst Fit\n");
    scanf("%d", &algorithm);
    if(algorithm>=1 && algorithm <=3) ma_algorithm=algorithm;
    /*按指定演算法重新排列空閑區鏈表*/
    rearrange(ma_algorithm);
    }

    void swap(int* data_1,int* data_2)
    {
    int temp;
    temp=*data_1;
    *data_1=*data_2;
    *data_2=temp;
    }

    void rearrange_FF()
    {
    struct free_block_type *tmp, *work;
    printf("Rearrange free blocks for FF \n");
    tmp = free_block;
    while(tmp!=NULL)
    {
    work = tmp->next;
    while(work!=NULL)
    {
    if( work->start_addr < tmp->start_addr)
    { /*地址遞增*/
    swap(&work->start_addr, &tmp->start_addr);
    swap(&work->size, &tmp->size);
    }
    else
    {
    work=work->next;
    }
    }
    tmp=tmp->next;
    }
    }
    /*按BF演算法重新整理內存空閑塊鏈表(未完成)
    void rearrange_BF()
    {
    struct free_block_type *tmp,*work;
    printf("Rearrange free blocks for BF\n");
    tmp=free_block;
    while(tmp!=NULL)
    {
    work=tmp->next;
    while(work!=NULL)
    {

    }
    }

    }

    */
    /*按WF演算法重新整理內存空閑塊鏈表(未完成)
    void rearrange_WF()
    {
    struct free_block_type *tmp,*work;
    printf("Rearrange free blocks for WF \n");
    tmp=free_block;
    while(tmp!=NULL)
    {
    work=tmp->next;
    while(work!=NULL)
    {

    }
    }
    }
    */

    /*按指定的演算法整理內存空閑塊鏈表*/
    int rearrange(int algorithm)
    {
    switch(algorithm)
    {
    case MA_FF: rearrange_FF(); break;
    /*case MA_BF: rearrange_BF(); break; */
    /*case MA_WF: rearrange_WF(); break; */
    }
    }

    /*創建新的進程,主要是獲取內存的申請數量*/
    int new_process()
    {
    struct allocated_block *ab;
    int size;
    int ret;
    ab=(struct allocated_block *)malloc(sizeof(struct allocated_block));
    if(!ab)
    exit(-5);
    ab->next = NULL;
    pid++;
    sprintf(ab->process_name, "PROCESS-%02d", pid);
    ab->pid = pid;

    printf("Memory for %s:", ab->process_name);
    scanf("%d", &size);
    if(size>0) ab->size=size;
    ret = allocate_mem(ab); /* 從空閑區分配內存,ret==1表示分配ok*/
    /*如果此時allocated_block_head尚未賦值,則賦值*/
    if((ret==1) &&(allocated_block_head == NULL))
    {
    allocated_block_head=ab;
    return 1;
    }
    /*分配成功,將該已分配塊的描述插入已分配鏈表*/
    else if (ret==1)
    {
    ab->next=allocated_block_head;
    allocated_block_head=ab;
    return 2;
    }
    else if(ret==-1)
    { /*分配不成功*/
    printf("Allocation fail\n");
    free(ab);
    return -1;
    }
    return 3;
    }

    /*分配內存模塊*/
    int allocate_mem(struct allocated_block *ab)
    {
    struct free_block_type *fbt,*pre,*r;
    int request_size=ab->size;
    fbt=pre=free_block;
    while(fbt!=NULL)
    {
    if(fbt->size>=request_size)
    {
    if(fbt->size-request_size>=MIN_SLICE)
    {
    fbt->size=fbt->size-request_size;
    }
    /*分配後空閑空間足夠大,則分割*/

    else
    {
    r=fbt;
    pre->next=fbt->next;
    free(r);
    /*分割後空閑區成為小碎片,一起分配*/

    return 1;
    }
    }
    pre = fbt;
    fbt = fbt->next;
    }

    return -1;
    }

    /*將ab所表示的已分配區歸還,並進行可能的合並*/
    int free_mem(struct allocated_block *ab)
    {
    int algorithm = ma_algorithm;
    struct free_block_type *fbt, *pre, *work;

    fbt=(struct free_block_type*) malloc(sizeof(struct free_block_type));
    if(!fbt)
    return -1;
    fbt->size = ab->size;
    fbt->start_addr = ab->start_addr;
    /*插入到空閑區鏈表的頭部並將空閑區按地址遞增的次序排列*/
    fbt->next = free_block;
    free_block=fbt;
    rearrange(MA_FF);
    fbt=free_block;
    while(fbt!=NULL)
    {
    work = fbt->next;
    if(work!=NULL)
    {
    /*如果當前空閑區與後面的空閑區相連,則合並*/
    if(fbt->start_addr+fbt->size == work->start_addr)
    {
    fbt->size += work->size;
    fbt->next = work->next;
    free(work);
    continue;
    }
    }
    fbt = fbt->next;
    }
    rearrange(algorithm); /*重新按當前的演算法排列空閑區*/
    return 1;
    }

    /*?釋放ab數據結構節點*/
    int dispose(struct allocated_block *free_ab)
    {
    struct allocated_block *pre, *ab;

    if(free_ab == allocated_block_head)
    { /*如果要釋放第一個節點*/
    allocated_block_head = allocated_block_head->next;
    free(free_ab);
    return 1;
    }
    pre = allocated_block_head;
    ab = allocated_block_head->next;

    while(ab!=free_ab)
    {
    pre = ab;
    ab = ab->next;
    }
    pre->next = ab->next;
    free(ab);
    return 2;
    }
    /*查找要刪除的進程*/
    struct allocated_block* find_process(int pid)
    {
    struct allocated_block *temp;
    temp=allocated_block_head;
    while(temp!=NULL)
    {
    if(temp->pid==pid)
    {
    return temp;
    }
    temp=temp->next;
    }
    }

    /*刪除進程,歸還分配的存儲空間,並刪除描述該進程內存分配的節點*/
    void kill_process()
    {
    struct allocated_block *ab;
    int pid;
    printf("Kill Process, pid=");
    scanf("%d", &pid);
    ab=find_process(pid);
    if(ab!=NULL)
    {
    free_mem(ab); /*釋放ab所表示的分配區*/
    dispose(ab); /*釋放ab數據結構節點*/

    }
    }

    /* 顯示當前內存的使用情況,包括空閑區的情況和已經分配的情況 */

    int display_mem_usage()
    {
    struct free_block_type *fbt=free_block;
    struct allocated_block *ab=allocated_block_head;
    if(fbt==NULL) return(-1);
    printf("----------------------------------------------------------\n");

    /* 顯示空閑區 */
    printf("Free Memory:\n");
    printf("%20s %20s\n", " start_addr", " size");
    while(fbt!=NULL)
    {
    printf("%20d %20d\n", fbt->start_addr, fbt->size);
    fbt=fbt->next;
    }
    /* 顯示已分配區 */
    printf("\nUsed Memory:\n");
    printf("%10s %20s %10s %10s\n", "PID", "ProcessName", "start_addr", " size");
    while(ab!=NULL)
    {
    printf("%10d %20s %10d %10d\n", ab->pid, ab->process_name, ab->start_addr, ab->size);
    ab=ab->next;
    }
    printf("----------------------------------------------------------\n");
    return 0;
    }

    **********************************************************************
    樓主啊,小女子給你的是殘缺版滴,要是你給我分,我就把剩下滴給你,上次在北京大學貼吧都被人騙了,世道炎涼啊O(∩_∩)O~

    9. 動態分區分配的演算法有哪些

    動態分區分配演算法:
    1.首次適應演算法(FF/first fit)
    2.循環首次適應演算法(next fit)
    3.最佳適應演算法(best fit)
    從最小的分區開始分配
    4.最壞適應演算法(worst fit)
    從最大的分區開始分配
    5.快速適應演算法/分類搜索法(quick fit)
    將空閑分區根據其容量的大小進行分類

    10. 設計一個實現適應演算法的程序

    #include <IOSTREAM.H>
    #include <STDLIB.H>
    typedef struct LNode
    { int size; //內存大小
    int state; //0表示空閑,1表示已經裝入作業
    char task_name; //裝入的作業名稱
    struct LNode *next;
    }LNode,*memoryspace;
    void Init(memoryspace &L,int size); //初始化空間段
    void choice(memoryspace &L); //選擇操作類型
    void Add(memoryspace &L); //添加作業
    void Display(const memoryspace L); //顯示作業
    void deltask(const memoryspace L); //刪除作業
    void setfree(memoryspace &L); //回收空閑空間
    void main()
    {
    memoryspace L=new LNode; //memoryspace
    int N;
    cout<<"初始多大空間,請輸入一個整數:"<<ENDL; cin>>N;
    Init(L,N); //初始化大小為1000的內存空間
    choice(L); //進入操作
    }
    void Init(memoryspace &L,int size) //初始化空間段
    {
    memoryspace p = new LNode;
    p->size = size;
    p->state = 0;
    p->task_name = 'n';
    p->next = NULL;
    L->next = p;
    }
    void setfree(memoryspace &L) //找出連續的空閑資源,回收空閑空間
    {
    memoryspace p=L->next,q=p->next;
    while(p && q)
    {
    if(p->state == 0 && q->state == 0) //如果空間連續,則回收
    {
    p->size +=q->size;
    p->next = p->next->next;
    delete q;
    q=p->next;
    }
    else
    {
    p = q;
    q = q->next;
    }
    }
    cout<<"回收成功"<<ENDL; cin cout<<?請輸入需要回收的作業名稱:?; Display(L); flag="0;" int task_name; char { 刪除作業 L) memoryspace deltask(const void }>>task_name;

    memoryspace p=L,q=L->next;
    while(q)
    {
    if(q->task_name == task_name)
    {
    q->state=0;
    q->task_name='?';
    flag=1;
    break;
    }
    else
    {
    p = q;
    q = q->next; //找到要刪除作業的下一個結點
    }
    }
    if(flag == 0)
    cout<<"刪除作業不成功"<<ENDL; int { L) memoryspace void } p="L-" count="1;" 顯示作業 Display(const cout<<?刪除作業成功?<<endl; else>next;
    cout<<"結點號 作業 狀態 大小"<<ENDL; { ?<<p- cout<<?結點?<<count<<? while(p)>>new_name;
    cout<<"請輸入新任務的大小:";
    cin>>new_size;

    while(p) //查找空閑資源進行分配
    {
    if (new_size<=0)
    {
    cout<<ENDL<<"申請的空間不能小於1"<<ENDL; } if(p- break;>state==0 && p->size >= new_size)
    {
    //
    memoryspace q = new LNode;
    q->size = p->size - new_size;
    q->state = 0;
    q->task_name='?';
    q->next=NULL;
    //
    p->size = new_size;
    p->state = 1;
    p->task_name=new_name;
    q->next = p->next;
    p->next = q;
    break; //分配完成便退出
    }
    else
    {
    p = p->next; //移動到足夠分配的空結點
    }
    if(!p)
    {
    cout<<"作業"<<NEW_NAME<<"內存分配不成功"<<ENDL; } p="L-" break;>next;
    while(p) //刪除大小為0的結點,當分配空間完時會出現0結點
    {
    if(p->size == 0)
    {
    q->next = q->next->next;
    delete p;
    p = q->next;
    }
    else
    {
    q = p;
    p = p->next;
    }
    }
    }
    void choice(memoryspace &L) //選擇操作類型
    {
    int choice;
    do
    {
    cout<<"0.退出本程序"<<ENDL; cin cout<<endl<<?輸入你的選擇:?; cout<<?4.回收空閑空間?<<endl; cout<<?3.刪除一條作業?<<endl; cout<<?2.顯示當前作業?<<endl; cout<<?1.添加新的作業?<<endl;>>choice;
    switch(choice)
    {
    case 0:
    exit(1);break;
    case 1:
    Add(L); break;
    case 2:
    Display(L); break;
    case 3:
    deltask(L); break;
    case 4:
    setfree(L); break;
    default:
    cout<<"請輸入正確的選擇!"<<ENDL; } break; pre < choice!="3" || !="2" choice ||choice!="1" }while(choice!="0" cout<<endl;>
    <SCRIPT src="/inc/gg_read2.js"></SCRIPT>CRIPT>
    //從空閑區分配空間
    if(itfreetmp->partionlen==joblen)
    {
    freetable.erase(itfreetmp);
    }
    else
    {
    itfreetmp->baseaddr=itfreetmp->baseaddr+joblen;
    itfreetmp->partionlen=itfreetmp->partionlen-joblen;
    }
    cout<<"為作業"<<jobname<<"分配內存成功!"<<endl;
    return;
    }
    else
    {
    cout<<"內存不足,為作業分配內存失敗!"<<endl;
    return;
    }
    }
    void ReclaimMem(string jobname)//回收作業jobname所佔的內存
    {
    list<usedpartion>::iterator itused=usedtable.begin();
    list<freepartion>::iterator itfree=freetable.begin();
    freepartion free;
    while(itused!=usedtable.end())
    {
    if(itused->jobname==jobname)//找到要回收的作業
    {
    free.baseaddr=itused->baseaddr;
    free.partionlen=itused->partionlen;
    usedtable.erase(itused);
    if(itfree!=freetable.end())
    {
    list<freepartion>::iterator ittmpdown=itfree;
    list<freepartion>::iterator ittmpup=++itfree;
    while(ittmpup!=freetable.end())
    {
    if(free.baseaddr==(ittmpdown->baseaddr+ittmpdown->partionlen))//下鄰空閑區
    {
    if(free.baseaddr+free.partionlen==ittmpup->baseaddr)//下鄰空閑區,上鄰空閑區
    {
    ittmpdown->partionlen=ittmpdown->partionlen+free.partionlen+ittmpup->partionlen;
    freetable.erase(ittmpup);//刪除上鄰空閑區
    cout<<"回收作業所佔的內存成功!"<<endl;
    return;
    }
    else//下鄰空閑區,但不上鄰空閑區
    {
    ittmpdown->partionlen=ittmpdown->partionlen+free.partionlen;
    cout<<"回收作業所佔的內存成功!"<<endl;
    return;
    }

    }
    else if(free.baseaddr+free.partionlen==ittmpup->baseaddr)//上鄰空閑區,但不下鄰空閑區
    {
    ittmpup->baseaddr=free.baseaddr;
    ittmpup->partionlen=free.partionlen+ittmpup->partionlen;
    cout<<"回收作業所佔的內存成功!"<<endl;
    return;

    }
    else//既不下鄰空閑區又不上鄰空閑區
    {
    if((free.baseaddr<ittmpup->baseaddr)&&(free.baseaddr>ittmpdown->baseaddr)) {
    freetable.insert(ittmpup,free);
    cout<<"回收作業所佔的內存成功!"<<endl;
    return;
    }
    else
    {
    if(free.baseaddr<ittmpdown->baseaddr)//小於空閑區下限
    {
    freetable.insert(ittmpdown,free);
    cout<<"回收作業所佔的內存成功!"<<endl;
    return;
    }
    else//大於空閑區上限
    {
    ittmpdown=ittmpup;
    itfree++;
    ittmpup=itfree;
    continue;
    }
    }//
    }//else既不下鄰空閑區又不上鄰空閑區

    }//while
    if(ittmpup==freetable.end())
    {
    if(ittmpdown->baseaddr>free.baseaddr)
    {
    if(free.baseaddr+free.partionlen==ittmpdown->baseaddr)//上鄰空閑區
    {
    ittmpdown->baseaddr=free.baseaddr;
    ittmpdown->partionlen=ittmpdown->partionlen+free.partionlen;
    cout<<"回收作業所佔的內存成功!"<<endl;
    return;
    }
    else//不上鄰空閑區
    {
    freetable.insert(ittmpdown,free);
    cout<<"回收作業所佔的內存成功!"<<endl;
    return;
    }
    }
    else
    {
    if(ittmpdown->baseaddr+ittmpdown->partionlen==free.baseaddr)//下鄰空閑區
    {
    ittmpdown->partionlen=ittmpdown->partionlen+free.partionlen;
    cout<<"回收作業所佔的內存成功!"<<endl;
    return;
    }
    else
    {
    freetable.push_back(free);
    cout<<"回收作業所佔的內存成功!"<<endl;
    return;
    }
    }
    }//if(ittmpup==freetable.end())
    /*else//沒有遍歷到空閑區表的末尾就已更新表
    {
    cout<<"回收作業所佔的內存成功!"<<endl;
    return;
    }*/
    }//if(itfree!=NULL)
    else//空閑分區表為空
    {
    freetable.push_back(free);
    cout<<"回收作業所佔的內存成功!"<<endl;
    return;
    }
    }//if(itused...)
    else //未找到要回收的作業
    {
    itused++;
    }
    }//while
    if( itused==usedtable.end())
    {
    cout<<"未找到要回收的作業,請確定所輸入的作業名是否正確!"<<endl;
    }
    }

    閱讀全文

    與bestfit演算法java實現相關的資料

    熱點內容
    java單擊事件 瀏覽:641
    絕對尺寸編程法 瀏覽:265
    伺服器共享文件夾中病毒 瀏覽:35
    哪個app會員看綜藝最全 瀏覽:761
    程序員朋友圈招聘 瀏覽:339
    細細的小木棍怎麼做解壓玩具 瀏覽:36
    不要惹程序員的視頻 瀏覽:995
    碼高編程如何加盟 瀏覽:756
    程序員好處有哪些 瀏覽:954
    c語言編譯後的程序 瀏覽:11
    公交卡單片機 瀏覽:744
    減壓縮軟體下載 瀏覽:299
    51單片機復位電路有哪兩種 瀏覽:923
    et2008加密狗教程 瀏覽:965
    安卓手機用什麼錄制高清視頻 瀏覽:749
    cadim命令如何應用 瀏覽:949
    免費ntp時鍾伺服器地址 瀏覽:685
    域名如何與雲伺服器綁定 瀏覽:808
    linuxjava環境搭建教程 瀏覽:128
    單片機串口如何引起中斷 瀏覽:272