Sunteți pe pagina 1din 12

Modelarea sistemelor economice Tema 1 Model de partitionare circulara Neamtu Claudia an III, grupa 4

Intr-o sala a unei institutii de invatamant exista m = 2n + 1, n 1 statii de lucru cu urmatoarele proprietati:

Tema 1. Model de partitionare circulara


Descriere model

P1. Fiecare statie este fixa P2. Fiecare statie a are ca vecini statiile b si c P3. Fiecare statie poate efectua k m operatii precizate, iar pe o perioada precizata de timp, fiecare statie este deservita de o singura persoana. Se cere un model matematic care sa precizeze cele n moduri distincte ale celor m statii cu verificarea proprietatilor P1, P2 si P3. Pentru modelul proiectat sa se scrie un program corespunzator. Cazuri particulare: C1. m=7 C2. m=17 C3. m=27 C4. m=47

Pentru a face o precizare vom presupune ca initial statiile sunt numerotate 1,2, , m . Aceasta numerotare indica faptul ca oricare ar fi statia i, 2 i m 1 , ea are ca vecini pe i 1 si i + 1 . Pe o perioada de timp precizata care poate fi o zi, o ora etc. oice statie trebuie sa aiba aceeasi vecini. Schimbandu-se perioada vor fi schimbati si vecinii, astfel incat in doua perioade dinstincte aceeasi statie nu trebuie sa aiba aceeasi vecini. Intr-o prima perioada statiile vor fi: 1,2,3, , m,1 si este evident ca orice statie are doi vecini distincti. Pentru identificarea pozitiei statiei in restul perioadelor se procedeaza astfel: pentru perioada 2 ordinul statiilor e dat de ordinea precizata in perioada 1 considerand statiile numerotate din doi in doi, adica 1,3,5, . Daca m este impar atunci este evident ca nefiind divizibil cu 3, din permutarea 1 nu vor rezulta transpozitii. Pentru precizarea ordinului cand perioada este 3 se considera ordinea din perioada 1, numarand statiile din trei in trei. Se obtine astfel 1,4,7, sau daca nu este divizibil cu 3 vor exista transpozitii, caz in care vor fi numerotate tot din trei in trei statiile care nu apar in sirul anterior generat. Continuand rationamentul pentru perioada n, ordinul statiilor se obtine din perioada 1 prin numararea din n in n, obtinand astfel 1, n,2n,

Cazuri particulare
1). m = 9 , n = 4 1,2,3,4,5,6,7,8,9 1. 1,3,5,7,9,2,4,6,8 2. (1,4,7,1) ; (2,5,8,2) ; (3,6,9,3) 3. 1,5,9,4,8,3,7,2,6 4.

Modelarea sistemelor economice Tema 1 Model de partitionare circulara Neamtu Claudia an III, grupa 4

2). m = 11 , n = 5 1,2,3,4,5,6,7,8,9,10,11 1. 1,3,5,7,9,11,2,4,6,8,10 2. 1,4,7,10,2,5,8,11,3,6,9 3. 1,5,9,2,6,10,3,7,11,4,8 4. 1,6,11,5,10,4,9,3,8,2,7 5. C1). m = 7, n = 3 1,2,3,4,5,6,7 1. 1,3,5,7,2,4,6 2. 1,4,7,3,6,2,5 3. C2). m = 17, n = 8 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 1. 1,3,5,7,9,11,13,15,17,2,4,6,8,10,12,14,16 2. 1,4,7,10,13,16,2,5,8,11,14,17,3,6,9,12,15 3. 1,5,9,13,17,4,8,12,16,3,7,11,15,2,6,1014 4. 1,6,11,16,4,9,14,2,7,12,17,5,10,15,3,8,13 5. 1,7,13,2,8,14,3,9,15,4,10,16,5,11,17,6,12 6. 1,8,15,5,12,2,9,16,6,13,3,10,17,7,14,4,11 7. 1,9,17,8,16,7,15,6,14,5,13,4,12,3,11,2,10 8. C3). m = 27, n = 13 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27 1. 1,3,5,7,9,11,13,15,17,19,21,23,25,27,2,4,6,8,10,12,14,16,18,20,22,24,26 2. (1,4,7,10,13,16,19,22,25,1) ; (2,5,8,11,14,17,20,23,26,2) ; (3,6,9,12,15,18,21,24,27,3) 3. 1,5,9,13,1 7,21,25,2, 6,10,14,18 ,22,26,3,7 ,11,15,19, 23,27,4,8, 12,16,20,2 4 4. 1,6,11,16,21,26,4,9,14,19,24,2,7,12,17,22,27,5,10,15,20,25,3,8,13,18,23 5. (1,7,13,19,25,4,10,16,22,1) ; (2,8,14,20,26,5,11,17,23,2) ; (3,9,15,21,27,6,12,18,24,3) 6. 1,8,15,22,2,9,16,23,3,10,17,24,4,11,18,25,5,12,19,26,6,13,20,27,7,14,21 7. 1,9,17,25,6,14,22,3,11,19,27,8,16,24,5,13,21,2,10,18,26,7,15,23,4,12,20 8. (1,10,19,1) ; (2,11,20,2) ; (3,12,21,3) ; (4,13,22,4) ; (5,14,23,5) ; (6,15,24,6) ; 9. (7,16,25,7) ; (8,17,26,8) ; (9,18,27,9) 1,11,21,4,14,24,7,17,27,10,20,3,13,23,6,16,26,9,19,2,12,22,5,15,25,8,18 10. 1,12,23,7,18,2,13,24,8,19,3,14,25,9,20,4,15,26,10,21,5,16,27,11,22,6,17 11. (1,13,25,10,22,7,19,4,16,1) ; (2,14,26,11,23,8,20,5,17,2) ; (3,15,27,12,24,9,21,6,18,3) 12. 1,14,27,13,26,12,25,11,24,10,23,9,22,8,21,7,20,6,19,5,18,4,17,3,16,2,15 13.

