<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.wesnoth.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=KevinXi</id>
	<title>The Battle for Wesnoth Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.wesnoth.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=KevinXi"/>
	<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/Special:Contributions/KevinXi"/>
	<updated>2026-04-23T03:45:23Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.16</generator>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54784</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54784"/>
		<updated>2014-04-17T03:43:32Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/wwzcHE3n&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
**Experience of units should take into consider&lt;br /&gt;
*Currently the intermediate value of hitpoints lost in battle simulation is store in the int unit::hitpoint_, need new double variable to store the expectation of the hitpoints lost, or just run a real simulation. Find out which way is good.&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*Its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Project==&lt;br /&gt;
===Timeline===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 27 Apr || &lt;br /&gt;
*&amp;lt;b&amp;gt;Prepare step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Familiar with the overall structure of Wesnoth project&lt;br /&gt;
*Familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 28 Apr - 11 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;General framework setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*Write design document for it&lt;br /&gt;
*implement dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 25 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Dynamic programming elements setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Learn more on dynamic programming, understand the advantages and of it&lt;br /&gt;
*Refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
*Implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
*Maybe Machine Learning can help to decide the value of state&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Test environment setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*Implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 15 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design and write document for the design of CAs&lt;br /&gt;
*Implement and test corresponding CAs, test them individually&lt;br /&gt;
*Refine the value of state(villages, units) again according to the performance of CAs&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs &amp;quot;design-implement-test&amp;quot; iterations step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Make the CAs interact with state&lt;br /&gt;
*Plug CAs into the general framework&lt;br /&gt;
*Test and deal with feedback&lt;br /&gt;
*Machine learning may help&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Plan===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;general framework&amp;quot; mentioned above: To implement my idea, I will likely to create new stage which is very similar to the current candidate_action_evaluation_loop stage, but utilize dynamic programming to figure out the decision by the states of current turn and a few turns ahead.&lt;br /&gt;
&lt;br /&gt;
The CAs: I plan to work in a &amp;quot;design-implement-test&amp;quot; iterations. The performance of CAs will be improved at the end of each iteration.&lt;br /&gt;
&lt;br /&gt;
The structure of new CA is also very similar to the current CA, hopefully they will be compatible. The new CA will be notified which decision the framework part made, and only the corresponded CAs(CAs designed for specific decision) will be add to the evaluation queue.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing(Roguelike, nethack is my favourite!), Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/152&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]]. I may need one week off for final exam, it usually happend at the end of June, and another week to return home from university and spend time with my parents, at the beginning of summer vacation.&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project. And I want to get the paycheck for tuition fee of the next semester.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54769</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54769"/>
		<updated>2014-04-16T00:22:45Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: /* Issues */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/wwzcHE3n&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
**Experience of units should take into consider&lt;br /&gt;
*Currently the intermediate value of hitpoints lost in battle simulation is store in the int unit::hitpoint_, need new double variable to store the expectation of the hitpoints lost, or just run a real simulation. Find out which way is good.&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*Its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Project==&lt;br /&gt;
===Timeline===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 27 Apr || &lt;br /&gt;
*&amp;lt;b&amp;gt;Prepare step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Familiar with the overall structure of Wesnoth project&lt;br /&gt;
*Familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 28 Apr - 11 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;General framework setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*Write design document for it&lt;br /&gt;
*implement dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 25 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Dynamic programming elements setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Learn more on dynamic programming, understand the advantages and of it&lt;br /&gt;
*Refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
*Implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
*Maybe Machine Learning can help to decide the value of state&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Test environment setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*Implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 15 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design and write document for the design of CAs&lt;br /&gt;
*Implement and test corresponding CAs, test them individually&lt;br /&gt;
*Refine the value of state(villages, units) again according to the performance of CAs&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs &amp;quot;design-implement-test&amp;quot; iterations step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Make the CAs interact with state&lt;br /&gt;
*Plug CAs into the general framework&lt;br /&gt;
*Test and deal with feedback&lt;br /&gt;
*Machine learning may help&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Plan===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;general framework&amp;quot; mentioned above: To implement my idea, I will likely to create new stage which is very similar to the current candidate_action_evaluation_loop stage, but utilize dynamic programming to figure out the decision by the states of current turn and a few turns ahead.&lt;br /&gt;
&lt;br /&gt;
The CAs: I plan to work in a &amp;quot;design-implement-test&amp;quot; iterations. The performance of CAs will be improved at the end of each iteration.&lt;br /&gt;
&lt;br /&gt;
The structure of new CA is also very similar to the current CA, hopefully they will be compatible. The new CA will be notified which decision the framework part made, and only the corresponded CAs(CAs designed for specific decision) will be add to the evaluation queue.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing(Roguelike, nethack is my favourite!), Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]]. I may need one week off for final exam, it usually happend at the end of June, and another week to return home from university and spend time with my parents, at the beginning of summer vacation.&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project. And I want to get the paycheck for tuition fee of the next semester.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54768</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54768"/>
		<updated>2014-04-16T00:21:51Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: /* Issues */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/wwzcHE3n&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*Currently the intermediate value of hitpoints lost in battle simulation is store in the int unit::hitpoint_, need new double variable to store the expectation of the hitpoints lost, or just run a real simulation. Find out which way is good.&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*Its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Project==&lt;br /&gt;
===Timeline===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 27 Apr || &lt;br /&gt;
*&amp;lt;b&amp;gt;Prepare step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Familiar with the overall structure of Wesnoth project&lt;br /&gt;
*Familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 28 Apr - 11 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;General framework setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*Write design document for it&lt;br /&gt;
*implement dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 25 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Dynamic programming elements setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Learn more on dynamic programming, understand the advantages and of it&lt;br /&gt;
*Refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
*Implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
*Maybe Machine Learning can help to decide the value of state&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Test environment setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*Implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 15 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design and write document for the design of CAs&lt;br /&gt;
*Implement and test corresponding CAs, test them individually&lt;br /&gt;
*Refine the value of state(villages, units) again according to the performance of CAs&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs &amp;quot;design-implement-test&amp;quot; iterations step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Make the CAs interact with state&lt;br /&gt;
*Plug CAs into the general framework&lt;br /&gt;
*Test and deal with feedback&lt;br /&gt;
*Machine learning may help&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Plan===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;general framework&amp;quot; mentioned above: To implement my idea, I will likely to create new stage which is very similar to the current candidate_action_evaluation_loop stage, but utilize dynamic programming to figure out the decision by the states of current turn and a few turns ahead.&lt;br /&gt;
&lt;br /&gt;
The CAs: I plan to work in a &amp;quot;design-implement-test&amp;quot; iterations. The performance of CAs will be improved at the end of each iteration.&lt;br /&gt;
&lt;br /&gt;
The structure of new CA is also very similar to the current CA, hopefully they will be compatible. The new CA will be notified which decision the framework part made, and only the corresponded CAs(CAs designed for specific decision) will be add to the evaluation queue.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing(Roguelike, nethack is my favourite!), Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]]. I may need one week off for final exam, it usually happend at the end of June, and another week to return home from university and spend time with my parents, at the beginning of summer vacation.&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project. And I want to get the paycheck for tuition fee of the next semester.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54763</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54763"/>
		<updated>2014-04-15T11:25:12Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/wwzcHE3n&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*Its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Project==&lt;br /&gt;
===Timeline===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 27 Apr || &lt;br /&gt;
*&amp;lt;b&amp;gt;Prepare step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Familiar with the overall structure of Wesnoth project&lt;br /&gt;
*Familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 28 Apr - 11 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;General framework setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*Write design document for it&lt;br /&gt;
*implement dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 25 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Dynamic programming elements setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Learn more on dynamic programming, understand the advantages and of it&lt;br /&gt;
*Refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
*Implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
*Maybe Machine Learning can help to decide the value of state&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Test environment setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*Implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 15 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design and write document for the design of CAs&lt;br /&gt;
*Implement and test corresponding CAs, test them individually&lt;br /&gt;
*Refine the value of state(villages, units) again according to the performance of CAs&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs &amp;quot;design-implement-test&amp;quot; iterations step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Make the CAs interact with state&lt;br /&gt;
*Plug CAs into the general framework&lt;br /&gt;
*Test and deal with feedback&lt;br /&gt;
*Machine learning may help&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Plan===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;general framework&amp;quot; mentioned above: To implement my idea, I will likely to create new stage which is very similar to the current candidate_action_evaluation_loop stage, but utilize dynamic programming to figure out the decision by the states of current turn and a few turns ahead.&lt;br /&gt;
&lt;br /&gt;
The CAs: I plan to work in a &amp;quot;design-implement-test&amp;quot; iterations. The performance of CAs will be improved at the end of each iteration.&lt;br /&gt;
&lt;br /&gt;
The structure of new CA is also very similar to the current CA, hopefully they will be compatible. The new CA will be notified which decision the framework part made, and only the corresponded CAs(CAs designed for specific decision) will be add to the evaluation queue.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing(Roguelike, nethack is my favourite!), Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]]. I may need one week off for final exam, it usually happend at the end of June, and another week to return home from university and spend time with my parents, at the beginning of summer vacation.&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project. And I want to get the paycheck for tuition fee of the next semester.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54762</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54762"/>
		<updated>2014-04-15T09:31:12Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: /* Questionnaire */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/7rtUhM5Z&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*Its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Project==&lt;br /&gt;
===Timeline===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 27 Apr || &lt;br /&gt;
*&amp;lt;b&amp;gt;Prepare step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Familiar with the overall structure of Wesnoth project&lt;br /&gt;
*Familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 28 Apr - 11 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;General framework setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*Write design document for it&lt;br /&gt;
*implement dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 25 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Dynamic programming elements setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Learn more on dynamic programming, understand the advantages and of it&lt;br /&gt;
*Refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
*Implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
*Maybe Machine Learning can help to decide the value of state&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Test environment setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*Implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 15 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design and write document for the design of CAs&lt;br /&gt;
*Implement and test corresponding CAs, test them individually&lt;br /&gt;
*Refine the value of state(villages, units) again according to the performance of CAs&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs &amp;quot;design-implement-test&amp;quot; iterations step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Make the CAs interact with state&lt;br /&gt;
*Plug CAs into the general framework&lt;br /&gt;
*Test and deal with feedback&lt;br /&gt;
*Machine learning may help&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Plan===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;general framework&amp;quot; mentioned above: To implement my idea, I will likely to create new stage which is very similar to the current candidate_action_evaluation_loop stage, but utilize dynamic programming to figure out the decision by the states of current turn and a few turns ahead.&lt;br /&gt;
&lt;br /&gt;
The CAs: I plan to work in a &amp;quot;design-implement-test&amp;quot; iterations. The performance of CAs will be improved at the end of each iteration.&lt;br /&gt;
&lt;br /&gt;
The structure of new CA is also very similar to the current CA, hopefully they will be compatible. The new CA will be notified which decision the framework part made, and only the corresponded CAs(CAs designed for specific decision) will be add to the evaluation queue.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing(Roguelike, nethack is my favourite!), Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]]. I may need one week off for final exam, it usually happend at the end of June, and another week to return home from university and spend time with my parents, at the beginning of summer vacation.&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project. And I want to get the paycheck for tuition fee of the next semester.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54761</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54761"/>
		<updated>2014-04-15T09:27:25Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: /* Questionnaire */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/7rtUhM5Z&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*Its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Project==&lt;br /&gt;
===Timeline===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 27 Apr || &lt;br /&gt;
*&amp;lt;b&amp;gt;Prepare step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Familiar with the overall structure of Wesnoth project&lt;br /&gt;
*Familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 28 Apr - 11 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;General framework setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*Write design document for it&lt;br /&gt;
*implement dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 25 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Dynamic programming elements setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Learn more on dynamic programming, understand the advantages and of it&lt;br /&gt;
*Refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
*Implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
*Maybe Machine Learning can help to decide the value of state&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Test environment setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*Implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 15 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design and write document for the design of CAs&lt;br /&gt;
*Implement and test corresponding CAs, test them individually&lt;br /&gt;
*Refine the value of state(villages, units) again according to the performance of CAs&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs &amp;quot;design-implement-test&amp;quot; iterations step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Make the CAs interact with state&lt;br /&gt;
*Plug CAs into the general framework&lt;br /&gt;
*Test and deal with feedback&lt;br /&gt;
*Machine learning may help&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Plan===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;general framework&amp;quot; mentioned above: To implement my idea, I will likely to create new stage which is very similar to the current candidate_action_evaluation_loop stage, but utilize dynamic programming to figure out the decision by the states of current turn and a few turns ahead.&lt;br /&gt;
&lt;br /&gt;
The CAs: I plan to work in a &amp;quot;design-implement-test&amp;quot; iterations. The performance of CAs will be improved at the end of each iteration.&lt;br /&gt;
&lt;br /&gt;
The structure of new CA is also very similar to the current CA, hopefully they will be compatible. The new CA will be notified which decision the framework part made, and only the corresponded CAs(CAs designed for specific decision) will be add to the evaluation queue.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]]. I may need one week off for final exam, it usually happend at the end of June, and another week to return home from university and spend time with my parents, at the beginning of summer vacation.&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project. And I want to get the paycheck for tuition fee of the next semester.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54760</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54760"/>
		<updated>2014-04-15T09:25:12Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/7rtUhM5Z&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*Its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Project==&lt;br /&gt;
===Timeline===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 27 Apr || &lt;br /&gt;
*&amp;lt;b&amp;gt;Prepare step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Familiar with the overall structure of Wesnoth project&lt;br /&gt;
*Familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 28 Apr - 11 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;General framework setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*Write design document for it&lt;br /&gt;
*implement dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 25 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Dynamic programming elements setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Learn more on dynamic programming, understand the advantages and of it&lt;br /&gt;
*Refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
*Implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
*Maybe Machine Learning can help to decide the value of state&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Test environment setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*Implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 15 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design and write document for the design of CAs&lt;br /&gt;
*Implement and test corresponding CAs, test them individually&lt;br /&gt;
*Refine the value of state(villages, units) again according to the performance of CAs&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs &amp;quot;design-implement-test&amp;quot; iterations step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Make the CAs interact with state&lt;br /&gt;
*Plug CAs into the general framework&lt;br /&gt;
*Test and deal with feedback&lt;br /&gt;
*Machine learning may help&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Plan===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;general framework&amp;quot; mentioned above: To implement my idea, I will likely to create new stage which is very similar to the current candidate_action_evaluation_loop stage, but utilize dynamic programming to figure out the decision by the states of current turn and a few turns ahead.&lt;br /&gt;
&lt;br /&gt;
The CAs: I plan to work in a &amp;quot;design-implement-test&amp;quot; iterations. The performance of CAs will be improved at the end of each iteration.&lt;br /&gt;
&lt;br /&gt;
The structure of new CA is also very similar to the current CA, hopefully they will be compatible. The new CA will be notified which decision the framework part made, and only the corresponded CAs(CAs designed for specific decision) will be add to the evaluation queue.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]]. I may need one week off for final exam, it usually happend at the end of June, and another few days to return home from university and spend time with my parents, at the beginning of summer vacation.&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project. And I want to get the paycheck for tuition fee of the next semester.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54759</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54759"/>
		<updated>2014-04-15T09:18:32Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: /* Timeline */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/7rtUhM5Z&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*Its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Project==&lt;br /&gt;
