Смекни!
smekni.com

Градиентный метод первого порядка (стр. 11 из 11)

if L1<>nil then L1.Caption:=inttostr(bestPPM.Time);

if L2<>nil then L2.Caption:=inttostr(bestPPM.ProcCount);

Result:=true

end

else Result:=false;

//-->>

{ Memo.Lines.Add('');

Memo.Lines.Add('--- Min ---');

Memo.Lines.Add('');

for i:=0 to AllMoves.Count-1 do

begin

PPM:=AllMoves[i];

Memo.Lines.Add(inttostr(PPM.UIN)+' <>

'+inttostr(PPM.processor)+':'+inttostr(PPM.afterUIN)+' Time=

'+inttostr(PPM.Time)+' PC='+inttostr(PPM.ProcCount));

if PPM.CurrentState then Memo.Lines.Add('Was current state!')

end;}

//<<--

ClearPossibleMoves(AllMoves);

DeselectTask;

end;

function ComparePPT(Item1, Item2: Pointer): Integer;

begin

if PProcTask(Item1).StartTime<PProcTask(Item2).StartTime then Result:=-

1

else

if PProcTask(Item1).StartTime>PProcTask(Item2).StartTime then Result:=1

else Result:=0

end;

procedure TSubMerger.OptimizeAuto(Form:TForm;L1,L2:TLabel);

var i,j,k:integer;

List,UINList:TList;

PPT,nextPPT:PProcTask;

Time:integer;

MatchError:boolean;

NewProc:TList;

NOP:integer;

NoChange:boolean;

StartFrom,NewStartFrom:integer;

BackList:TList;

BackTime:integer;

begin

while OptimizeOneStep(L1,L2) do Form.Update;

Time:=GetTime;

UINList:=TList.Create;

NewStartFrom:=0;

repeat

StartFrom:=NewStartFrom;

NoChange:=true;

for i:=0 to Procs.Count-2 do

begin

NewStartFrom:=i+1;

List:=Procs[i];

for j:=0 to List.Count-1 do UINList.Add(List[j]);

List:=Procs[i+1];

for j:=0 to List.Count-1 do UINList.Add(List[j]);

UINList.Sort(ComparePPT);

MatchError:=false;

PPT:=UINList.first;

for j:=1 to UINList.Count-1 do

begin

nextPPT:=UINList[j];

if (PPT.StartTime = nextPPT.StartTime) or

(PPT.StartTime+PPT.Length>nextPPT.StartTime) then

begin

MatchError:=true;

break

end;

PPT:=nextPPT;

end;

if not MatchError then

begin

SaveBackUp(BackList);

BackTime:=GetTime;

NOP:=Procs.Count-1;

ClearLinks(true);

ClearProcs(false);

for j:=0 to UINList.Count-1 do

begin

PPT:=UINList[j];

PPT.ProcNum:=i+1;

AllProcTasks.delete(AllProcTasks.indexOf(PPT));

end;

for j:=0 to AllProcTasks.Count-1 do

begin

PPT:=AllProcTasks[j];

if PPT.ProcNum>i+1 then PPT.ProcNum:=PPT.ProcNum-1

end;

for j:=0 to UINList.Count-1 do AllProcTasks.add(UINList[j]);

FormLinkTasksAndSetTimes(NOP);

if BackTime>=GetTime then

begin

NoChange:=false;

NewStartFrom:=0;

while BackList.Count<>0 do

begin

PPT:=BackList.first;

BackList.delete(0);

dispose(PPT)

end;

end

else RestoreBackUp(BackList,NOP+1,true);

break;

end;

UINList.Clear;

end;

UINList.Clear;

until NoChange;

UINList.Destroy;

end;

procedure TSubMerger.SaveBackUp(var List:Tlist);

var backPPT,PPT:PProcTask;

i:integer;

begin

List:=TList.Create;

for i:=0 to AllProcTasks.Count-1 do

begin

PPT:=AllProcTasks[i];

new(backPPT);

backPPT^:=PPT^;

backPPT.Prev:=nil;

List.add(backPPT);

end;

end;

procedure TSubMerger.RestoreBackUp(var

List:Tlist;NOP:integer;ClearCurrent:boolean);

var backPPT,PPT:PProcTask;

i:integer;

begin

Selected:=nil;

ClearLinks(true);

ClearProcs(true);

for i:=0 to List.Count-1 do

begin

backPPT:=List[i];

new(PPT);

PPT^:=backPPT^;

AllProcTasks.add(PPT);

if ClearCurrent then dispose(backPPT);

end;

if ClearCurrent then List.Destroy;

FormLinkTasksAndSetTimes(NOP);

end;

end.