Modelarea sistemelor economice Tema 1 Model de partitionare circulara Neamtu Claudia an III, grupa 4

Rezolvarea temei
unit UTGraphSearch; interface uses forms, classes, dialogs, sysutils, windows, contnrs; type tMethodCall = procedure of object; tFuncCall = function(const s:string; list:pointer):boolean; Tgroup = class(tObject) {Un nod al grafului} nbradjacents:integer; index:integer; g:string; {Valoare grup} adjacents: array of integer; constructor create; end; {Obiectul graf adiacent} TGraphList = class(TStringList) {Lista nodurilor de inceput} public QG:TObjectlist; {Lista rezultatelor (grupuri si blocuri)} finalized:boolean; {true = noduri sortate gata pentru adaugare arcuri (margini)} stop:boolean; {verificare "flag oprire" in timpul cautarii} nodesSearched:int64; visited:array of boolean; BackTo20:boolean; constructor create; destructor destroy; override; procedure clear; override; function AddNode(key:string):boolean; function DeleteNode(key:string):boolean; function AddEdge(key 1, key2:string; bothways:boolean):boolean; procedure finalize; procedure MakePathsToDF(GoalFound:TMethodcall); {adancimea grafului} function validgrp(const g:string):boolean; end; var pairs:array['A' .. 'O','A' .. 'O'] of boolean; implementation Uses combo, U --,Kirkman 1 ; {******* UpdatePairs *********} procedure updatepairs(const g:string); begin pairs[g[1],g[2]]:=true; pairs[g[l],g[3]]:=true; pairs[g[2].g[3]]:=true; end; {****** UndoPairs *********} procedure undopairs(const g:string); begin pairs[g[1],g[2]]:=false; pairs[g[l],g[3]]:=false; pairs[g[2],g[3]]:=false;

Modelarea sistemelor economice Tema 1 Model de partitionare circulara Neamtu Claudia an III, grupa 4

end; {********** TGraphList.ValidGrp ***********} function TGraphList.validgrp(const g:string):boolean; var i,j,k:integer; g2:string; begin if pairs[g[1],g[2]] or pairs[g[1],g[3]] or pairs[g[2],g[3]] then resu1t:=false else result:=true; if result then begin for i:=5*(QG.count div 5) to QG.count-1 do {verificare daca se repeta vreo litera} begin g2:=TGroup(QG[i]).g; for j:=1 to 3 do for k:=l to 3 do if g[j]=g2[k] then begin result:=false; break; end; if not result then break; end; end; end; {************ TGroup.Create *********} constructor TGroup.create; begin inherited create; setlength(adjacents,5); {incepem de la 5, apoi adaugam cate 5} nbradjacents:=0: index:=0; {legatura catre cheia anterioara} g:= ; end; {************* TGraphlist.create *****} constructor TgraphList.create; begin inherited; QG:=TObjectlist. create; QG.OwnsObjects:=false; finalized:=false; end; {*********** TGraphlist.destroy ******} destructor TGraphList.destroy; var i:integer; begin {free all nodes} for i:=0 to count-l do objects[i].free; QG.free; inherited; end;

Modelarea sistemelor economice Tema 1 Model de partitionare circulara Neamtu Claudia an III, grupa 4

