[Genetic algorithm] Song of ice and fire

Source: Internet
Author: User

[Description]

In the famous Game "Song of ice and fire", heroes personally fought! Heroes start with HPH and mph magic values. Heroes can use different skills. Your hero has three skills: furious, chaotic, and Angel.

A hero wants to create a group of monsters. Each monster has an HPM point of life at the beginning. This group of monsters has a nm at the beginning, and their total life value has a HPM x nm point, as the battle proceeds, the total life of the monster group is reduced. Assume that the current monster group has a total life value of, then the number of surviving monsters is rounded up by H/HPM.

The battle is carried out on a one-dimensional battlefield that contains N + 1 grids. The grids start from 0 and your hero remains unchanged on the grid 0, at the beginning, monsters can move on the "N" grid. Monsters can move up to the "v" grid in a single round.

Combat is the turn of the system, your hero is the first hand, the hero and the monster rotation, the monster's strategy is very simple, they first to the hero's direction to move min (v, P-1) grid, P indicates the number of the grids in which they start to be integrated. If the monsters reach the No. 1 lattice after moving, they will attack your hero, if K monsters attack, your hero will deduct K points of life. If your hero's life value changes to a non-positive value, the battle will fail.

In your hero's round, he must use one of the three skills. The effect of the three skills is as follows:

Furious: reduce the LP point life of the monster group. P is the number of the lattice where the monsters are located.

Chaos transfer: Send the monster group to any grid (not 0 ).

Angel Heart: Your hero restores the DH point life value, but cannot exceed the initial life value.

Using any skill consumes 1 magic point. When the life of a monster group falls to 0, your hero wins.

An initial situation is provided. You can determine whether your hero has a strategy to win. If yes, output any winning decision-making scheme.

[Data range]

1 ≤ n ≤ 10, 2 ≤ HPH ≤ 100, 1 ≤ mph ≤ 50, 1 ≤ HPM ≤ 10, 1 ≤ nm ≤ 10, 1 ≤ v ≤ n, 1 ≤ DH <HPH, 1 ≤ Li ≤ 10

[Question]

The answer to this question should be a search or DP, but I used simulated annealing for the previous question in the test room, so I used the genetic algorithm (although these two items seem to have no connection ). for the first time, we used genetic algorithms and various wa, but the main reason was that the meaning of the questions was not clearly understood, and the genetic algorithm was not difficult.

I evaluate the merits and demerits of a hero based on the maximum damage that a hero can cause to the monster before his death. the method to generate the next generation is: the optimal 10 individual directly enters the next round of inheritance, and then selects 10 pairs of individuals to randomly mate, the mating individual will respectively half of their genes to the child, the offspring produce variations with several fragments in their gene fragments. Here I use the simulated annealing method to learn from it. The more random the number of fragments, the fewer the mutant fragments selected. the actual evaluation results are very good. There is a great chance to be able to communicate with each other, and I will leave at least 85 points. in addition, in the initial stage, the individual should be greedy to create some excellent individuals. In this way, the correctness of the algorithm will be further improved. (the number of cycles may be reduced due to differences in the evaluation machine)

In the sorting process, I used to select sorting. To avoid array exchange, I directly defined two pointers to accelerate.

Code:

Program heroes; Type Int = longint; arr = array [1 .. 50] of char; point = ^ arr; arr2 = array [1 .. 50] of int; P2 = ^ arr2; const skill: array [0 .. 2] of char = ('l', 't', 'H'); var I, J, K, M, N, II, T: int; OK: boolean = false; h_h, m_h, n_m, h_m, v_m, DH, h_tot_m: int; LP, next: array [1 .. 20] of int; F: array [1 .. 20] of ARR; G: array [1 .. 20] of arr2; P: array [1 .. 20] of point; Q: array [1 .. 20] of P2; harm: array [1 .. 20] of int; AA: array [1 .. 120000] of int; PO1: point; PO2: P2; function min (X, Y: INT): int; begin if x <Y then exit (X) else exit (y); end; function get (P: point; Q: P2): int; var I, j, H, M: int; // calculate the individual fitness begin get: = 0; H: = h_h; M: = N; for I: = 1 to m_h do begin if P ^ [I] = 'l' then Inc (get, Lp [m]) else if P ^ [I] = 'H' then begin Inc (H, DH); If H> h_h then h: = h_h; end else if P ^ [I] = 't'then M: = Q ^ [I]; M: = next [m]; if M = 1 then begin h: = H-trunc (h_tot_m-get + h_m-1)/h_m); If H <= 0 Then break; end; Procedure sort; // sort begin for I: = 1 to 20 do begin K: = I; for J: = I + 1 to 20 do if (harm [J]> harm [k]) then K: = J; PO1: = P [I]; P [I]: = P [k]; P [k]: = PO1; PO2: = Q [I]; Q [I]: = Q [k]; Q [k]: = PO2; T: = harm [k]; harm [k]: = harm [I]; harm [I]: = T; end; procedure new (F: point; G: P2); var I, X: int; // generate descendant begin X: = random (10) + 1; for I: = 1 to m_h> 1 do begin f ^ [I]: = P [x] ^ [I]; G ^ [I]: = Q [x] ^ [I]; end; X: = random (10) + 1; for I: = m_h> 1 + 1 to m_h do begin f ^ [I]: = P [x] ^ [I]; G ^ [I]: = Q [x] ^ [I]; end; for I: = 1 to AA [II] Do begin X: = random (m_h) + 1; f ^ [x]: = skill [random (3)]; if f ^ [x] = 't'then G ^ [x]: = random (n) + 1 else G ^ [x]: = 0; end; Procedure main; // filter begin for I: = 1 to 20 do harm [I]: = get (@ f [I], @ g [I]); for I: = 1 to 20 do begin P [I]: = @ f [I]; Q [I]: = @ g [I]; end; for I: = 1 to 45000 do Aa [I]: = trunc (100000-i)/10000) + 1; for II: = 1 to 45000 do begin sort; if harm [1]> = h_tot_m then begin OK: = true; exit; end; for I: = 10 to 20 do new (P [I], Q [I]); for I: = 10 to 20 do harm [I]: = get (P [I], Q [I]); end; Procedure print (X: INT ); vaR harm: int; // The output is begin writeln ('invalid my'); get (P [X], Q [x]); harm: = 0; M: = N; for I: = 1 to m_h do begin write (P [x] ^ [I]); If P [x] ^ [I] = 't'then writeln ('', Q [x] ^ [I]) else writeln; If P [x] ^ [I] = 'l' then Inc (harm, Lp [m]); if P [x] ^ [I] = 't'then M: = Q [x] ^ [I]; M: = next [m]; if (harm> = h_tot_m) Then exit; end; begin randomize; assign (input, 'heroes. in '); reset (input); assign (output, 'heroes. out'); rewrite (output); read (n, h_h, m_h, h_m, n_m, v_m, DH); h_tot_m: = h_m * n_m; for I: = 1 to n do read (LP [I]); for I: = 1 to n do next [I]: = I-min (v_m, I-1); for I: = 1 to 20 do for J: = 1 to m_h do begin f [I, j]: = skill [random (3)]; if f [I, j] = 't'then G [I, j]: = random (n) + 1; end; next [1]: = 1; main; if OK then begin for I: = 1 to 20 do if harm [I]> = h_tot_m then begin print (I); break; end else write ('defeated '); close (input); close (output); end.

By QW

Reprinted please indicate the source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.