===Timeline===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 27 Apr || &lt;br /&gt;
*&amp;lt;b&amp;gt;Prepare step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Familiar with the overall structure of Wesnoth project&lt;br /&gt;
*Familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 28 Apr - 11 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;General framework setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*Write design document for it&lt;br /&gt;
*implement dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 25 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Dynamic programming elements setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Learn more on dynamic programming, understand the advantages and of it&lt;br /&gt;
*Refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
*Implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
*Maybe Machine Learning can help to decide the value of state&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Test environment setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*Implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 15 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design and write document for the design of CAs&lt;br /&gt;
*Implement and test corresponding CAs, test them individually&lt;br /&gt;
*Refine the value of state(villages, units) again according to the performance of CAs&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs &amp;quot;design-implement-test&amp;quot; iterations step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Make the CAs interact with state&lt;br /&gt;
*Plug CAs into the general framework&lt;br /&gt;
*Test and deal with feedback&lt;br /&gt;
*Machine learning may help&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Plan===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;general framework&amp;quot; mentioned above: To implement my idea, I will likely to create new stage which is very similar to the current candidate_action_evaluation_loop stage, but utilize dynamic programming to figure out the decision by the states of current turn and a few turns ahead.&lt;br /&gt;
&lt;br /&gt;
The CAs: I plan to work in a &amp;quot;design-implement-test&amp;quot; iterations. The performance of CAs will be improved at the end of each iteration.&lt;br /&gt;
&lt;br /&gt;
The structure of new CA is also very similar to the current CA, hopefully they will be compatible. The new CA will be notified which decision the framework part made, and only the corresponded CAs(CAs designed for specific decision) will be add to the evaluation queue.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project. And I want to get the paycheck for tuition fee of the next semester.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54758</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54758"/>
		<updated>2014-04-15T09:15:58Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: /* Benchmarks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/7rtUhM5Z&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*Its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Project==&lt;br /&gt;
===Timeline===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 4 May || &lt;br /&gt;
*&amp;lt;b&amp;gt;Prepare step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Familiar with the overall structure of Wesnoth project&lt;br /&gt;
*Familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 5 May - 11 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;General framework setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*Write design document for it&lt;br /&gt;
*implement dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 25 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Dynamic programming elements setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Learn more on dynamic programming, understand the advantages and of it&lt;br /&gt;
*Refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
*Implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
*Maybe Machine Learning can help to decide the value of state&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Test environment setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*Implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 15 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design and write document for the design of CAs&lt;br /&gt;
*Implement and test corresponding CAs, test them individually&lt;br /&gt;
*Refine the value of state(villages, units) again according to the performance of CAs&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs &amp;quot;design-implement-test&amp;quot; iterations step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Make the CAs interact with state&lt;br /&gt;
*Plug CAs into the general framework&lt;br /&gt;
*Test and deal with feedback&lt;br /&gt;
*Machine learning may help&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Plan===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;general framework&amp;quot; mentioned above: To implement my idea, I will likely to create new stage which is very similar to the current candidate_action_evaluation_loop stage, but utilize dynamic programming to figure out the decision by the states of current turn and a few turns ahead.&lt;br /&gt;
&lt;br /&gt;
The CAs: I plan to work in a &amp;quot;design-implement-test&amp;quot; iterations. The performance of CAs will be improved at the end of each iteration.&lt;br /&gt;
&lt;br /&gt;
The structure of new CA is also very similar to the current CA, hopefully they will be compatible. The new CA will be notified which decision the framework part made, and only the corresponded CAs(CAs designed for specific decision) will be add to the evaluation queue.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project. And I want to get the paycheck for tuition fee of the next semester.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54757</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54757"/>
		<updated>2014-04-15T09:12:26Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: /* Project */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/7rtUhM5Z&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Project==&lt;br /&gt;
===Timeline===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 4 May || &lt;br /&gt;
*&amp;lt;b&amp;gt;Prepare step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Familiar with the overall structure of Wesnoth project&lt;br /&gt;
*Familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 5 May - 11 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;General framework setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*Write design document for it&lt;br /&gt;
*implement dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 25 May ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Dynamic programming elements setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Learn more on dynamic programming, understand the advantages and of it&lt;br /&gt;
*Refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
*Implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
*Maybe Machine Learning can help to decide the value of state&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;Test environment setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*Implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 15 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs setup step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Design and write document for the design of CAs&lt;br /&gt;
*Implement and test corresponding CAs, test them individually&lt;br /&gt;
*Refine the value of state(villages, units) again according to the performance of CAs&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*&amp;lt;b&amp;gt;CAs &amp;quot;design-implement-test&amp;quot; iterations step&amp;lt;/b&amp;gt;&lt;br /&gt;
*Make the CAs interact with state&lt;br /&gt;
*Plug CAs into the general framework&lt;br /&gt;
*Test and deal with feedback&lt;br /&gt;
*Machine learning may help&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Plan===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;general framework&amp;quot; mentioned above: To implement my idea, I will likely to create new stage which is very similar to the current candidate_action_evaluation_loop stage, but utilize dynamic programming to figure out the decision by the states of current turn and a few turns ahead.&lt;br /&gt;
&lt;br /&gt;
The CAs: I plan to work in a &amp;quot;design-implement-test&amp;quot; iterations. The performance of CAs will be improved at the end of each iteration.&lt;br /&gt;
&lt;br /&gt;
The structure of new CA is also very similar to the current CA, hopefully they will be compatible. The new CA will be notified which decision the framework part made, and only the corresponded CAs(CAs designed for specific decision) will be add to the evaluation queue.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project. And I want to get the paycheck for tuition fee of the next semester.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54756</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54756"/>
		<updated>2014-04-15T08:53:23Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: /* Plan */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/7rtUhM5Z&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Project==&lt;br /&gt;
===Timeline===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 4 May || &lt;br /&gt;
*familiar with the overall structure of Wesnoth project&lt;br /&gt;
*familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 5 May - 11 May ||&lt;br /&gt;
*learn dynamic programming, understand the advantages and of it&lt;br /&gt;
*design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*write design document for it&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 18 May ||&lt;br /&gt;
*implement prototype of dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 19 May - 25 May ||&lt;br /&gt;
*find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 8 Jun ||&lt;br /&gt;
*implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
|-&lt;br /&gt;
| 9 Jun - 15 Jun ||&lt;br /&gt;
*refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*design and write document for the design of CAs&lt;br /&gt;
*implement and test corresponding CAs&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*make the CAs interact with state&lt;br /&gt;
*plug CAs into the general framework&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 20 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 21 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Plan===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;general framework&amp;quot; mentioned above: To implement my idea, I will likely to create new stage which is very similar to the current candidate_action_evaluation_loop stage, but utilize dynamic programming to figure out the decision by the states of current turn and a few turns ahead.&lt;br /&gt;
&lt;br /&gt;
The CAs: I plan to work in a &amp;quot;design-implement-test&amp;quot; iterations. The performance of CAs will be improved at the end of each iteration.&lt;br /&gt;
&lt;br /&gt;
The structure of new CA is also very similar to the current CA, hopefully they will be compatible. The new CA will be notified which decision the framework part made, and only the corresponded CAs(CAs designed for specific decision) will be add to the evaluation queue.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project. And I want to get the paycheck for tuition fee of the next semester.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54755</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54755"/>
		<updated>2014-04-15T08:51:17Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/7rtUhM5Z&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Project==&lt;br /&gt;
===Timeline===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 4 May || &lt;br /&gt;
*familiar with the overall structure of Wesnoth project&lt;br /&gt;
*familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 5 May - 11 May ||&lt;br /&gt;
*learn dynamic programming, understand the advantages and of it&lt;br /&gt;
*design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*write design document for it&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 18 May ||&lt;br /&gt;
*implement prototype of dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 19 May - 25 May ||&lt;br /&gt;
*find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 8 Jun ||&lt;br /&gt;
*implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
|-&lt;br /&gt;
| 9 Jun - 15 Jun ||&lt;br /&gt;
*refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*design and write document for the design of CAs&lt;br /&gt;
*implement and test corresponding CAs&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*make the CAs interact with state&lt;br /&gt;
*plug CAs into the general framework&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 20 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 21 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Plan===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;general framework&amp;quot; mentioned above: To implement my idea, I will likely to create new stage which is very similar to the current candidate_action_evaluation_loop stage, but utilize dynamic programming to figure out the decision by the states of current turn and a few turns ahead.&lt;br /&gt;
&lt;br /&gt;
The CAs: I prepare to work in a &amp;quot;design-implement-test&amp;quot; iterations. The performance of CAs will improved at the end of each iteration.&lt;br /&gt;
&lt;br /&gt;
The structure of new CA is also very similar to the current CA, hopefully they will be compatible. The new CA will be notified which decision the framework part made, and only the corresponded CAs(CAs designed for specific decision) will be add to the evaluation queue.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project. And I want to get the paycheck for tuition fee of the next semester.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54743</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54743"/>
		<updated>2014-04-14T16:12:57Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/7rtUhM5Z&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==Plan==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 4 May || &lt;br /&gt;