{********** TGraphlist.clear *********} procedure TGraphList.clear; var i: integer; begin for i:=0 to count-l do begin TGroup(objects[i]).free; delete(i); end; finalized:=false; inherited; end; {**************** TGraphList.Addnode **********} function TGraphList.AddNode(key:string):boolean; {adaugare nod nou} var node:TGroup; begin if not finalized then begin node:=TGroup.create; addobject(key,node); result:=true; end; else result:=false; end; {************** TGraphList.DeleteNode ********} function TGraph List. DeleteNode(key:string):boolean; var i:integer; begin if not finalized then begin i:=indexof(key); if i>=0 then begin objects[i].Free; delete(i); result:=true; end; else result:=false; end; else result:=false; end; {*************** TGraphList.AddEdge ***********} function TGraphList.AddEdge(key 1,key2:string; bothways:boolean): boolean; {adaugare arc la nod} var k1 ,k2 :integer; j:integer; found:boolean; node 1,node2:TGroup; begin if not finalized then begin showmessage('Nodul trebuie adaugat si lista finalizata inainte de

Modelarea sistemelor economice Tema 1 Model de partitionare circulara Neamtu Claudia an III, grupa 4

adaugarea arcurilor'); result:=false; exit; end; result:=true; if find(key1,k1) and find(key2,k2) then begin node1:=TGroup(objects[k1]); with node 1 do begin found:=false; for j:=O to nbradjacents-1 do if adjacents[i]=k2 then begin found:=true; break; end; if not found then begin inc(nbradjacents); if nbradjacents>length(adjacents) then setlength(adjacents,length(adjacents)+5); adjacents[nbradjacents-l]:= k2; end; end; if bothways then begin node2:=TGroup(objects[k2]); with node2 do begin found:=false; for j:=0 to nbradjacents-l do if adjacents[j]=kl then begin found:=true; break; end; if not found then begin inc(nbradjacents ); if nbradjacents> length(adjacents) then setlength(adjacents,length(adjacents )+5); adjacents[nbradjacents-l]:=kl; end; end; end; end; {***********TGraphList.finalize ** ** *** ** *} procedure TGraphlist.finalize; {sortarea nodurilor si completarea valorilor index} var i :integer; begin sort; for i:=0 to count-l do

Modelarea sistemelor economice Tema 1 Model de partitionare circulara Neamtu Claudia an III, grupa 4

with Tgroup(objects[i]) do begin index:=i; g:=strings[i]; end; finalized:=true; end; {***********TGraphList.MakepathstoDF **** *** ** *} procedure TGraphList.MakePathsToDF(GoalFound:TMethodCall); {cautare in adancime pentru rezultat} var i,j :integer; nodenbr:integer; ch1,ch2:char; g:string; function NoOverlap(s1,s2:string):boolean; begin resu1t:=true; if sl>s2 then result:=fa1se else for i:=1 to 1ength(s1)-1 do for j:=1 to length(s2) do if s1[i]=s2[j] then begin result:=fa1se; break; end; end; procedure dfs(const v:integer); {cautare reeursiva in adancime} var temp:TGroup; i:integer; g:string; begin if stop then exit; {flagul de oprire a fost setat} inc(nodesSearehed); temp:=TGroup(objects[v]); {preluare nod} QG.add(temp); updatepairs(strings[temp.index]); {adaugare in coada} visited[temp.index]:=true; if (QG.count=30)then {succes} begin GoalFound; BackTo20:=true; visited[temp.index]:=fa1se; {intoareere de 1a nod} undopairs(temp.g); QG.de1ete(QG.count-l); {stergere noduri temporare} exit; end; else begin {nu se intampla in adancime maxima} if(QG.count mod 5 =0) then {startul blocului, ob1igatoriu cu "A"} begin {preluare nod nou de pornire} if QG.eount>O then temp:= TGroup(QG[QG.eount-l]); for i:= 0 to count-1 do begin

Modelarea sistemelor economice Tema 1 Model de partitionare circulara Neamtu Claudia an III, grupa 4

g:=strings[i]; if g[l]=='A' then begin if (not visited[i]) and validgrp(g) then begin dfs(i); {intoarcere recursiva pt al doilea grup} if stop then exit; break; end; end ; else break; end; end; else begin {nu este startul blocului} temp:=TGroup(QG[QG.count-l]); with temp do begin for i:= 0 to nbradjacents-l do begin if stop then exit; if not visited[adjacents[i]])and validgrp(strings[adjacents[i]]) then dfs(adjacents[i]); if BackT020 then if QG.count>20 then break; else BackT020:=false; end; end; end; end; if QG.count> 1 then begin visited[temp.index]:=false; {iesire din nod} undopairs(temp.g); QG.delete(QG.count-l); {stergere noduri temporare} end; if nodessearched mod 16384=0 then begin from1.label1.caption:=format('Noduri parcurse%l0.0n, [nodessearched+0.0]); application.processmessages; end; end; begin {initializare perechi de tablou} for ch1:='A' to O do for ch2:='A' to O do pairs[ch1,ch2]:=false; {crearea grupurilor} combos.setup(3,15,combinations); while combos.getnext do with combos do begin g:= ; for i:= 1 to 3 do g[i]:=char(ord(A)-l+selected[i]); addnode(g);

Modelarea sistemelor economice Tema 1 Model de partitionare circulara Neamtu Claudia an III, grupa 4

end; for i:=0 to 4 do begin g:= ; for j :=0 to 2 do g[j+1]:=char(ord('A')+i*3+j); deletenode(g); updatepairs (g) ; end; {creare adiacente} finalize; for i:=0 to count-2 do begin for j:=i+1 to count-l do begin if noOverlap(strings[i],strings[j]) then addedge(strings[i],strings[j],false); end; end; {iesire daca nu s-a gasit rezolvare} setlength(visited,0); {resetare tablou "parcurse"} setlength( visited,count); QG.clear; find('BEH',nodenbr); visited[nodenbr]:=true; {eliminare BEJ} visited[nodenbr+l]:=true; {eliminare BEI} find('ADG',nodenbr); stop:=false; nodessearched:=O; BackT020:=false; dfs(nodenbr); {pornire cautare recursiva} end; end. Unit Combo; Interface Const maxentries=20; Type bytearray=array[1..maxentries] of byte; TCombotype=(Combinations,Permutations); TComboSet=c1ass(TObject) private N: word; R: word; NumberOfSubsets :int64; Ctype: TComboType; loops: bytearray; public Selected: bytearray; Procedure Setup(newR, newN:word; NewCtype:TComboType); Function Getnext:boolean; Function GetNextCombo:Boolean; Function GetNextPermute:boolean; Function GetCount:int64; function GetR:integer; procedure assign(x:Tcomboset);

Modelarea sistemelor economice Tema 1 Model de partitionare circulara Neamtu Claudia an III, grupa 4

10

end; var combos:TComboSet; Implementation procedure TComboset.assign(x:TComboset); begin N:=x.n; R:=x.r; NumberOfSubsets:=x.numberofsubsets; Ctype:=x.Ctype; loops:=x.1oops; Selected:=x.Selected: end; Procedure TComboset.Setup(newR,newN:word; NewCtype:TComboType); Function factorial(n:word):int64; var k:int64; i:word; begin k:=1; for i:=2 to n do k:=k*i; resuIt:=k; end; var i:word; begin n:=newn; if n>maxentries then n:=maxentries; r:=newr; if r>n then r:=n; Ctype:=newCtype; for i:=1 to r do begin selected[i]:=i; loops [i]:=0; end; selected[r]:=r-1; NumberOfSubsets:=(Factorial(n) div factorial(n-r)); if Ctype=Combinations then NumberofSubsets:=trunc(numberofSubsets/factorial(r)); end; Function TComboset.getnextpennute:boolean; var i,j,k:word; done: boolean; flag: boolean; begin getnextpermute:=false; i:=r; done:=false; while not done do begin j :=selected[i]+ 1; if loops[i]>n- i then j :=n+ 1; if (j<=n) then repeat flag:=false;

Modelarea sistemelor economice Tema 1 Model de partitionare circulara Neamtu Claudia an III, grupa 4

11

for k:=1 to i do if selected[k]=j then begin f1ag:=true: break.; end; if not flag then begin selected[i]:=j; inc(loops [i]); inc(i); if i<=r then j:=1; end; else inc(j); Until (i>r) or (j>n); If j>n then Begin selected[i]:=0; loops[i]:=0; dec(i); end; else Begin done:=true; getnextpermute:=true; End; If i=0 then Begin done:=true; End; End; End; Function Tcomboset.getNextcombo:boolean; Var i,j :word; done: boolean; Begin getnextcombo:=false; i:=r; done:=false; while not done do Begin j :=selected[i]+ 1; if j<=n then Function Tcomboset.getnext; Begin {Getnext} If Ctype=Combinations then getnext:=getnextcombo Else getnext:=getnextpermute; {inc(count);} End; function TComboset.Getcount:int64; begin resu1t:=NumberOfSubsets; end; function TComboset:GetR:integer; begin resu1t:=r;

Modelarea sistemelor economice Tema 1 Model de partitionare circulara Neamtu Claudia an III, grupa 4

12

end; Initialization combos:=TComboset.create; End.

S-ar putea să vă placă și