<< Graphics`Graphics`

pocbloku = 4;
n = 2^pocbloku;
predchazejici = {1, 1, 1, 1};
hodina = 1;
meneni = Table[n/2^i, {i, 1, pocbloku}];
kombinace = 
        Transpose[
          Table[Flatten[
              Table[Flatten[{Table[1, {meneni[[i]]}], 
                    Table[0, {meneni[[i]]}]}], {meneni[[1]]/
                    meneni[[i]]}]], {i, 1, 
              pocbloku}]];
c = {{20, 0.002, 0.0005, 5000, 4000, 440}, {5, 0.0015, 0.0007, 40, 
            60, 440}, {30, 0.0017, 0.0006, 50, 30, 440}, {20, 0.0023, 0.0006, 
            60, 30, 440}};
zatizeni = {400, 500, 550, 650, 700, 1500, 1300, 1200, 1200, 1100, 
          900, 1400, 
1400, 1500, 1500, 1400, 1600, 1400, 
          1300, 1400, 1200, 1300, 1400, 900};
vysledek = Table[1, {24}];
vyslednenakl = Table[1, {24}];

Optimalizace = 
        Function[hodina, 
          naklady = 
            MatrixForm[
              Table[Plus @@ 
                  Flatten[
                    Table[kombinace[[a]][[i]] ((Subscript[P, i])^(j - 1))*
                        c[[i, j]], {i, pocbloku}, {j, 3}]], {a, 1, 
                  n}]]; 
pokryti = 
            MatrixForm[
              Table[(((zatez - Sum[kombinace[[a]][[i]]*(Subscript[P, i]), {i, pocbloku}]))^2), {a, 1, n}]]; 
horniomez = 
            MatrixForm[
              Table[Sum[
                  Exp[kombinace[[a]][[i]]*(Subscript[P, i]) - 
                      c[[i, 6]]], {i, pocbloku}], {a, 1, 
                  n}]]; 
dolniomez = 
            MatrixForm[
              Table[Sum[
                  Exp[kombinace[[a]][[i]]*(-(Subscript[P, i]))], {i, 
                    pocbloku}], {a, 1, n}]]; 
fitness[
              zatez_] = 
            MatrixForm[
              Table[naklady[[1]][[a]] + 
                  10  pokryti[[1]][[a]] + horniomez[[1]][[a]] + dolniomez[[1]][[a]], {a, 1, 
                  n}]]; 
rozlozeni = 
            MatrixForm[
              Table[FindMinimum[fitness[
                        zatizeni[[hodina]]][[1]][[a]], {(Subscript[P, 1]), 
                    0}, {(Subscript[P, 2]), 0}, {(Subscript[P, 3]), 0}, {(Subscript[P, 4]), 0}, 
                  MaxIterations -> 10^5, AccuracyGoal -> 1], {a, 1, 
                  n - 1}]]; 
kontrola = 
            MatrixForm[
              Table[zatizeni[[hodina]] - 
                  Sum[rozlozeni[[1]][[a]][[2]][[i]][[2]], {i, 1, pocbloku}], {a, 1, n - 1}]]; 
nakl = 
            MatrixForm[
              Table[rozlozeni[[1]][[a]][[1]]*
                  zatizeni[[hodina]], {a, 1, 
                  n - 1}]]; 
mernenakl = 
            MatrixForm[
              Table[rozlozeni[[1]][[a]][[1]], {a, 1, 
                  n - 1}]]; 
odstaveni = 
            Table[c[[i]][[4]]*
                Im[Sqrt[kombinace[[a]][[i]] - 
                      predchazejici[[i]]]], {i, 1, pocbloku}, {a, 1, 
                n - 1}]; 
najeti = 
            Table[c[[i]][[5]]*
                Re[Sqrt[kombinace[[a]][[i]] - 
                      predchazejici[[i]]]], {i, 1, pocbloku}, {a, 1, 
                n - 1}]; 
soucetpenal = 
            MatrixForm[
              Table[Sum[odstaveni[[i]][[a]] + 
                    Sum[najeti[[i]][[a]]], {i, 1, pocbloku}], {a, 
                  1, n - 1}]]; 
celknakl = 
            Table[nakl[[1]][[a]] + soucetpenal[[1]][[a]], {a, 1, n - 1}]; 
minp = Position[
                Table[Select[celknakl, # < celknakl[[i]] &], {i, 1, 
                    Length[celknakl]}], {}][[1, 
                1]]; 
minprvek = 
            celknakl[[minp]]; 
predchazejici = 
            kombinace[[minp]];];

For[iter = 1, iter < 25, iter++, Optimalizace[iter]; 
        vysledek[[iter]] = 
          vysledek[[iter]]*rozlozeni[[1, minp, 2]]; 
        vyslednenakl[[iter]] = 
          vyslednenakl[[iter]]*minprvek];

MatrixForm[vysledek]
BarChart[vyslednenakl/zatizeni, 
        BarStyle -> RGBColor[ .90,  .90,  .90], 
        AxesLabel -> {"t [h]", "c [Kč/MWh]"}, 
        PlotLabel -> "M\:011brné náklady b\:011bhem \
obchodního dne", ImageSize -> 550, 
        TextStyle -> {FontSize -> 12, FontFamily -> "arial", 
            FontWeight -> "Bold"}];