mirror of
https://github.com/Lendaia/oe-alga-feladatok.git
synced 2026-04-23 12:26:07 +01:00
ahhhhhhhhfuézbgí-sdehzfxrAIŐHQBG
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Formats.Asn1;
|
||||
using System.Linq;
|
||||
using Microsoft.Identity.Client;
|
||||
|
||||
@@ -6,16 +7,16 @@ namespace OE.ALGA.Optimalizalas
|
||||
{
|
||||
public class VisszalepesesOptimalizacio<T>
|
||||
{
|
||||
int n;
|
||||
int[] M;
|
||||
T[,] R;
|
||||
Func<int, T, bool> ft;
|
||||
Func<int, T, T[], bool> fk;
|
||||
Func<T[], int> josag;
|
||||
public int n;
|
||||
public int[] M;
|
||||
public T[,] R;
|
||||
public Func<int, T, bool> ft;
|
||||
public Func<int, T, T[], bool> fk;
|
||||
public Func<T[], float> josag;
|
||||
|
||||
public int Lepesszam { get; set;} = 0;
|
||||
|
||||
public VisszalepesesOptimalizacio(int n, int[] M, T[,] R, Func<int, T, bool> ft, Func<int, T, T[], bool> fk, Func<T[], int> josag)
|
||||
public VisszalepesesOptimalizacio(int n, int[] M, T[,] R, Func<int, T, bool> ft, Func<int, T, T[], bool> fk, Func<T[], float> josag)
|
||||
{
|
||||
this.n = n;
|
||||
this.M = M;
|
||||
@@ -25,7 +26,7 @@ namespace OE.ALGA.Optimalizalas
|
||||
this.josag = josag;
|
||||
}
|
||||
|
||||
public bool[] OptimalisMegoldas()
|
||||
public T[] OptimalisMegoldas()
|
||||
{
|
||||
int szint = 0;
|
||||
T[] E = new T[n];
|
||||
@@ -76,7 +77,7 @@ namespace OE.ALGA.Optimalizalas
|
||||
public class VisszalepesesHatizsakPakolas
|
||||
{
|
||||
HatizsakProblema problema;
|
||||
public int LepesSzam {get;}
|
||||
public int LepesSzam {get; set;}
|
||||
|
||||
public VisszalepesesHatizsakPakolas(HatizsakProblema problema)
|
||||
{
|
||||
@@ -84,25 +85,91 @@ namespace OE.ALGA.Optimalizalas
|
||||
}
|
||||
virtual public bool[] OptimalisMegoldas()
|
||||
{
|
||||
int[] M;
|
||||
bool[] R;
|
||||
}
|
||||
public int OptimalisErtek()
|
||||
{
|
||||
int[] M = new int[problema.N];
|
||||
|
||||
bool[,] R = new bool[problema.N, 2];
|
||||
for (int i = 0; i < problema.N; i++)
|
||||
{
|
||||
M[i] = 2;
|
||||
R[i, 0] = true;
|
||||
R[i, 1] = false;
|
||||
}
|
||||
VisszalepesesOptimalizacio<bool> visszalepesesoptimalizacio = new VisszalepesesOptimalizacio<bool>(problema.N, M, R, Ft, Fk, Josag);
|
||||
bool[] eredmeny = visszalepesesoptimalizacio.OptimalisMegoldas();
|
||||
LepesSzam = visszalepesesoptimalizacio.Lepesszam;
|
||||
return eredmeny;
|
||||
}
|
||||
public float OptimalisErtek()
|
||||
{
|
||||
return problema.OsszErtek(OptimalisMegoldas());
|
||||
}
|
||||
private float Josag(bool[] pakolas)
|
||||
{
|
||||
float ertek = 0;
|
||||
if (!problema.Ervenyes(pakolas))
|
||||
{
|
||||
ertek = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
ertek = problema.OsszErtek(pakolas);
|
||||
}
|
||||
return ertek;
|
||||
}
|
||||
private bool Ft(int egesz, bool logikai)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
private bool Fk(int egesz, bool logikai, bool[] tomb)
|
||||
{
|
||||
int weight = 0;
|
||||
for (int i = 0; i < problema.N; i++)
|
||||
{
|
||||
if(tomb[i])
|
||||
{
|
||||
weight += problema.W[i];
|
||||
}
|
||||
}
|
||||
return (weight <= problema.Wmax);
|
||||
}
|
||||
|
||||
}
|
||||
public class SzetvalasztasEsKorlatozasOptimalizacio<T> : VisszalepesesOptimalizacio<T>
|
||||
{
|
||||
public Func<int, T[], int> fb;
|
||||
public SzetvalasztasEsKorlatozasOptimalizacio(Func<int, T[], int> fb, int n, int[] M, T[,] R, Func<int, T, bool> ft, Func<int, T, T[], bool> fk, Func<T[], int> josag) : base(n, M, R, ft, fk, josag)
|
||||
public SzetvalasztasEsKorlatozasOptimalizacio(Func<int, T[], int> fb, int n, int[] M, T[,] R, Func<int, T, bool> ft, Func<int, T, T[], bool> fk, Func<T[], float> josag) : base(n, M, R, ft, fk, josag)
|
||||
{
|
||||
this.fb = fb;
|
||||
}
|
||||
|
||||
public override void Backtrack(int szint, T[] E, bool van, T[] O)
|
||||
public override void Backtrack(int szint, ref T[] E, ref bool van, ref T[] O)
|
||||
{
|
||||
|
||||
int i = 0;
|
||||
while(i < M[szint])
|
||||
{
|
||||
Lepesszam++;
|
||||
i++;
|
||||
if (ft(szint, R[szint, i]))
|
||||
{
|
||||
if(fk(szint, R[szint, i], E) && fb(szint, O) > 6)
|
||||
{
|
||||
E[szint] = R[szint, i];
|
||||
|
||||
if (szint == n)
|
||||
{
|
||||
if ((!van) || josag(E) > josag(O))
|
||||
{
|
||||
O = E;
|
||||
}
|
||||
van = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Backtrack(szint+1, ref E, ref van, ref O);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public class SzetvalasztasEsKorlatozasHatizsakPakolas : VisszalepesesHatizsakPakolas
|
||||
@@ -112,7 +179,8 @@ namespace OE.ALGA.Optimalizalas
|
||||
}
|
||||
public override bool[] OptimalisMegoldas()
|
||||
{
|
||||
|
||||
bool[] asd = new bool[2] {true, true};
|
||||
return asd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user