*familiar with the overall structure of Wesnoth project&lt;br /&gt;
*familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 5 May - 11 May ||&lt;br /&gt;
*learn dynamic programming, understand the advantages and of it&lt;br /&gt;
*design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
*write design document for it&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 18 May ||&lt;br /&gt;
*implement prototype of dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 19 May - 25 May ||&lt;br /&gt;
*find a method to test the result of new AI(maybe AI arena?)&lt;br /&gt;
*implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 8 Jun ||&lt;br /&gt;
*implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
|-&lt;br /&gt;
| 9 Jun - 15 Jun ||&lt;br /&gt;
*refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*design and write document for the design of CAs&lt;br /&gt;
*implement and test corresponding CAs&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*make the CAs interact with state&lt;br /&gt;
*plug CAs into the general framework&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 20 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 21 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
See [[SoC2014_kevin_AI#Plan|&amp;quot;Plan&amp;quot;]].&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54742</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54742"/>
		<updated>2014-04-14T16:03:20Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A rule-based expert system style solution utilizing dynamic programming to make decisions&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
===General Idea===&lt;br /&gt;
&lt;br /&gt;
All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&lt;br /&gt;
&lt;br /&gt;
The priority is not constant, it is a function of the battlefield situation. For now I focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals. See the [[SoC2014_kevin_AI#Details of Priorities|&amp;quot;Details of Priorities&amp;quot;]] part for details.&lt;br /&gt;
&lt;br /&gt;
If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&lt;br /&gt;
&lt;br /&gt;
Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&lt;br /&gt;
&lt;br /&gt;
===The Attack/Retreat Positioning===&lt;br /&gt;
&lt;br /&gt;
When I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&lt;br /&gt;
&lt;br /&gt;
===Details of Priorities===&lt;br /&gt;
&lt;br /&gt;
Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
====Priority of village CA====&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (p&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt;)should satisfy the following conditions:&lt;br /&gt;
*At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
*The priority decreases as the number of own villages increases.&lt;br /&gt;
*The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
*The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
*The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2i77i35.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. v&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is the number of villages we can get in current turn, g&amp;lt;sub&amp;gt;v&amp;lt;/sub&amp;gt; is the gold one village can generate per turn, t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is total number of turns, t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; is current turn, so t&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;-t&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt;is how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. v&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is the number of current own villages, T is time of day modifier, v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt; is the total number of villages, T+v&amp;lt;sub&amp;gt;s&amp;lt;/sub&amp;gt;/2 move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
http://i59.tinypic.com/2q3dh1v.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
====Priority of Attack/Defence/Retreat CA====&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
http://i61.tinypic.com/s2uq89.jpg&lt;br /&gt;
&lt;br /&gt;
g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt; is enemy’s lose and g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is our lose. If in some situation g&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; is much greater than g&amp;lt;sub&amp;gt;e&amp;lt;/sub&amp;gt;, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
http://i60.tinypic.com/2wna9gi.jpg&lt;br /&gt;
&lt;br /&gt;
====Summary====&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
http://i57.tinypic.com/5yaiog.jpg&lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
==Details of Prototype==&lt;br /&gt;
===Flow of Prototype===&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
===Example Output===&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/7rtUhM5Z&lt;br /&gt;
&lt;br /&gt;
===Issues===&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 4 May || &lt;br /&gt;
*familiar with the overall structure of Wesnoth project&lt;br /&gt;
*familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 5 May - 11 May ||&lt;br /&gt;
*learn dynamic programming, understand the advantages and of it&lt;br /&gt;
*design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 18 May ||&lt;br /&gt;
*implement prototype of dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 19 May - 25 May ||&lt;br /&gt;
*find a method to test the result of new AI&lt;br /&gt;
*implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 8 Jun ||&lt;br /&gt;
*implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
|-&lt;br /&gt;
| 9 Jun - 15 Jun ||&lt;br /&gt;
*refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*design, implement and test correspond CAs&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*make the CAs interact with state&lt;br /&gt;
*plug CAs into the general framework&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 20 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 21 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54736</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54736"/>
		<updated>2014-04-14T07:20:57Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: /* Questionnaire */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;A rule-based expert system style solution utilizing dynamic programming to make decisions&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==General Idea==&lt;br /&gt;
&amp;lt;p&amp;gt;All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The priority is not constant, it is a function of the battlefield situation. For now I just focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;About the attack/retreat position: when I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Details of Priorities==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Priority of village CA===&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (pv)should satisfy the following conditions:&lt;br /&gt;
&lt;br /&gt;
At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
&lt;br /&gt;
The priority decreases as the number of own villages increases.&lt;br /&gt;
&lt;br /&gt;
The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
&lt;br /&gt;
The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
1.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. vcis the number of villages we can get in current turn, gvis the gold one village can generate per turn, tsis total number of turns, tcis current turn, so ts-tcis how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. vois the number of current own villages, Tis time of day modifier, vsis the total number of villages, T+vs2move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
1.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Priority of attack/defence/retreat  CA===&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
2.jpg&lt;br /&gt;
&lt;br /&gt;
geis enemy’s lose and gois our lose. If in some situation gois much greater than ge, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
3.jpg&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
4.jpg&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming and Details of Prototype==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Flow of Prototype==&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example Output==&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/bgaZGX2B&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Issues==&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| 21 Apr - 4 May || &lt;br /&gt;
*familiar with the overall structure of Wesnoth project&lt;br /&gt;
*familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| 5 May - 11 May ||&lt;br /&gt;
*learn dynamic programming, understand the advantages and of it&lt;br /&gt;
*design a general framework for dynamic programming be applied to battlefield&lt;br /&gt;
|-&lt;br /&gt;
| 12 May - 18 May ||&lt;br /&gt;
*implement prototype of dynamic programming framework&lt;br /&gt;
|-&lt;br /&gt;
| 19 May - 25 May ||&lt;br /&gt;
*find a method to test the result of new AI&lt;br /&gt;
*implement it if do not have&lt;br /&gt;
|-&lt;br /&gt;
| 26 May - 1 Jun ||&lt;br /&gt;
*refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
|-&lt;br /&gt;
| 2 Jun - 8 Jun ||&lt;br /&gt;
*implement the stage_state utilizing the formula figured out last week&lt;br /&gt;
|-&lt;br /&gt;
| 9 Jun - 15 Jun ||&lt;br /&gt;
*refine the value of state(villages, units), make them comparable under one standard&lt;br /&gt;
|-&lt;br /&gt;
| 16 Jun - 29 Jun ||&lt;br /&gt;
*design, implement and test correspond CAs&lt;br /&gt;
|-&lt;br /&gt;
| 30 Jun - 6 Jul ||&lt;br /&gt;
*make the CAs interact with state&lt;br /&gt;
*plug CAs into the general framework&lt;br /&gt;
|-&lt;br /&gt;
| 7 Jul - 13 Jul ||&lt;br /&gt;
*test AI, analyze result, balance argument&lt;br /&gt;
*is there a automatic way to balancing?&lt;br /&gt;
|-&lt;br /&gt;
| 14 Jul - 20 Jul ||&lt;br /&gt;
*maybe public to players and collect data and respond&lt;br /&gt;
|-&lt;br /&gt;
| 21 Jul - 27 Jul ||&lt;br /&gt;
*judge if it 'global' enough&lt;br /&gt;
|-&lt;br /&gt;
| 28 Jul - 3 Aug ||&lt;br /&gt;
*write wiki page, documents&lt;br /&gt;
*debug&lt;br /&gt;
|-&lt;br /&gt;
| 4 Aug - 10 Aug ||&lt;br /&gt;
*adjust AI according to the responds of community&lt;br /&gt;
|-&lt;br /&gt;
| 11 Aug - 17 Aug ||&lt;br /&gt;
*test, debug&lt;br /&gt;
|-&lt;br /&gt;
| next ||&lt;br /&gt;
*stay here, see if there is someone taking care of the whiteboard part of Wesnoth&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54727</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54727"/>
		<updated>2014-04-14T04:51:08Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;A rule-based expert system style solution utilizing dynamic programming to make decisions&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==General Idea==&lt;br /&gt;
&amp;lt;p&amp;gt;All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The priority is not constant, it is a function of the battlefield situation. For now I just focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;About the attack/retreat position: when I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Details of Priorities==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Priority of village CA===&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (pv)should satisfy the following conditions:&lt;br /&gt;
&lt;br /&gt;
At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
&lt;br /&gt;
The priority decreases as the number of own villages increases.&lt;br /&gt;
&lt;br /&gt;
The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
&lt;br /&gt;
The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
1.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. vcis the number of villages we can get in current turn, gvis the gold one village can generate per turn, tsis total number of turns, tcis current turn, so ts-tcis how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. vois the number of current own villages, Tis time of day modifier, vsis the total number of villages, T+vs2move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
1.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Priority of attack/defence/retreat  CA===&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
2.jpg&lt;br /&gt;
&lt;br /&gt;
geis enemy’s lose and gois our lose. If in some situation gois much greater than ge, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
3.jpg&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
4.jpg&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming and Details of Prototype==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the current turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable calculated in the constructor of class stage_state, given the current state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method decision::calc_decision() take state as argument and calculate the new state of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
Calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state variable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Flow of Prototype==&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Example Output==&lt;br /&gt;
&lt;br /&gt;
http://pastebin.com/bgaZGX2B&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Issues==&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| Week 1 || &lt;br /&gt;
*familiar with the overall structure of Wesnoth project&lt;br /&gt;
*familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| Week 2 ||&lt;br /&gt;
*design algorithm into detail&lt;br /&gt;
*learn useful interfaces&lt;br /&gt;
|-&lt;br /&gt;
| Week 3 ||&lt;br /&gt;
*implement prototypes&lt;br /&gt;
*test prototypes&lt;br /&gt;
|-&lt;br /&gt;
| Week 4 ||&lt;br /&gt;
*analyse feedbacks&lt;br /&gt;
*refine algorithm&lt;br /&gt;
|-&lt;br /&gt;
| Week 5 ||&lt;br /&gt;
*to be updated&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54720</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54720"/>
		<updated>2014-04-13T18:17:34Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;A rule-based expert system style solution utilizing dynamic programming to make decisions&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==General Idea==&lt;br /&gt;
&amp;lt;p&amp;gt;All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The priority is not constant, it is a function of the battlefield situation. For now I just focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;About the attack/retreat position: when I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Details of Priorities==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Attention: Following part contains some formulas, if they cannot be displayed correctly, please follow https://docs.google.com/document/d/1tHMdXp5-yh4eNymOVk3gQxvCJ26DZtaC9_1xC6cGwmo/edit?usp=sharing&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
My two goals of constructing priorities function are: a. Covering as much human players' experience rules as possible. b. Giving meaning to the values of priority, making them related to real game value and comparable under one criterion.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The &amp;quot;one criterion&amp;quot; I choose is gold. Every decisions made in the game have something to do with gold. If you use your unit to attack enemy, you lose some HP and your enemy lose some HP, but what you actually do is to use some of your golds to consume enemy's golds, because you use golds to recruit/recall and maintain unit, whose HP can be converted into gold, the same as the enemy. When you retreat, you try to prevent enemy to take your golds more than his consume. If you let some of your units to take a village, you are actually grab the opportunity of getting golds in future turns from your enemy. And usually the goal of a scenario is to minimize your enemy's gold and maximize yours.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
So the priorities generally are &amp;quot;how much profit can a CA bring back&amp;quot;. The higher priority, the more golds can a CA bring from enemy to you&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Priority of village CA===&lt;br /&gt;
&lt;br /&gt;
The priority of village CA (pv)should satisfy the following conditions:&lt;br /&gt;
&lt;br /&gt;
At the beginning of a scenario, the priority should be higher than others.&lt;br /&gt;
&lt;br /&gt;
The priority decreases as the number of own villages increases.&lt;br /&gt;
&lt;br /&gt;
The priority should become lower than other CA when own half of villages, since if the map is balance, the enemy would take another half at same time.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;half&amp;quot; mentioned above should be changed according to the time of day, this allow ghosts to  give up one or two villages when it is sunny.&lt;br /&gt;
&lt;br /&gt;
The priority gets lower as it gets closer to the end of the scenario, since only a few turn remained to let villages generate gold, the sum of golds villages generate is smaller.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Here is the function of priority:&lt;br /&gt;
&lt;br /&gt;
1.jpg&lt;br /&gt;
&lt;br /&gt;
Explanation:&lt;br /&gt;
&lt;br /&gt;
we get villages, enemy cannot get, so we get two times profit. vcis the number of villages we can get in current turn, gvis the gold one village can generate per turn, tsis total number of turns, tcis current turn, so ts-tcis how many turns remain(the total number of turn can not be sure, but it can estimate by the size of map---from my experience, its value is a half of the larger one between the wide and height of map, say, a 40*30 map takes 40/2=20 turns)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
This front part of function show us golds our villages generate totally, it can satisfy bullet 1 and 5. To satisfy bullet 2 to 4, I apply sigmoid function to it. vois the number of current own villages, Tis time of day modifier, vsis the total number of villages, T+vs2move the inflection point of sigmoid function to satisfy bullet 3 and 4, divide by 5(haven’t check this parameter) because I want to make the curve smoother.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
If we fix the front part to 20, as the number of own villages increases, we get this(total village=20 and time of day modifier is -2):&lt;br /&gt;
&lt;br /&gt;
1.png&lt;br /&gt;
&lt;br /&gt;
So it seems to satisfy bullet 2 to 4.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Priority of attack/defence/retreat  CA===&lt;br /&gt;
&lt;br /&gt;
The calculating part base on current combat CA, we simulate an attack inside “sandbox” and convert our lose and enemy’s lose into golds.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The priority, or golds we get is:&lt;br /&gt;
&lt;br /&gt;
2.jpg&lt;br /&gt;
&lt;br /&gt;
geis enemy’s lose and gois our lose. If in some situation gois much greater than ge, that means, if we attack in this turn, we lose more; if we just stand there, we lose more when next turn enemy attack us, so we retreat to “get” the golds we lose if we stand there.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
3.jpg&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
&lt;br /&gt;
Use gold as criterion make priority of CAs comparable and give priority real sense related to the game. We actually make three kinds of decision in every turn, use (gain_of_enemy, gain_of_us) to represent:&lt;br /&gt;
&lt;br /&gt;
4.jpg&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
And the goal of a scenario is to minimize your enemy's gold and maximize yours, this remind me dynamic programming, maybe I can build something on this.&lt;br /&gt;
&lt;br /&gt;
==Elements of Dynamic Programming and Details of Prototype==&lt;br /&gt;
===Stage and Stage Variable===&lt;br /&gt;
&lt;br /&gt;
A stage contains two turns, it start from the AI turn and end when the following enemy’s turn finish.&lt;br /&gt;
&lt;br /&gt;
Use class stage_state to record information of stages. Use stage_no_ as stage variable.&lt;br /&gt;
&lt;br /&gt;
===State and State Variable===&lt;br /&gt;
&lt;br /&gt;
The state is the delta of “gold” between own and enemy.&lt;br /&gt;
&lt;br /&gt;
The state variable is the difference of own_state_ and enemy_state_.&lt;br /&gt;
 &lt;br /&gt;
The state variable Calculated in the constructor of class stage_state.&lt;br /&gt;
&lt;br /&gt;
===Decision and Decision Variable===&lt;br /&gt;
&lt;br /&gt;
Use class decision to represent decision.&lt;br /&gt;
&lt;br /&gt;
Method calc_decisions() take state as argument and calculate the gain of specified decision.&lt;br /&gt;
&lt;br /&gt;
===Policy and Optimal Policy===&lt;br /&gt;
&lt;br /&gt;
Optimal policy is the sequence of decisions that maximize the state of final stage.&lt;br /&gt;
&lt;br /&gt;
calculate by calc_optimal_policy().&lt;br /&gt;
&lt;br /&gt;
===Equation of State Transition===&lt;br /&gt;
&lt;br /&gt;
Add initial state and gain of each stage's decision.&lt;br /&gt;
&lt;br /&gt;
The stage_state hold the result of this value.&lt;br /&gt;
&lt;br /&gt;
===Objective Function and Optimal Objective Function===&lt;br /&gt;
&lt;br /&gt;
Maximize the state_value_&lt;br /&gt;
&lt;br /&gt;
==Flow of Prototype==&lt;br /&gt;
&lt;br /&gt;
Use a queue to contain the stage_state. First initialize stage_1 as current stage, push it into the queue, then pass the queue to calc_decisions(). After that, we get all the final stage_state in the queue(for now, we look ahead 3 stages, or 6 turns, we have 2 decisions of each stage, so we have 1 stage_state for stage 1, 2 stage_state for stage 2, 4 stage_state for stage 3). Finally, calc_optimal_policy() will find the highest state_value among all 4 stage_state for stage 3, and output informations the stage_state hold in the member decision_.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Benchmarks==&lt;br /&gt;
&lt;br /&gt;
*Win 70% games against default RCA AI&lt;br /&gt;
&lt;br /&gt;
*its behaviour achieving the goal of 'analyze and make decisions globally', e.g. sometimes it give up local optimal to achieve global optimal.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Issues==&lt;br /&gt;
&lt;br /&gt;
*The state rise high when units die&lt;br /&gt;
**Partly because totally 6 turns of state of villages weight too much. I have dropped 0.1 weight for each turn because the further we look ahead, the rougher the state value.&lt;br /&gt;
**The very last hitpoint of a unit should be more valuable than first few lost. This make sense because we want injured unit retreat. I am trying multiple functions to show the rise of value for each hitpoint.&lt;br /&gt;
**“you can convert gold into units, but this takes some time - 1 turn to recruit, and 1-2 more turns to move to the frontline. So, there is 'current' gold (units on the frontline) and 'future' gold (income and units not yet on the frontline)” as crab_ mentioned.&lt;br /&gt;
*Experience of units should take into consider&lt;br /&gt;
*The balance of each part of state(units, villages)&lt;br /&gt;
*Current version do not have much difference with brute force search. How to show the advantage of dynamic programming, that is, the usage of intermediate results?&lt;br /&gt;
*The class stage_state may cost too much&lt;br /&gt;
*Multiple leader: most part of prototype is designed for multiple leaders but some part with “TODO: multiple leader” don’t work for multiple leaders.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/143&lt;br /&gt;
&lt;br /&gt;
https://github.com/wesnoth/wesnoth/pull/144&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| Week 1 || &lt;br /&gt;
*familiar with the overall structure of Wesnoth project&lt;br /&gt;
*familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| Week 2 ||&lt;br /&gt;
*design algorithm into detail&lt;br /&gt;
*learn useful interfaces&lt;br /&gt;
|-&lt;br /&gt;
| Week 3 ||&lt;br /&gt;
*implement prototypes&lt;br /&gt;
*test prototypes&lt;br /&gt;
|-&lt;br /&gt;
| Week 4 ||&lt;br /&gt;
*analyse feedbacks&lt;br /&gt;
*refine algorithm&lt;br /&gt;
|-&lt;br /&gt;
| Week 5 ||&lt;br /&gt;
*to be updated&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
See the description of prototype part and the code(https://github.com/Kevin-Xi/wesnoth/tree/prototype) of prototype for details.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=Git_for_Wesnoth_Crash_Course&amp;diff=54372</id>
		<title>Git for Wesnoth Crash Course</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=Git_for_Wesnoth_Crash_Course&amp;diff=54372"/>
		<updated>2014-04-04T08:18:07Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: use 'git remote show' to show config&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Foreword by iceiceice: This page is intended for new wesnoth devs and contributors who have never used git before. I'm hoping that it will be written *mainly* by newbies like myself who have just figured out enough to use it successfully, and therefore not contain a bunch of information superfluous for that purpose. At the time that I am writing this, I have managed to write about 8 github pull requests and get them merged into wesnoth, and I hope you will be able to do the same after reading this.&lt;br /&gt;
&lt;br /&gt;
== What is git? ==&lt;br /&gt;
&lt;br /&gt;
The most effective *absolute first timer* intro to git that I found when I began is here: &amp;lt;http://gitref.org&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I suggest that you read through this cover to cover, flipping through the pages using the red arrows at the bottom of the page. You can skip the part about &amp;quot;stashing&amp;quot;, but you should read carefully and understand the stuff about:&lt;br /&gt;
&lt;br /&gt;
* cloning a repo&lt;br /&gt;
* checking out a branch, creating a new branch, merging branches&lt;br /&gt;
* adding and committing changes&lt;br /&gt;
* taking diffs to make sure you understand exactly what you are committing&lt;br /&gt;
* resetting when you messed up and need to go back a few steps, and using git log and git reflog to assist with this&lt;br /&gt;
* moving content between local and remote locations, with push and pull&lt;br /&gt;
&lt;br /&gt;
== Setting up our workflow for wesnoth in git ==&lt;br /&gt;
&lt;br /&gt;
It is common in reading about git to see the terms &amp;quot;upstream / downstream&amp;quot;. This is often confusing at the beginning -- after all information is usually flowing in both directions, sometimes you are getting code from the wesnoth repo, and sometimes you are sending code back. Which way is up / down?&lt;br /&gt;
&lt;br /&gt;
The answer is that like a river, &amp;quot;downstream&amp;quot; is the direction most information flows. Any user that simply wants to build wesnoth from source, and not make any changes, will generally do so by cloning / pulling https://github.com/wesnoth/wesnoth. So wesnoth is the upstream repo, and the users are downstream. Since you are a developer, you will be forking the upstream repo and making changes, and hopefully with work eventually get these changes pulled into the upstream repo. That is the development cycle, and sending info back upstream is what makes you a developer. &lt;br /&gt;
&lt;br /&gt;
In this guide we are going to assume that you will use github besides just the git command line. The reason for this is that github has a lot of nice tools to offer and makes some things really easy, especially with comparing your branch to master and seeing diffs in a nice graphical interface, and with making pull requests. Technically, this means you will actually control *two* repos -- the local one on your machine, and a fork of the main wesnoth repo, on github. The first step is to set these two things up. Follow the instructions here: &lt;br /&gt;
&lt;br /&gt;
https://help.github.com/articles/fork-a-repo&lt;br /&gt;
&lt;br /&gt;
So the steps are:&lt;br /&gt;
# Fork the main wesnoth repo and tie the fork to your user account.&lt;br /&gt;
# Clone the fork onto your local machine, using 'git clone'.&lt;br /&gt;
# Configure remotes.&amp;lt;br&amp;gt;The github article describes how to configure the main wesnoth repo under the name &amp;quot;upstream&amp;quot;. Because we cloned from your fork, the fork is automatically configured under the name &amp;quot;origin&amp;quot;. You can see your configuration status with 'git remote show &amp;lt;branch_name&amp;gt;'.&lt;br /&gt;
&lt;br /&gt;
You might find this naming scheme confusing, as the name &amp;quot;origin&amp;quot; might also aptly describe the main wesnoth repo. So it might be a good idea to reconfigure that repo under the name &amp;quot;fork&amp;quot;. But if you do, you should keep in mind that much of the github help documentation will assume the name &amp;quot;origin&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Additionally, we ask that you configure your actual full name into git, following instructions here:&lt;br /&gt;
&lt;br /&gt;
https://help.github.com/articles/setting-your-username-in-git&lt;br /&gt;
&lt;br /&gt;
so that we can associate an actual person to every commit that makes it into the game. Your github account should also have your real name as well.&lt;br /&gt;
&lt;br /&gt;
== The basic workflow ==&lt;br /&gt;
&lt;br /&gt;
Here's the picture we now have, with 3 repos:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    upstream&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
                     origin&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
     local&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are three basic steps in the development cycle.&lt;br /&gt;
&lt;br /&gt;
# Syncing with upstream and making a topic branch for your patch.&lt;br /&gt;
# Committing your changes and pushing them to your fork.&lt;br /&gt;
# Making a pull request, getting it pulled, and cleaning up at the end.&lt;br /&gt;
&lt;br /&gt;
=== Syncing and Branching ===&lt;br /&gt;
&lt;br /&gt;
In this step, you will make sure your local master is up to date with the most recent development changes before beginning work. &lt;br /&gt;
&lt;br /&gt;
First make sure you are on master:&lt;br /&gt;
&lt;br /&gt;
  git checkout master&lt;br /&gt;
  git status&lt;br /&gt;
&lt;br /&gt;
Now, pull the upstream master to your machine:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    upstream&lt;br /&gt;
       |&lt;br /&gt;
       |&lt;br /&gt;
       |             origin&lt;br /&gt;
       |          &lt;br /&gt;
       v      &lt;br /&gt;
     local&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  git pull upstream master&lt;br /&gt;
&lt;br /&gt;
At this point your local master is up to date. While you are at it, you might as well sync up your *origin* as well:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    upstream&lt;br /&gt;
         &lt;br /&gt;
        &lt;br /&gt;
                     origin&lt;br /&gt;
               /--&amp;gt;  &lt;br /&gt;
            --/    &lt;br /&gt;
     local     &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  git push origin master&lt;br /&gt;
&lt;br /&gt;
So at this point, master should be the same everywhere. In this guide, we will never make any changes directly to master, and will *only* make changes to the topic branch, so no matter which repo you look at, master should be the same, and git will be comparing your work against the main wesnoth master.&lt;br /&gt;
&lt;br /&gt;
Now we are ready to make a new ''topic branch'', which will hold our work for this patch. We are on master, as a call to &amp;quot;git status&amp;quot; will confirm. So we will make our new branch:&lt;br /&gt;
&lt;br /&gt;
  git checkout -b great_new_feature&lt;br /&gt;
&lt;br /&gt;
As you learned in the gitref guide, you could also have done this by&lt;br /&gt;
&lt;br /&gt;
  git branch great_new_feature&lt;br /&gt;
  git checkout great_new_feature&lt;br /&gt;
&lt;br /&gt;
Since we were on master just before we did this, the current most up-to-date master will be the point of departure for our branch, which is best to avoid conflicts when it is eventually merged in.&lt;br /&gt;
&lt;br /&gt;
=== Committing your changes and pushing to your fork ===&lt;br /&gt;
&lt;br /&gt;
Now, you will make your changes to wesnoth and test them, using 'git add' and 'git commit', as you learned in the guide.&lt;br /&gt;
&lt;br /&gt;
Rules of thumb:&lt;br /&gt;
&lt;br /&gt;
# Write a good commit message.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;A commit message explains what happened and why in the commit. A good commit message is structured like a short email. The first line has special importance -- it should ideally be at most 80 characters, and play the roll of the &amp;quot;subject&amp;quot; of your email. These should be written in the imperative e.g. &amp;quot;add field to object and create accessor methods&amp;quot;, &amp;quot;fix broken constructor&amp;quot;, &amp;quot;remove unnecessary #include&amp;quot;, etc. Skip a line after this line, and put the &amp;quot;body&amp;quot; of the email after it. Small commits often won't have a body in the commit message, but every commit must have a commit message.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# Make appropriately sized commits.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;Each commit should correspond to a single logical step in your programming task. The commits make up the development history; people will look at your commits to try to figure out how we got to where we are today, and if necessary, may need to revert some commits / revert to an earlier state to fix something in the future. If you can't write a succinct commit message explaining what happened, then the commit is too large and too complicated. Another guideline is, you should at least think about committing almost every time you compile (depending on your habits).&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;On the other hand, you shouldn't make a commit for every line of code typed. If it would never make sense to revert to the current point in code, you probably shouldn't commit. So for example, if you define a field of an object, but it has no accessors and no way to be used in code, it would never make sense to revert to that time in history, so those changes should be rolled together in one commit. If you have a commit that just consists of &amp;quot;fixed some whitespace&amp;quot;, that should be squashed in with another commit.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;It is better to commit more often than less often, as before you are done you will have the opportunity to clean things up a bit, and it is much easier to squash commits together than to break up a commit that is too big.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;See also additional remarks here: [[DeveloperGuide]]&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
# When you are done, make a changelog entry, and commit it with the message &amp;quot;update changelog&amp;quot;.&lt;br /&gt;
#*We also have a players_changelog, which should contain only changes players are likely to notice, and be less technical than the main technical changelog.&lt;br /&gt;
#*There is also a file RELEASE_NOTES which basically gets turned into a forum post when the next release comes around, so if there is a major feature which should be advertised, that should go in there.&lt;br /&gt;
#*If this is your first commit, the devs will ask you to make an appropriate note about yourself in data/about.cfg&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, when your branch is ready, push it to your fork.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    upstream&lt;br /&gt;
        &lt;br /&gt;
        &lt;br /&gt;
                     origin&lt;br /&gt;
               /--&amp;gt;  &lt;br /&gt;
            --/    &lt;br /&gt;
     local     &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  git push origin great_new_feature&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now, if you navigate to the page for your fork, you will see &amp;quot;recently pushed branch great_new_feature (3 minutes ago)&amp;quot; on your main master page. Click &amp;quot;compare&amp;quot; to compare it against master. You will now see a list of the commits you made with their messages, and a color-highlighted diff of all the files you modified. You can click through the commits in order to see each of the changes you made and understand how your project progressed -- this is exactly what the devs will do when they review your pull request.&lt;br /&gt;
&lt;br /&gt;
In the github interface, you can always see a list of branches by clicking on &amp;quot;branches&amp;quot; in the line near the middle of the page &lt;br /&gt;
&lt;br /&gt;
&amp;quot;10,000+ commits     27 branches     248 releases     85 contributors&amp;quot;&lt;br /&gt;
&lt;br /&gt;
You can also checkout a branch from the main page using the drop down menu, and you can compare branches using the &amp;quot;compare&amp;quot; button.&lt;br /&gt;
&lt;br /&gt;
=== Cleaning up your branch ===&lt;br /&gt;
&lt;br /&gt;
Before making your pull request, if you looked at the diffs and the history and saw anything confusing or not quite right, now is your opportunity to change it. From the gitref guide, you know how to add more commits, and you know that you can use reset to go back in time and redo things. Reset is especially good if your last commit was too big and you want to break it up into a series of smaller commits -- the command 'git reset HEAD^' will leave your working directory the same, but jump back in time one commit and unstage all of those changes, so you can add a smaller subset of the files, commit those, then add the others, etc.&lt;br /&gt;
&lt;br /&gt;
More generally, using 'git reflog' you can see how to reset back any number of commits / commands. &lt;br /&gt;
&lt;br /&gt;
You can also use 'git commit --amend' to edit the commit message of the most recent commit.&lt;br /&gt;
&lt;br /&gt;
There are several more advanced cleanup features available with git, but we'll defer any further discussion.&lt;br /&gt;
&lt;br /&gt;
Every time you make changes, you will want to push to your origin to sync them up again. If you rewrote your history, then the push will be rejected, because the histories won't match up. Therefore, you will have to *force push* which will discard the old history and replace it with the new one:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    upstream&lt;br /&gt;
          &lt;br /&gt;
         &lt;br /&gt;
                     origin&lt;br /&gt;
               /--&amp;gt;  &lt;br /&gt;
            --/    &lt;br /&gt;
     local     &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  git push --force origin great_new_feature&lt;br /&gt;
&lt;br /&gt;
You can and should do this throughout your development if you want to look at the diffs of your changes as you work -- after all, the wesnoth source code is vast and it is easy to forget exactly where you are in your project. And you can keep cleaning up this way until you are satisfied. In this way, your github fork ends up being sort of like a personal web-based IDE plug-in / extension to help you with development.&lt;br /&gt;
&lt;br /&gt;
=== Making a pull request ===&lt;br /&gt;
&lt;br /&gt;
When you are finally done, select your branch on your fork, and click the green button to create a pull request. By default, the target will be wesnoth:master, the main repo. The description of the pull request which you enter will become the commit message of the merge operation when it finally goes through. If you fixed a bug, you should mention e.g. &amp;quot;fixed bug #5678&amp;quot; in the message, as this will automatically link to our gna bug tracker (bugs.wesnoth.org). That's pretty much all there is to it -- github has a guide here: https://help.github.com/articles/creating-a-pull-request&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    upstream&lt;br /&gt;
             &amp;lt;--\&lt;br /&gt;
                 \--&lt;br /&gt;
                     origin&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
     local     &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You'll then see your pull request appear in the &amp;quot;pull requests&amp;quot; section of the main wesnoth repo. At the time of writing this, we also have &amp;quot;travis-ci&amp;quot;, a &amp;quot;continuous integration system&amp;quot; configured on our repo. Travis will automatically try to compile wesnoth with your changes, as a reality check before your pull request is merged. It is not mandatory for the travis build to pass though, and additionally you may still make changes to your pull request by pushing more changes to your fork. Travis *should* then try to compile the updated list of changes after this. You can even still force push to erase the content of the PR and replace it with new content -- however you shouldn't make your PR until you are ready for it to be merged.&lt;br /&gt;
&lt;br /&gt;
Now you just have to wait for a member of the development team to review your pull request. You can often find someone on the irc channel, #wesnoth-dev. Additionally devs may comment directly on your PR with questions, so you might check it periodically for this.&lt;br /&gt;
&lt;br /&gt;
=== Cleaning up at the end ===&lt;br /&gt;
&lt;br /&gt;
Congratulations, your PR got merged! Now its time to cleanup and resync with the new changes. On the github page for your PR, you will now see a button with the option &amp;quot;delete this branch&amp;quot;. Since the content of great_new_feature is now merged into master, this branch no longer needs to exist as you won't work on it anymore, and future work will go onto a different branch. So you should delete it, which will delete the branch on *origin*, your github-associated repo.&lt;br /&gt;
&lt;br /&gt;
Since master has now changed and we want master to be synced up, you should now do step 1 again:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    upstream&lt;br /&gt;
       |&lt;br /&gt;
       |&lt;br /&gt;
       |             origin&lt;br /&gt;
       |          &lt;br /&gt;
       v      &lt;br /&gt;
     local&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  git checkout master&lt;br /&gt;
  git pull upstream master&lt;br /&gt;
&lt;br /&gt;
At this point git will understand that great_new_feature has been merged into master, so when you ask to delete this branch from local as well, it will do it:&lt;br /&gt;
&lt;br /&gt;
  git branch -d great_new_feature&lt;br /&gt;
&lt;br /&gt;
If you do these steps out of order, i.e. try to delete the branch before before syncing master, it will make a warning &amp;quot;Are you sure? You have unmerged changes on this branch...&amp;quot; etc.&lt;br /&gt;
&lt;br /&gt;
Finally, sync master on the origin as well:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    upstream&lt;br /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
                     origin&lt;br /&gt;
               /--&amp;gt;  &lt;br /&gt;
            --/    &lt;br /&gt;
     local     &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  git push origin master&lt;br /&gt;
&lt;br /&gt;
That's the end of the development cycle, and now you can begin on your next patch. Note that throughout the cycle, information only flowed counter-clockwise:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    upstream&lt;br /&gt;
       |     &amp;lt;--\&lt;br /&gt;
       |         \--&lt;br /&gt;
       |             origin&lt;br /&gt;
       |       /--&amp;gt;  &lt;br /&gt;
       v    --/    &lt;br /&gt;
     local&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If for some reason you find yourself doing something where information is flowing the other way, that is a good sign that something is going wrong, at least as far as this guide is concerned! (The exception is the steps in which we set up our repos -- don't overthink this.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Advanced / Alternative workflows ==&lt;br /&gt;
&lt;br /&gt;
You can use pull requests as described above whether you have commit access or not -- if you have commit access, then you can click to accept your own pull requests.&lt;br /&gt;
&lt;br /&gt;
However, if you have commit access, you may prefer not to use github / pull requests at all, and instead to push directly to wesnoth:master. You can still use topic branches to hold your work, and merge them to your local master using git merge, then use 'git push upstream master' to push them to the main wesnoth repo. &lt;br /&gt;
&lt;br /&gt;
If you are lazy, you might even skip making a topic branch and just commit to local master, although IMO this can make things more confusing if you get a merge conflict later.&lt;br /&gt;
&lt;br /&gt;
Either way, afaik the vast majority of wesnoth developers prefer to push directly to wesnoth:master.&lt;br /&gt;
&lt;br /&gt;
An extreme option appropriate for small changes is to skip the local repo as well, and simply commit changes directly from the github web interface. This might be appropriate for example if you forgot to commit a changelog entry in an earlier patch, or are simply changing some numbers used as parameters somewhere. Obviously if you change source code this way, you should make certain that everything still compiles immediately after! (Note: Don't actually do that.)&lt;br /&gt;
&lt;br /&gt;
== The ultimate cleanup power tool: git rebase ==&lt;br /&gt;
&lt;br /&gt;
Using things in the gitref guide, you can see how to use reset to undo mistakes you made by backing up and trying again, and for small commits that is probably fine. However, if you have a large and complicated series of changes, the better way to do this is using git rebase. Using git rebase in interactive mode, you can rewrite history by&lt;br /&gt;
&lt;br /&gt;
* reordering your commits&lt;br /&gt;
* discarding unwanted commits&lt;br /&gt;
* squashing commits together&lt;br /&gt;
* editing the content changes represented by an individual commit&lt;br /&gt;
* editing the commit message&lt;br /&gt;
&lt;br /&gt;
For our purposes, we assume you are working on a topic branch, and then you will only need to use the form&lt;br /&gt;
&lt;br /&gt;
  git rebase -i master&lt;br /&gt;
&lt;br /&gt;
You can see some documentation for using this command here: https://help.github.com/articles/interactive-rebase&lt;br /&gt;
&lt;br /&gt;
Using 'git rebase', you can make your commit history very *clean* -- instead of the history saying &amp;quot;I tried a bunch of things, undid some of them, tried some other things, and found something I liked&amp;quot;, your commit history can basically be &amp;quot;I took the simplest and most logical route to the solution that was finally the best&amp;quot;. When you review your commits, instead of feeling like a nasty blood-and-elbow-grease engineering project, it should feel like folding a perfect origami crane, to the extent possible ;)&lt;br /&gt;
&lt;br /&gt;
But to reiterate, this has a purpose. &lt;br /&gt;
# Other devs need to be able to understand the history.&lt;br /&gt;
# It should be possible to jump back in time by checking out one of your commits and find ourselves in a sensible state when we do so.&lt;br /&gt;
# If something breaks or some feature become incompatible, it should be possible to roll back only a piece of the implementation of your feature without removing the whole thing.&lt;br /&gt;
&lt;br /&gt;
If you want to use rebase to cleanup your history, it is important to do it *before* it is pulled into wesnoth, as once that happens it is basically no longer possible. If we rewrite history in the main wesnoth repo, then afterwards whenever any dev tries to 'git pull upstream master', their git will give them strange merge errors, stemming from the incompatible history, and then they will become nervous, jump on irc and exclaim some variation of &amp;quot;omg wtf bbq&amp;quot;. So except in extreme circumstances, we will never rebase the main wesnoth master, and for the same reason, we will never use &amp;quot;git push --force upstream master&amp;quot;. That's why if you want to do these things, it is important to do them on your *fork* before it makes it onto master.&lt;br /&gt;
&lt;br /&gt;
If you feel like it, you might read this, which is a historical email between early users / developers of git, in which Linus Torvalds explains about shared history of repos and when it is appropriate to use git rebase. &lt;br /&gt;
&lt;br /&gt;
http://www.mail-archive.com/dri-devel@lists.sourceforge.net/msg39091.html&lt;br /&gt;
&lt;br /&gt;
When you become comfortable with git rebase -i, it will greatly improve your workflow, as you will know that you can easily review and revise commit messages later, and easily reorder and squash commits. Typically I will now commit every single time I compile, and even if it is a commit which e.g. fixes a compile time error, or for some other reason I know it will be squashed in somewhere else, I give it the commit message &amp;quot;fixup&amp;quot;, as a note to myself to mark it thusly in the first git rebase pass. (Actually, while writing this I have just learned about the --autosquash feature of git rebase which takes this idea further, good stuff there.)&lt;br /&gt;
&lt;br /&gt;
Note that unlike Linus and github, in this guide we aren't thinking about your github fork repo as public -- we prefer to think of it as your personal private IDE/tool as I described earlier. If other wesnoth devs are cloning it or pulling your topic branches, we assume you will work it out with them. With that caveat the philosophies expressed on github and by Linus should generally apply to the wesnoth project as well.&lt;br /&gt;
&lt;br /&gt;
=== Solving merge conflicts with git rebase ===&lt;br /&gt;
&lt;br /&gt;
Github version: https://github.com/edx/edx-platform/wiki/How-to-Rebase-a-Pull-Request&lt;br /&gt;
&lt;br /&gt;
Suppose that you modify a file in the same place as someone else, and their change gets merged before yours. If git can't figure out how to merge, then someone will have to fix it. This could be done by whoever is merging the new content into master, but if you don't have commit access then that person is not you. One way that *you* could fix it is to resync your master to get the new changes locally, and then rebase your topic branch so that your changes are applied *after* the most recent changes.&lt;br /&gt;
&lt;br /&gt;
  git rebase master&lt;br /&gt;
&lt;br /&gt;
To understand what is happening, we should understand a bit more about how git works. Intuitively, git is all about &amp;quot;snapshots&amp;quot; of the project content. Every commit represents a snapshot, and you can look at this snapshot by checking out the commit. However git does not store each snapshot separately -- instead git defines the snapshots in terms of one another, and stores only the diffs. When you rebase your topic branch onto master, this will also update the &amp;quot;point of departure&amp;quot; where your branch was created, so that in the history, it will depart from the current head of master with the most recent changes. (Obviously, if you don't sync master until you are done working, then this subtlety to 'git rebase' is irrelevant.) &lt;br /&gt;
&lt;br /&gt;
If merging your branch with the current master would create a conflict, then when git rebase gets to the commit that creates the conflict, it will get confused when it tries to apply that diff, stop, tell you about the problem, and ask you to resolve it -- you can open up the offending file and go to the point where the conflict is happening, and git will leave a note for you of the content it is having trouble with. The note will look similar to what happens in the gitref guide here, under &amp;quot;merge conflicts&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
http://gitref.org/branching/#merge&lt;br /&gt;
&lt;br /&gt;
You just have to remove the note, make the file look like it should to make everything compatible, and type&lt;br /&gt;
&lt;br /&gt;
  git add .&lt;br /&gt;
  git rebase --continue&lt;br /&gt;
&lt;br /&gt;
Then the rebasing process will continue, and at the end your branch should be compatible with master and ready to be merged in.&lt;br /&gt;
&lt;br /&gt;
You can read more about this aspect of git rebase here if you like, although most likely you won't actually need more than we just talked about for wesnoth development. &lt;br /&gt;
&lt;br /&gt;
http://git-scm.com/book/en/Git-Branching-Rebasing&lt;br /&gt;
&lt;br /&gt;
That's all for this guide, have fun hacking on wesnoth!&lt;br /&gt;
&lt;br /&gt;
== Addendum ==&lt;br /&gt;
The following section contains commands with the -f / --force options. Those are usually used to rewrite history, which is fine for your own fork but disallowed for upstream. Do not use them on the main repository.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
control with which repo you interact&lt;br /&gt;
        git remote -v&lt;br /&gt;
 &lt;br /&gt;
  upstream should be      git@github.com:wesnoth/wesnoth.git&lt;br /&gt;
  origin should be        git@github.com:your_name/wesnoth.git&lt;br /&gt;
&lt;br /&gt;
if a command goes awry, you can go to &amp;quot;git reflog&amp;quot;, find the point just before you typed it, and reset --hard to that commit, and it should be like the bad thing never happened&lt;br /&gt;
 &lt;br /&gt;
revert every file in the repo to look like it did at the HEAD commit (last successful sync)&lt;br /&gt;
        git reset --hard HEAD&lt;br /&gt;
&lt;br /&gt;
get a clean slate by syncing with upstream&lt;br /&gt;
        git pull upstream master&lt;br /&gt;
        git fetch upstream&lt;br /&gt;
        git checkout -B master upstream/master&lt;br /&gt;
        git push --force origin master&lt;br /&gt;
if this errors, try again after&lt;br /&gt;
        git reset --hard HEAD&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
undo n commits, split/squash and apply again&lt;br /&gt;
        git reset HEAD~n&lt;br /&gt;
        git add -p&lt;br /&gt;
        git commit&lt;br /&gt;
        git push -f&lt;br /&gt;
&lt;br /&gt;
You can also push from one ‘ref’ to a different one: &lt;br /&gt;
        $ git push &amp;lt;remote&amp;gt; &amp;lt;from&amp;gt;:&amp;lt;to&amp;gt;&lt;br /&gt;
&lt;br /&gt;
completely overwrite branch b with a (from a to b)&lt;br /&gt;
        git push -f origin a:b&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
before any commit:&lt;br /&gt;
&lt;br /&gt;
* if the diff shows an invisible change in the first line - stop! Fix the file encoding back to UTF8 without BOM.&lt;br /&gt;
 &lt;br /&gt;
* don't forget the changelog (and, for visible changes, the players_changelog)&lt;br /&gt;
 &lt;br /&gt;
* try to keep the summary at 50 characters or less, maximum 72&amp;lt;br&amp;gt;commit meter in units of 10 chars:&lt;br /&gt;
 '         |         |         |         |         | 50 character summary limit&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
* [[Project#Developers]]&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54229</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54229"/>
		<updated>2014-03-24T12:02:52Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
==Attention==&lt;br /&gt;
&amp;lt;p&amp;gt;This page will be continuously modified and updated.&amp;lt;/p&amp;gt;&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;A rule-based expert system style solution&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
&amp;lt;p&amp;gt;All decisions of AI have three global strategic goals to achieve: hit enemy as heavy as possible, take villages as much as possible, protect self as cautious as possible. These three goals ideally should be achieve in every single action but they may conflict with each other, so these goals need to be associated with priority.(p1: hit enemy, p2: grab villages, p3: protect self). The results of these three goals is a bunch of actions, for example, to achieve &amp;quot;hit enemy&amp;quot; goal, the &amp;quot;actions to deal with enemy&amp;quot;(this is a CA) will be executed, that may contains &amp;quot;find a good place to attack&amp;quot;, &amp;quot;just stand there but not attack to prevent the enemy escape next turn&amp;quot;, &amp;quot;recruit unit suit for battle&amp;quot;, etc. And the actions to deal with village may contains &amp;quot;recruit scout-like unit&amp;quot;, &amp;quot;let the resilient unit to guard the village&amp;quot;, &amp;quot;let the unit with heal ability stand behind&amp;quot;, &amp;quot;block enemy from near the village&amp;quot;, etc. The actions to deal with protecting may contains &amp;quot;give up village if I have more than enemy&amp;quot;, &amp;quot;stand behind the resilient unit&amp;quot;, &amp;quot;retreat to the zero power-projection contour line&amp;quot;, etc.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The priority is not constant, it is a function of the battlefield situation. For now I just focus on time-space-economy(that is, time of day-terrain-gold and villages) because I think these are the three factors that influence all units in the battlefield and tightly associated with three goals.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;If in this turn, p1+p2-p3&amp;gt;0, the AI think it is worth to attack, otherwise it retreats.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;About the attack/retreat position: when I consider the question on&amp;quot;where to retreat&amp;quot;, I am thinking about a kind of &amp;quot;Damage contour map&amp;quot;, that is, each location of battlefield is associated with the damage the unit may bear at this location, that can be calculate by power_projection(I hope it can combine with defense of certain unit type at this terrain), Then circle the roughly same value to get a contour map and let unit to stand by a contour line that they can bear the damage on that line. Hopefully if enemies form a line, the same value positions on contour map may also be a line, so AI can form a line in this way. But when some of them punch into the middle of the AI's line, let units retreat to one single side which is near to their leader(so they will not run in every direction, that will leave the path free to its leader), and see if it is good to abandon unit that be surrounded, according to the HP, EXP and gold. So it hopefully form a skirmish line without hard-coding the rules about forming line.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Actually, I hope to implement CAs that form strategies by its own without hard-coding, only given the battlefield situation. The closest approach as far as I can see is to find patterns inside the rules, which is related to the three strategic goals.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
No. But I forked wesnoth on github and write a toy AI recently.&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| Week 1 || &lt;br /&gt;
*familiar with the overall structure of Wesnoth project&lt;br /&gt;
*familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| Week 2 ||&lt;br /&gt;
*design algorithm into detail&lt;br /&gt;
*learn useful interfaces&lt;br /&gt;
|-&lt;br /&gt;
| Week 3 ||&lt;br /&gt;
*implement prototypes&lt;br /&gt;
*test prototypes&lt;br /&gt;
|-&lt;br /&gt;
| Week 4 ||&lt;br /&gt;
*analyse feedbacks&lt;br /&gt;
*refine algorithm&lt;br /&gt;
|-&lt;br /&gt;
| Week 5 ||&lt;br /&gt;
*to be updated&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
This will be updated later.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54218</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54218"/>
		<updated>2014-03-24T04:07:03Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
==Attention==&lt;br /&gt;
&amp;lt;p&amp;gt;This page will be continuously modified and updated. The idea is out of date, I will update ASAP.&amp;lt;/p&amp;gt;&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;A way to analyse status of times, terrains, arms, special abilities of units and many other aspects to change the behavior modes of AI, with fuzzy logic to describe the AI rules and Combs method to prevent combinatorial explosion&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
&amp;lt;p&amp;gt;Following is my idea on how to analyse status of times, terrains, arms, special abilities of units and many other aspects to change the behavior modes of AI, with the help of fuzzy logic, combs method(to deal with combinatorial explosion) and defuzzification.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;I start from a naive idea: If it is day, let lawful units fight offensively and chaotic units retreat; If it is night, let chaotic units attack and lawful units fight defensively.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Two problems should be discussed: Defination of &amp;quot;offensively/defensively&amp;quot; and the factors that influence decisions. &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;First the defination: To be specific, AI play offensively means it tend to attack when there is a chance, tend to claim the villages and recruit new units to the battlefield. In contrast, AI play defensively means it tend to retreat, it think the units is valuable and only attack when it is quite sure, it may give up villages when the time of day is not right for its unit. But how much does an AI want to retreat? Does retreating five hexes and retreating six hexes make any difference? It seems that the output is fuzzy, so may be we could apply fuzzy logic on it. For example, if it is night and enemies is &amp;quot;very close(a fuzzy set)&amp;quot; and terrain is &amp;quot;a kind of bad(another fuzzy set)&amp;quot;, the merman warrior &amp;quot;badly(fuzzy output)&amp;quot; want to retreat.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Second the factors: The factors that influence decisions are far more beyond time of day. For example, if a merman warrior should cross flat to retreat at night, it would be better to rush into the river ahead. Even we just consider terrain for now: According to the acient Chinese book The Art of War by Sun Tzu(I really like this book and it really help me a lot in Wesnoth battlefield), there are six kinds of terrain(the 16 types of Wesnoth can classified in a category for a specific unit): accessible, enmeshes, disadvantageous to both side, narrow and precipitous, hazardous, distant. In each kind of terrain we should decide in different way. So the complexity of situation bring us combinatorial explosion of AI rules. But if we use fuzzy logic, there is a way called &amp;quot;Combs method(http://en.wikipedia.org/wiki/Combs_method)&amp;quot; can prevent this problem.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To sum up, use fuzzy logic to describe the AI rules and Combs method to prevent combinatorial explosion, I believe we can teach AI to make global attack/retreat decision.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
No. But I forked wesnoth on github and write a toy AI recently.&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| Week 1 || &lt;br /&gt;
*familiar with the overall structure of Wesnoth project&lt;br /&gt;
*familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| Week 2 ||&lt;br /&gt;
*design algorithm into detail&lt;br /&gt;
*learn useful interfaces&lt;br /&gt;
|-&lt;br /&gt;
| Week 3 ||&lt;br /&gt;
*implement prototypes&lt;br /&gt;
*test prototypes&lt;br /&gt;
|-&lt;br /&gt;
| Week 4 ||&lt;br /&gt;
*analyse feedbacks&lt;br /&gt;
*refine algorithm&lt;br /&gt;
|-&lt;br /&gt;
| Week 5 ||&lt;br /&gt;
*to be updated&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
This will be updated later.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54069</id>
		<title>SoC2014 kevin AI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=SoC2014_kevin_AI&amp;diff=54069"/>
		<updated>2014-03-20T18:31:37Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: gsoc2014 kevin's proposal on improve AI&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{SoC2014Student}}&lt;br /&gt;
[[Category:SoC_Ideas_AI_Global_strategy]]&lt;br /&gt;
==Attention==&lt;br /&gt;
&amp;lt;p&amp;gt;This page will be continuously modified and updated.&amp;lt;/p&amp;gt;&lt;br /&gt;
==Description==&lt;br /&gt;
&amp;lt;h4&amp;gt;Kevin Xi - AI: Improve AI by implementing global attack/retreat decision&amp;lt;/h4&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;A way to analyse status of times, terrains, arms, special abilities of units and many other aspects to change the behavior modes of AI, with fuzzy logic to describe the AI rules and Combs method to prevent combinatorial explosion&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Idea==&lt;br /&gt;
&amp;lt;p&amp;gt;Following is my idea on how to analyse status of times, terrains, arms, special abilities of units and many other aspects to change the behavior modes of AI, with the help of fuzzy logic, combs method(to deal with combinatorial explosion) and defuzzification.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;I start from a naive idea: If it is day, let lawful units fight offensively and chaotic units retreat; If it is night, let chaotic units attack and lawful units fight defensively.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Two problems should be discussed: Defination of &amp;quot;offensively/defensively&amp;quot; and the factors that influence decisions. &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;First the defination: To be specific, AI play offensively means it tend to attack when there is a chance, tend to claim the villages and recruit new units to the battlefield. In contrast, AI play defensively means it tend to retreat, it think the units is valuable and only attack when it is quite sure, it may give up villages when the time of day is not right for its unit. But how much does an AI want to retreat? Does retreating five hexes and retreating six hexes make any difference? It seems that the output is fuzzy, so may be we could apply fuzzy logic on it. For example, if it is night and enemies is &amp;quot;very close(a fuzzy set)&amp;quot; and terrain is &amp;quot;a kind of bad(another fuzzy set)&amp;quot;, the merman warrior &amp;quot;badly(fuzzy output)&amp;quot; want to retreat.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Second the factors: The factors that influence decisions are far more beyond time of day. For example, if a merman warrior should cross flat to retreat at night, it would be better to rush into the river ahead. Even we just consider terrain for now: According to the acient Chinese book The Art of War by Sun Tzu(I really like this book and it really help me a lot in Wesnoth battlefield), there are six kinds of terrain(the 16 types of Wesnoth can classified in a category for a specific unit): accessible, enmeshes, disadvantageous to both side, narrow and precipitous, hazardous, distant. In each kind of terrain we should decide in different way. So the complexity of situation bring us combinatorial explosion of AI rules. But if we use fuzzy logic, there is a way called &amp;quot;Combs method(http://en.wikipedia.org/wiki/Combs_method)&amp;quot; can prevent this problem.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;To sum up, use fuzzy logic to describe the AI rules and Combs method to prevent combinatorial explosion, I believe we can teach AI to make global attack/retreat decision.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IRC==&lt;br /&gt;
Kevin_Xi&lt;br /&gt;
&lt;br /&gt;
==Questionnaire==&lt;br /&gt;
&lt;br /&gt;
'''1) Basics'''&lt;br /&gt;
&lt;br /&gt;
'''1.1) Write a small introduction to yourself.'''&lt;br /&gt;
Hi, I am Kevin, a junior Software Engineering student in Beihang University. I love open source, I use and learn from it all the time. I played The Battle of Wesnoth for about a year. In my free time(besides coding), I like taking some MOOC course about computer, game design, music and thinking. I also like watching Star Trek and enjoying Jazz, the great works of Horace Sliver make me feel relaxed.&lt;br /&gt;
&lt;br /&gt;
'''1.2) State your preferred email address.'''&lt;br /&gt;
&lt;br /&gt;
kevin.xgr [at] gmail.com&lt;br /&gt;
&lt;br /&gt;
'''1.3) If you have chosen a nick for IRC and Wesnoth forums, what is it?'''&lt;br /&gt;
&lt;br /&gt;
Kevin_Xi on IRC, FAKevin on forum.&lt;br /&gt;
&lt;br /&gt;
'''1.4) Why do you want to participate in summer of code?'''&lt;br /&gt;
&lt;br /&gt;
I think become part of something bigger than you(open source) is a kind of intrinsic motivation of human being and my heart is always vibrated to the great moment that a software come to life. I believe &amp;quot;Learning by Doing&amp;quot; and want to improve my skills, both on coding and communicating, by participate in a real-life project. The GSoC project is a good trigger for me to get my hands dirty.&lt;br /&gt;
&lt;br /&gt;
'''1.5) What are you studying, subject, level and school?'''&lt;br /&gt;
&lt;br /&gt;
I am a third year student on Software Engineering, Beihang University. The course I have finished are: Object-oriented programming with C++, Principles of Computer Organization and Architecture, Software Engineering, Database System Concepts, Network, Operating System, Compiler, System Analysis and Design and so on.&lt;br /&gt;
&lt;br /&gt;
'''1.6) What country are you from, at what time are you most likely to be able to join IRC?'''&lt;br /&gt;
&lt;br /&gt;
China. 8:30~10:00am, 1:30~12:00pm UTC+8.&lt;br /&gt;
&lt;br /&gt;
'''1.7) Do you have other commitments for the summer period ? Do you plan to take any vacations ? If yes, when.'''&lt;br /&gt;
&lt;br /&gt;
I would like to take this as a full time job, so I don't arrange other things.&lt;br /&gt;
&lt;br /&gt;
'''2) Experience'''&lt;br /&gt;
&lt;br /&gt;
'''2.1) What programs/software have you worked on before?'''&lt;br /&gt;
&lt;br /&gt;
Part of my individual project is available here: https://github.com/Kevin-Xi?tab=repositories. Many of them are tools for my daily life.&lt;br /&gt;
For teamwork, the latest project I am working on is a Raspberry Pi based smart home system. I develop web server use Django on Raspberry Pi.&lt;br /&gt;
&lt;br /&gt;
'''2.2) Have you developed software in a team environment before? (As opposed to hacking on something on your own)'''&lt;br /&gt;
&lt;br /&gt;
Yes, I teamed up with my classmates on C++, C#, Java, Django project several times. We often follow agile development methods and use git to manage our project.&lt;br /&gt;
&lt;br /&gt;
'''2.3) Have you participated to the Google Summer of Code before? As a mentor or a student? In what project? Were you successful? If not, why?'''&lt;br /&gt;
&lt;br /&gt;
No. I just know it this year from my RSS reader&lt;br /&gt;
&lt;br /&gt;
'''2.4) Are you already involved with any open source development projects? If yes, please describe the project and the scope of your involvement.'''&lt;br /&gt;
&lt;br /&gt;
No. But I want to get involved. Sometimes I modify Linux kernel and re-compile it to learn it.&lt;br /&gt;
&lt;br /&gt;
'''2.5) Gaming experience - Are you a gamer?'''&lt;br /&gt;
&lt;br /&gt;
Yes. I play a lot, enjoy it and feel there are many things to learn from it, so I taked gamification course at Coursera. I do well in class and get a 10 out of 10 in a recently design work.&lt;br /&gt;
&lt;br /&gt;
'''2.5.1) What type of gamer are you?'''&lt;br /&gt;
&lt;br /&gt;
According to the Bartle Player Type Model, I am an archiver who like complete all levels and explorer who like finding out all possibilities in game.&lt;br /&gt;
&lt;br /&gt;
'''2.5.2) What type of games?'''&lt;br /&gt;
&lt;br /&gt;
Role-playing, Alternate reality, Educational, Puzzles, Strategy.&lt;br /&gt;
&lt;br /&gt;
'''2.5.3) What type of opponents do you prefer?'''&lt;br /&gt;
&lt;br /&gt;
Clever and polite. The most important is I can learn from my opponents.&lt;br /&gt;
&lt;br /&gt;
'''2.5.4) Are you more interested in story or gameplay?'''&lt;br /&gt;
&lt;br /&gt;
It depends on the type of game. I like fantasy background in RPG, gameplay in puzzles.&lt;br /&gt;
&lt;br /&gt;
'''2.5.5) Have you played Wesnoth? If so, tell us roughly for how long and whether you lean towards single player or multiplayer.'''&lt;br /&gt;
&lt;br /&gt;
I played it for about 1 year. I played all official campaigns and like to be a observer of online battles.&lt;br /&gt;
&lt;br /&gt;
'''2.6) If you have contributed any patches to Wesnoth, please list them below. You can also list patches that have been submitted but not committed yet and patches that have not been specifically written for GSoC. If you have gained commit access to our repository (during the evaluation period or earlier) please state so.'''&lt;br /&gt;
&lt;br /&gt;
No. But I forked wesnoth on github and write a toy AI recently.&lt;br /&gt;
&lt;br /&gt;
'''3) Communication skills'''&lt;br /&gt;
&lt;br /&gt;
'''3.1) Though most of our developers are not native English speakers, English is the project's working language. Describe your fluency level in written English.'''&lt;br /&gt;
&lt;br /&gt;
There are four kinds of abilities one should develop when learning languages: Listening, Speaking, Reading and Writing. For listening, I am able to understand MOOC class videos without subtitles. For Speaking, although I do not have so much chances to talk directly to a native speaker, I sometimes practice my speaking skills by read after some English podcast online. For Reading, I choose origin English version textbook and read technical papers for information I want. For Writing, I write programming log as diary in English.&lt;br /&gt;
&lt;br /&gt;
'''3.2) What spoken languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
Chinese, English&lt;br /&gt;
&lt;br /&gt;
'''3.3) Are you good at interacting with other players? Our developer community is friendly, but the player community can be a bit rough.'''&lt;br /&gt;
&lt;br /&gt;
Yes, I am a rational and friendly person.&lt;br /&gt;
&lt;br /&gt;
'''3.4) Do you give constructive advice?'''&lt;br /&gt;
&lt;br /&gt;
Yes. Sometimes I also give simple ideas to begin brainstorm among team members.&lt;br /&gt;
&lt;br /&gt;
'''3.5) Do you receive advice well?'''&lt;br /&gt;
&lt;br /&gt;
I have a systematic approach to take advice and learn from it .&lt;br /&gt;
&lt;br /&gt;
'''3.6) Are you good at sorting useful criticisms from useless ones?'''&lt;br /&gt;
&lt;br /&gt;
I think this depend how deep I understand the system. For wesnoth, since I don't familiar now, I wouldn't say I can. But I will try to ask developers and get familiar on it.&lt;br /&gt;
&lt;br /&gt;
'''3.7) How autonomous are you when developing ? Would you rather discuss intensively changes and not start coding until you know what you want to do or would you rather code a proof of concept to &amp;quot;see how it turn out&amp;quot;, taking the risk of having it thrown away if it doesn't match what the project want'''&lt;br /&gt;
&lt;br /&gt;
As a Software Engineering student, I consider both. But the way I like most is develop a prototype(that allow you to test your idea and fail quickly if it don't work) and test it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''4) Project'''&lt;br /&gt;
&lt;br /&gt;
'''4.1) Did you select a project from our list? If that is the case, what project did you select? What do you want to especially concentrate on?'''&lt;br /&gt;
&lt;br /&gt;
Yes, I choose &amp;quot;Improve AI by implementing global attack/retreat decision&amp;quot;. I want to concentrate on figuring out in what situation the AI should change its strategy, in a quantifiable way with the help of fuzzy logic(to be efficient, economic) and other mathematical principles.&lt;br /&gt;
&lt;br /&gt;
'''4.2) If you have invented your own project, please describe the project and the scope.'''&lt;br /&gt;
&lt;br /&gt;
No.&lt;br /&gt;
&lt;br /&gt;
'''4.3) Why did you choose this project?'''&lt;br /&gt;
&lt;br /&gt;
I like AI since I was a freshman, I have some experiences on AI and got a 100 out of 100 in my Machine Learning course on MOOC. Since AI of wesnoth should include strategy analysis on terrain, time, arms, special abilities of units and many other aspects, I read the famous ancient Chinese book The Art of War by Sun Tzu since I was young, so I like and good at analysing these elements.&lt;br /&gt;
&lt;br /&gt;
'''4.4) Include an estimated timeline for your work on the project. Don't forget to mention special things like &amp;quot;I booked holidays between A and B&amp;quot; and &amp;quot;I got an exam at ABC and won't be doing much then&amp;quot;.'''&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Time !! Tasks&lt;br /&gt;
|-&lt;br /&gt;
| Week 1 || &lt;br /&gt;
*familiar with the overall structure of Wesnoth project&lt;br /&gt;
*familiar with the AI part &lt;br /&gt;
|-&lt;br /&gt;
| Week 2 ||&lt;br /&gt;
*design algorithm into detail&lt;br /&gt;
*learn useful interfaces&lt;br /&gt;
|-&lt;br /&gt;
| Week 3 ||&lt;br /&gt;
*implement prototypes&lt;br /&gt;
*test prototypes&lt;br /&gt;
|-&lt;br /&gt;
| Week 4 ||&lt;br /&gt;
*analyse feedbacks&lt;br /&gt;
*refine algorithm&lt;br /&gt;
|-&lt;br /&gt;
| Week 5 ||&lt;br /&gt;
*to be updated&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''4.5) Include as much technical detail about your implementation as you can'''&lt;br /&gt;
&lt;br /&gt;
This will be updated later.&lt;br /&gt;
&lt;br /&gt;
'''4.6) What do you expect to gain from this project?'''&lt;br /&gt;
&lt;br /&gt;
Familiar with the workflow of an open source community and keep working as a part of community. Improve my skill on programming and communication. The joy of accomplishment of the project.&lt;br /&gt;
&lt;br /&gt;
'''4.7) What would make you stay in the Wesnoth community after the conclusion of SOC?'''&lt;br /&gt;
&lt;br /&gt;
Creative, smart and friendly people in the community. Working as a part of something bigger than me.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5) Practical considerations'''&lt;br /&gt;
&lt;br /&gt;
'''5.1) Are you familiar with any of the following tools or languages?'''&lt;br /&gt;
&lt;br /&gt;
;Git (used for all commits)&lt;br /&gt;
:I manage almost all of my code, either individual or team work,  on git&lt;br /&gt;
    &lt;br /&gt;
;C++ (language used for all the normal source code)&lt;br /&gt;
:I feel comfortable with the basic and the advanced features of C++, and code a lot.&lt;br /&gt;
&lt;br /&gt;
;STL, Boost, Sdl (C++ libraries used by Wesnoth)&lt;br /&gt;
:I use STL, but don't familiar with Boost and Sdl&lt;br /&gt;
&lt;br /&gt;
;Python (optional, mainly used for tools)&lt;br /&gt;
:Python is my daily life tool. I catch podcast and process text file with it. And I use Django to set up my web server.&lt;br /&gt;
&lt;br /&gt;
;build environments (eg cmake/scons)&lt;br /&gt;
:I use g++ and make&lt;br /&gt;
   &lt;br /&gt;
;WML (the wesnoth specific scenario language)&lt;br /&gt;
:It &amp;quot;make coding accessible&amp;quot;&lt;br /&gt;
&lt;br /&gt;
;Lua (used in combination with WML to create scenarios) &lt;br /&gt;
:No, I don't have experience on Lua. But I can be a fast learner with the experience of C++, python, Common Lisp, Java and so on.&lt;br /&gt;
&lt;br /&gt;
'''5.2) Which tools do you normally use for development? Why do you use them?'''&lt;br /&gt;
Vim, g++, gdb, make, git. Because these tools are build-in and powerful, and I can learn and modify them since open source. I will use IDE for some big project.&lt;br /&gt;
&lt;br /&gt;
'''5.3) What programming languages are you fluent in?'''&lt;br /&gt;
&lt;br /&gt;
C++, Java, Python, basic part of Common Lisp&lt;br /&gt;
&lt;br /&gt;
'''5.4) Would you mind talking with your mentor on telephone / internet phone?'''&lt;br /&gt;
Not at all, I will send my phone number to my mentor if I am accepted.&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=WritingYourOwnAI&amp;diff=54034</id>
		<title>WritingYourOwnAI</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=WritingYourOwnAI&amp;diff=54034"/>
		<updated>2014-03-20T08:38:05Z</updated>

		<summary type="html">&lt;p&gt;KevinXi: when best_defense is not -1, we move&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Note ==&lt;br /&gt;
'''THIS PAGE IS UNDER CONSTRUCTION.'''&lt;br /&gt;
&lt;br /&gt;
== Writing your own AI ==&lt;br /&gt;
&lt;br /&gt;
Wesnoth supports a pluggable AI system that allows programmers to write their own AIs in C++ or Python.&lt;br /&gt;
You might like to look this over before starting: [[WhyWritingAWesnothAIIsHard]]&lt;br /&gt;
&lt;br /&gt;
== Python ==&lt;br /&gt;
&lt;br /&gt;
For information on the Python AI API look at [[ReferencePythonAPI]].&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
The rest of this page describes the C++ AI API. To write an AI in C++, you need to derive a class from ''ai_interface'' (defined in '''ai.hpp'''), and implement the function ''play_turn()'' which will be called every time your AI is expected to play a turn.&lt;br /&gt;
&lt;br /&gt;
Class ''ai_interface'' contains three important functions&lt;br /&gt;
which allow you to execute the three basic types of move available in the game:&lt;br /&gt;
&lt;br /&gt;
* ''attack_enemy()'', which is used to order an attack on an enemy unit,&lt;br /&gt;
* ''move_unit()'', which is used to order a unit to move from one location to another, and&lt;br /&gt;
* ''recruit()'', which is used to recruit a new unit.&lt;br /&gt;
&lt;br /&gt;
Of course, to decide where units are to move and attack, you must have information about the state of the game - the dimensions and layout of the map, the locations and type of units on the map, the types of units your side can recruit, and information about your allies and enemies.&lt;br /&gt;
&lt;br /&gt;
Firstly, a type ''location'' is defined, which defines any location on the map.  It has members ''x'' and ''y''.  In '''pathfind.hpp''' there are a number of functions which will tell you useful things about locations -- whether two locations&lt;br /&gt;
are adjacent, all the locations adjacent to a certain location,&lt;br /&gt;
and the distance between locations.&lt;br /&gt;
&lt;br /&gt;
A type ''move_map'' is defined as a ''std::multimap&amp;lt;location,location&amp;gt;''.  Note that ''std::multimap'' is of course a standard C++ container, and cannot be documented here.  http://www.sgi.com/tech/stl/ is a good reference on standard C++ containers.  The purpose of a ''move_map'' is to show all the possible moves for a side.  It can either be a ''source -&amp;gt; destination'' map, which associates the locations of all the units a side has to all the possible places they can move to, or a ''destination -&amp;gt; source'' map, which associates all the locations all the units a side has can get to, to all the places they are now.&lt;br /&gt;
&lt;br /&gt;
The function ''calculate_possible_moves()'' is provided&lt;br /&gt;
as a useful utility function. It can give you maps for where all&lt;br /&gt;
your units can move, or where all your enemy's movements&lt;br /&gt;
can move when it's their turn. This is a very important&lt;br /&gt;
function to use to work out all the possible places your units can move to.&lt;br /&gt;
&lt;br /&gt;
''ai_interface'' also defines an ''info'' type.  This type contains a number of references to various game objects which you&lt;br /&gt;
will need access to in order to make moves.  The two most important of these objects are the unit map (unit_map units)&lt;br /&gt;
and the game map (gamemap map).&lt;br /&gt;
&lt;br /&gt;
The unit map is of type ''std::map&amp;lt;location,unit&amp;gt;''&lt;br /&gt;
and associates locations with units.  This object can be used to find the location of, and information about, every unit on the board.  See '''unit.hpp''' for a definition of the ''unit'' object.&lt;br /&gt;
&lt;br /&gt;
The game map allows you to inspect the dimensions and layout of the playing board.  Given a location, it can tell you the&lt;br /&gt;
terrain type at that location.  See '''map.hpp''' for a definition of this object.  You can combine this class with use of the functions in '''pathfind.hpp''' to find various&lt;br /&gt;
information about where units can move to.&lt;br /&gt;
&lt;br /&gt;
The team class (defined in '''team.hpp''') is also very important.  Each side is represented by a ''team'' object. The team object can tell you the gold balance of a team, which villages (note that internally, villages are often called 'towers') the team owns, what units the team can recruit,&lt;br /&gt;
and which other teams are this teams friends or enemies.&lt;br /&gt;
&lt;br /&gt;
The utility function ''current_team()'' can be used to get a reference to the team that your AI is in control of, but you&lt;br /&gt;
can also use the vector ''teams'' inside the info object to get a list of all teams.&lt;br /&gt;
&lt;br /&gt;
If you want to make your AI customizable within the configuration file, you can gain access to any parameters passed to your AI using ''team::ai_parameters()''.  This returns an object of type ''config'' (see '''config.hpp''').  These ''config'' objects are representations of WML document fragments.  When the user defines your side, if they put an [ai] tag inside it, everything inside the [ai] tag will be returned by ''team::ai_parameters()''.&lt;br /&gt;
&lt;br /&gt;
== Using your AI ==&lt;br /&gt;
&lt;br /&gt;
Finally, when you have your AI ready to go, you can add it to the ''create_ai()'' function in '''ai.cpp'''.  Suppose you called your class ''killer_ai'', you could add it like so:&lt;br /&gt;
&lt;br /&gt;
 if(name == &amp;quot;killer_ai&amp;quot;)&lt;br /&gt;
     return new killer_ai(info);&lt;br /&gt;
&lt;br /&gt;
Then, you can define a side to use your AI in [[ReferenceWML|WML]]:&lt;br /&gt;
&lt;br /&gt;
 ai_algorithm=killer_ai&lt;br /&gt;
&lt;br /&gt;
and when that side is created, it'll use your AI!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== An example ==&lt;br /&gt;
&lt;br /&gt;
Let us conclude with a small sample AI, called ''sample_ai''.  How should this AI behave?&lt;br /&gt;
&lt;br /&gt;
* First it should detect if there are any enemies in range,&lt;br /&gt;
and if there are it should attack them by moving onto the&lt;br /&gt;
best defensive terrain next to them.  Attacks should be made with the weapon for which damage*strikes*chance to hit is&lt;br /&gt;
the highest.&lt;br /&gt;
* If there are no enemies in range, it should move units onto villages that don't already belong to it.&lt;br /&gt;
* If there are no enemies or villages in range, it should move toward the enemy leader along the shortest possible route.&lt;br /&gt;
* At the end of its turn, it should recruit random units until it runs out of money or doesn't have any space.&lt;br /&gt;
&lt;br /&gt;
In the following example, I will place all functions in-line&lt;br /&gt;
rather than in the cpp file. To do this properly, of course you should put them in the cpp file.  The entire definition of this AI can be found in '''ai.cpp''' and '''ai.hpp''' in the source distribution.&lt;br /&gt;
&lt;br /&gt;
We start the definition,&lt;br /&gt;
&lt;br /&gt;
  class sample_ai : public ai_interface {&lt;br /&gt;
  public:&lt;br /&gt;
      sample_ai(info&amp;amp; i) : ai_interface(i) {}&lt;br /&gt;
&lt;br /&gt;
We have defined the constructor which takes an ''info'' object&lt;br /&gt;
and passes it straight onto ai_interface.  We don't need to&lt;br /&gt;
store anything ourselves in this simple AI.  (Although it would be fine to have data members if we wanted them.)&lt;br /&gt;
&lt;br /&gt;
Next we define the main function, ''play_turn()'':&lt;br /&gt;
&lt;br /&gt;
      void play_turn() {&lt;br /&gt;
          do_attacks();&lt;br /&gt;
          get_villages();&lt;br /&gt;
          do_moves();&lt;br /&gt;
          do_recruitment();&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
Just a series of calls to functions we are about to write which do the actual work.  Firstly, ''do_attacks()''. We start by&lt;br /&gt;
calculating all the moves our units can make:&lt;br /&gt;
&lt;br /&gt;
  private:&lt;br /&gt;
      void do_attacks() {&lt;br /&gt;
          std::map&amp;lt;location,paths&amp;gt; possible_moves;&lt;br /&gt;
          move_map srcdst, dstsrc;&lt;br /&gt;
          calculate_possible_moves(possible_moves,srcdst,dstsrc,false);&lt;br /&gt;
&lt;br /&gt;
Note that the ''possible_moves'' thing is of little direct interest.  It contains details of exactly which tiles the unit&lt;br /&gt;
moves along to get from one tile to another.  This is useful for the display to know about when it draws the unit moving, but as an AI programmer, it's not likely you'll ever care about&lt;br /&gt;
what it contains. Just pass it along to the ''move_unit()'' function so it can draw the unit moving along the correct path.&lt;br /&gt;
&lt;br /&gt;
The things we're interested in are ''srcdst'', and especially ''dstsrc'', which will tell us all the hexes our units can reach.&lt;br /&gt;
We want to check if any of these hexes are next to an enemy unit.  Let's walk over the units and see if we can reach any of them:&lt;br /&gt;
&lt;br /&gt;
          for(unit_map::const_iterator i = get_info().units.begin(); i != get_info().units.end(); ++i) {&lt;br /&gt;
              if(current_team().is_enemy(i-&amp;gt;second.side()) {&lt;br /&gt;
&lt;br /&gt;
We're iterating over all units, but we're only interested in units that are enemies of our side.  So, we access our team object, and ask if the side the unit is on is an enemy.  If it is, then we're interested in seeing if any of our units can move to a hex that's adjacent to the enemy unit.  We do this by getting the six locations around the enemy unit:&lt;br /&gt;
&lt;br /&gt;
                  location adjacent_tiles[6];&lt;br /&gt;
                  get_adjacent_tiles(i-&amp;gt;first,adjacent_tiles);&lt;br /&gt;
&lt;br /&gt;
This kind of call is very common in the game's code -- make an array of 6 locations, and fill them up with the locations adjacent to a certain location.  We actually want to find the position to attack from which gives our unit the best possible defense. So, we initialize some variables to find the best possible defense:&lt;br /&gt;
&lt;br /&gt;
                  int best_defense = -1;&lt;br /&gt;
                  std::pair&amp;lt;location,location&amp;gt; best_movement;&lt;br /&gt;
&lt;br /&gt;
The value of ''best_defense'' will of course be between 1 and 100, but we give it a value of -1 to mean 'not initialized', since we haven't found any possible attacks at all yet.  Variable ''best_movement'' will contain the destination/source pair that gives the best possible defense for our attacking unit.&lt;br /&gt;
&lt;br /&gt;
                  for(size_t n = 0; n != 6; ++n) {&lt;br /&gt;
                      typedef move_map::const_iterator Itor;&lt;br /&gt;
                      std::pair&amp;lt;Itor,Itor&amp;gt; range = dstsrc.equal_range(adjacent_tiles[n]);&lt;br /&gt;
&lt;br /&gt;
If you don't understand how ''equal_range'' works, then look up documentation on how the standard container multimap works.  ''range'' now refers to all the possible movements that can end&lt;br /&gt;
with our unit being at ''adjacent_tiles[n]''.  Let's iterate over all those movements, and find if any of them give a better defensive rating than our current best defense.  We'll start our iteration by creating some aliases that ensure we don't go crazy ;)&lt;br /&gt;
&lt;br /&gt;
                      while(range.first != range.second) {&lt;br /&gt;
                          const location&amp;amp; dst = range.first-&amp;gt;first;&lt;br /&gt;
                          const location&amp;amp; src = range.first-&amp;gt;second;&lt;br /&gt;
&lt;br /&gt;
Now let's find out about the unit that we're planning to send to this destination:&lt;br /&gt;
&lt;br /&gt;
                          const unit_map::const_iterator un = get_info().units.find(src);&lt;br /&gt;
                          assert(un != get_info().units.end());&lt;br /&gt;
&lt;br /&gt;
We can assume that the unit is in that location (hence the assert), because ''calculate_possible_moves()'' said that it's the possible source of a move.  Let's find out the type of terrain we're planning to move to:&lt;br /&gt;
&lt;br /&gt;
                          const gamemap::TERRAIN terrain = get_info().map.get_terrain(dst);&lt;br /&gt;
&lt;br /&gt;
Okay, so we have the unit, and we have the terrain, now we should be able to find out the unit's defensive rating on this terrain.&lt;br /&gt;
The ''unit'' class has a convenient ''defense_modifier()'' function which will tell us the chance of hitting that unit on a certain terrain.&lt;br /&gt;
&lt;br /&gt;
                          const int chance_to_hit = un-&amp;gt;second.defense_modifier(get_info().map,terrain);&lt;br /&gt;
&lt;br /&gt;
So, now we have all that, if it's the best chance to hit we've seen so far, or we haven't seen any other chances to hit at all, then we add it as our best option seen.&lt;br /&gt;
&lt;br /&gt;
                          if(best_defense == -1 || chance_to_hit &amp;lt; best_defense) {&lt;br /&gt;
                              best_defense = chance_to_hit;&lt;br /&gt;
                              best_movement = *range.first;&lt;br /&gt;
                          }&lt;br /&gt;
&lt;br /&gt;
That's it for these two loops. Iterate and close:&lt;br /&gt;
&lt;br /&gt;
                          ++range.first;&lt;br /&gt;
                      }&lt;br /&gt;
                  }&lt;br /&gt;
&lt;br /&gt;
Now if we found a possible move, best_defense will not be -1,&lt;br /&gt;
and the movement will be stored in ''best_movement''.  So, if ''best_defense'' is not -1, we want to move from ''best_movement.second'' to ''best_movement.first''.&lt;br /&gt;
&lt;br /&gt;
                  if(best_defense != -1) {&lt;br /&gt;
                      move_unit(best_movement.second,best_movement.first,possible_moves);&lt;br /&gt;
&lt;br /&gt;
Remember that ''possible_moves'' thing?  That comes in useful here, where we have to give it to the display object so it can know the path to move the unit along.  This is the only time we need to touch it.&lt;br /&gt;
&lt;br /&gt;
Immediately after moving, we want to attack.  First we need to know which weapon to use.  We'll write a ''choose_weapon()''&lt;br /&gt;
function later which will choose our weapon.  It'll have to take the location of the attacker and the location of the defender, and it'll return an int referring to our weapon of choice.  For now we'll just make use of this function:&lt;br /&gt;
&lt;br /&gt;
                      const int weapon = choose_weapon(best_movement.first,i-&amp;gt;first);&lt;br /&gt;
                      attack_enemy(best_movement.first,i-&amp;gt;first,weapon);&lt;br /&gt;
&lt;br /&gt;
This will implement our attack.  What now?  We've attacked once, but we want to attack with as many units as we can attack with, right?  We can't use the same move_maps again, because they'll be invalid now that we've moved and attacked. What we'll do is we'll call ''do_attacks()'' all over again, recursively, and return immediately.  This way all our maps will be recalculated.&lt;br /&gt;
&lt;br /&gt;
                       do_attacks();&lt;br /&gt;
                       return;&lt;br /&gt;
                   }&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
That's the entire function done.  It'll keep attacking while it finds attacks, and when it finally runs out of attacks to execute, it'll return nicely. Let's write that ''choose_weapon()'' function now:&lt;br /&gt;
&lt;br /&gt;
  int choose_weapon(const location&amp;amp; attacker, const location&amp;amp; defender) {&lt;br /&gt;
      const unit_map::const_iterator att = get_info().units.find(attacker);&lt;br /&gt;
      assert(att != get_info().units.end());&lt;br /&gt;
&lt;br /&gt;
      const std::vector&amp;lt;a ttack_type&amp;gt;&amp;amp; attacks = att-&amp;gt;second.attacks();&lt;br /&gt;
&lt;br /&gt;
unit contains a convenient ''attacks()'' function which returns a vector of all a unit's possible attacks.  We'll store the&lt;br /&gt;
best attack found so far, and iterate over all attacks:&lt;br /&gt;
&lt;br /&gt;
      int best_attack_rating = -1;&lt;br /&gt;
      int best_attack = -1;&lt;br /&gt;
      for(int n = 0; n != attacks.size(); ++n) {&lt;br /&gt;
&lt;br /&gt;
There is a nice function called ''evaluate_battle_stats()'' in '''actions.hpp''' which will give us all sorts of information about a potential battle. We make use of it here:&lt;br /&gt;
&lt;br /&gt;
          const battle_stats stats = evaluate_battle_stats(get_info().map,&lt;br /&gt;
                attacker, defender, n, get_info().units,&lt;br /&gt;
                get_info().state, get_info().gameinfo, 0, false);&lt;br /&gt;
&lt;br /&gt;
A rather complicated function call, but most of the parameters can be pulled straight from ''get_info()''.  The last two parameters are a little confusing.  The first one of these, ''attacker_terrain_override'', is used if we wanted to know what the combat would look like if the attacker was on different terrain to what it is on now.  If this is non-0, the function will assume the attacker is on the type of terrain given.  This is useful if you want to test the possibility of moving to many different hexes without actually moving there.  The last parameter is false, meaning that strings won't be included in the results.  Strings are useful for showing to a player in a dialog,&lt;br /&gt;
but not useful for an AI, and are expensive to calculate, so this should always be false from within AI algorithms.&lt;br /&gt;
&lt;br /&gt;
Let's use our stats to come up with a rating for this attack:&lt;br /&gt;
&lt;br /&gt;
           const int attack_rating = stats.damage_defender_takes*stats.nattacks*stats.chance_to_hit_defender;&lt;br /&gt;
&lt;br /&gt;
Now if this is the best attack, we can use it,&lt;br /&gt;
&lt;br /&gt;
           if(best_attack == -1 || attack_rating &amp;gt; best_attack_rating) {&lt;br /&gt;
               best_attack = n;&lt;br /&gt;
               best_attack_rating = attack_rating;&lt;br /&gt;
           }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
       return best_attack;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Now we're done with that, we can move onto our ''get_villages()'' function.  We start off by calculating possible moves,&lt;br /&gt;
&lt;br /&gt;
  void get_villages() {&lt;br /&gt;
      std::map&amp;lt;location,paths&amp;gt; possible_moves;&lt;br /&gt;
      move_map srcdst, dstsrc;&lt;br /&gt;
      calculate_possible_moves(possible_moves,srcdst,dstsrc,false);&lt;br /&gt;
&lt;br /&gt;
Now it's a simple matter of iterating over possible destinations,&lt;br /&gt;
and seeing if they are villages not controlled by us:&lt;br /&gt;
&lt;br /&gt;
      for(move_map::const_iterator i = dstsrc.begin(); i != dstsrc.end(); ++i) {&lt;br /&gt;
          if(get_info().map.is_village(i-&amp;gt;first) &amp;amp;&amp;amp;&lt;br /&gt;
              current_team().owns_village(i-&amp;gt;first) == false) {&lt;br /&gt;
&lt;br /&gt;
First it checks whether the destination is a village.  The right side of the ''&amp;amp;&amp;amp;'' simply sees if our team owns the village at that location or not. If we don't own the village, we've found the movement we want to make, and we recurse and return.&lt;br /&gt;
&lt;br /&gt;
              move_unit(i-&amp;gt;second,i-&amp;gt;first,possible_moves);&lt;br /&gt;
              get_villages();&lt;br /&gt;
              return;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Just a couple more functions now.  Firstly, ''do_moves()'' is meant to move our units toward the enemy leader. Well, there may be multiple enemies and thus more than one leader, so we'll just go for the first enemy leader we can find.  We start off by trying to find the enemy leader:&lt;br /&gt;
&lt;br /&gt;
  void move_units() {&lt;br /&gt;
      unit_map::const_iterator leader;&lt;br /&gt;
      for(leader = get_info().units.begin(); leader != get_info().units.end(); ++leader) {&lt;br /&gt;
&lt;br /&gt;
A unit is a leader if it can recruit -- so we use the ''can_recruit()'' function to test if it's a leader.&lt;br /&gt;
&lt;br /&gt;
          if(leader-&amp;gt;second.can_recruit() &amp;amp;&amp;amp; current_team().is_enemy(leader-&amp;gt;second.side())) {&lt;br /&gt;
              break;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
We better have found an enemy leader, otherwise we'll just return...&lt;br /&gt;
&lt;br /&gt;
      if(leader == get_info().units.end())&lt;br /&gt;
          return;&lt;br /&gt;
&lt;br /&gt;
Now, let's find all our unit's possible moves:&lt;br /&gt;
&lt;br /&gt;
      std::map&amp;lt;location,paths&amp;gt; possible_moves;&lt;br /&gt;
      move_map srcdst, dstsrc;&lt;br /&gt;
      calculate_possible_moves(possible_moves,srcdst,dstsrc,false);&lt;br /&gt;
&lt;br /&gt;
We want to find the move that'll take us as close as possible to the enemy leader.  Let's make our variables to show us the best move so far,&lt;br /&gt;
&lt;br /&gt;
      int closest_distance = -1;&lt;br /&gt;
      std::pair&amp;lt;location,location&amp;gt; closest_move;&lt;br /&gt;
&lt;br /&gt;
Now iterate and find the destination closest to the enemy leader:&lt;br /&gt;
&lt;br /&gt;
      for(move_map::const_iterator i = dstsrc.begin(); i != dstsrc.end(); ++i) {&lt;br /&gt;
          const int distance = distance_between(i-&amp;gt;first,leader-&amp;gt;first);&lt;br /&gt;
          if(closest_distance == -1 || distance &amp;lt; closest_distance) {&lt;br /&gt;
              closest_distance = distance;&lt;br /&gt;
              closest_move = *i;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
If ''closest_distance'' is not -1, we've found a valid move that'll take one of our units toward the enemy leader.  We can make the move and recurse&lt;br /&gt;
&lt;br /&gt;
      if(closest_distance != -1) {&lt;br /&gt;
          move_unit(closest_move.second,closest_move.first,possible_moves);&lt;br /&gt;
          do_moves();&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Okay, all our movement functions are done!  Now all we've got left is the recruitment function. We start by getting the units that we can recruit.&lt;br /&gt;
&lt;br /&gt;
  void do_recruitment() {&lt;br /&gt;
      const std::set&amp;lt;std::string&amp;gt;&amp;amp; options = current_team().recruits();&lt;br /&gt;
&lt;br /&gt;
We can choose the number of a unit to recruit at random:&lt;br /&gt;
&lt;br /&gt;
      const int choice = (rand()%options.size());&lt;br /&gt;
      std::set&amp;lt;std::string&amp;gt;::const_iterator i = options.begin();&lt;br /&gt;
      std::advance(i,choice);&lt;br /&gt;
      const bool res = recruit(*i);&lt;br /&gt;
&lt;br /&gt;
And if the recruitment succeeds, we will try to recruit another unit,&lt;br /&gt;
&lt;br /&gt;
      if(res) {&lt;br /&gt;
          do_recruitment();&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
That's it! We've made our ''sample_ai''.  All we have to do is add it to ''create_ai'' in '''ai.cpp''' and we're done!&lt;br /&gt;
&lt;br /&gt;
== AI - specific parameters ==&lt;br /&gt;
&lt;br /&gt;
 wesnoth --multiplayer --controller1=ai --controller2=ai --algorithm1=z_ai --algorithm2=sample_ai&lt;br /&gt;
&lt;br /&gt;
Use the ''--nogui'' switch before ''--multiplayer'' to make the game run without displaying a GUI.  The winner will be reported on stdout.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
* [[DeveloperResources]]&lt;br /&gt;
* [[PythonTestScript]] - Simple Python AI test script&lt;br /&gt;
&lt;br /&gt;
[[Category:Create]]&lt;br /&gt;
[[Category:AI]]&lt;/div&gt;</summary>
		<author><name>KevinXi</name></author>
		
	</entry>
</feed>