sobota 27. října 2018
How to win on deity or sid in Civilization 3
Generally, you want to finish the game as fast as possible, otherwise enemies are going to overbuild/overreseach you. In order to be able to finish the map quickly, pick a small map. In order to be able to quickly get to the enemy, pick a single continent with a minimum of water mass. Set a wining condition to elimination - with this setting, it is enough to conquer a single opponent's city in order to completely eliminate the opponent. With this setting, the opponent’s biggest advantage - many settlers from the beginning, is turned into a weakness. All you need is 2 unit that are capable of overcoming 2 spearmen in a weakly defended city, and the opponent is gone. In order to maximize your advantage, pick a nation with an early unique unit. And set your opponents to nations with late unique unite. The selection of your nation depends on the game rules of your Civ mod/patch. For example, in RARR, Aztecs are awesome. Their unique unit costs the same as a warrior. But it has a bonus attack point. It has a bonus movement point. It is not penalized for movement in jungle and forest (set the map to wet climate in order to maximize this benefit). It has a bonus hit point and it can enslave. And of course it helps that you can build the unique unit from the beginning without any resource dependency and that it triggers early gold age (remember, you want to finish the game as fast as possible). If you can pick your starting position, it is good to start next to a bonus resource like game in order to maximize the population growth and unique unit production. If everything goes well, you should easily win in the first 30 turns by conquest.
úterý 31. července 2018
Disadvantages of AUC and the fixes
Area Under the Curve for Receiver Operating Characteristics (further referred as AUC) is a nice measure, but of course, it has some potential theoretical disadvantages:
- It takes into account all possible decision thresholds.
- It assumes uniform misclassification cost.
- It does not measure calibration of the predictions.
Consequences:
- We are generally interested into decision thresholds close to the true ratio of positive samples to negative samples. For example, whenever we have an extreme class ratio, like in information retrieval where we have only a few positive samples but many negative samples, we are much more likely to be interested into observing the few predicted positives than into observing a few almost certain negatives (or complementary, to seeing all but a few almost certain negatives), simply because positives are rare while there is an abundance of negative samples (if you randomly pick a few samples, you are almost guaranteed that they all will be negative). But AUC puts equal weight to all thresholds. Consequently, in the case of severe class imbalance, it is easy to find examples, where higher AUC corresponds to worse performance in the real world - a model with modest AUC but stellar performance in the working region can beat a model that has an awesome AUC but underperforms in the working region.
- The sum of the cost of all positives samples is generally similar to the sum of the cost of all negative samples. In the case of severe class imbalance, we generally put much more weight on the rare class samples than on the common samples. I.e.: it is more important for us that a positive sample is classified as positive than when a negative sample is classified as negative. However, AUC puts equal weight on both classes. Consequently, if we can correctly predict a large portion of negative samples as negative (as is common in information retrieval), it is easy to get AUC close to 1.0 as demonstrated in this example.
- Whenever we need calibrated probabilities as the output of a classifier (for example for risk assessment), AUC is not really informative. But in defense of AUC, if the business application asks for ranking and not for calibrated probabilities, we want to optimize ranking and not the quality of calibration because we can come with two models: one with great ranking but terrible calibration (e.g.: because all the probabilities are squeezed to the decision boundary like in SVM) and another model with great calibration but terrible ranking (e.g.: decision trees create plateaus of probabilities that hurt their ranking scores).
Practical complications:
- When AUC was defined, it was not said how to handle ties. Consequently, everyone implements it differently and the reported values differ.
- It was (originally) defined only for binary classification. Consequently, there are multiple non-comparable proposals for extension into multiclass situations.
- All ranking measures that take into consideration all the predictions (like AUC does) are slow when you have a lot of samples (> 10M).
- Use AUC with Beta prior on the threshold location: H measure
- Use sample-weighted AUC
- Use Brier score
- Test the implementation against reasonably looking unit-tests . A fast and concise implementation that passes the tests is at MATLAB Central.
- Use M measure
- Both, thresholding and calibration measures scale linearly with the count of samples. Use them. Alternatively, you may calculate a ranking measure on a specific subset of data. An example is NDCG@k, which you may get in O(n + k log k) time, where k is the top k samples and n is the count of samples.
pondělí 16. července 2018
Impression of R
Following text is structured as a sonnet: thesis, antithesis and synthesis.
The philosophy of R is beautiful. Everything is a function. Hence the syntax highly regular (predictable). And R contains the common syntax sugar that you would expect from a functional language like named and default function parameters. This is a great improvement in comparison to Matlab, where you have to fake this functionality. Also, R always passes by value. Hence, it behaves more predictably than Java, which passes primitives by value and objects, effectively, by reference (Java passes a reference to the object by value). But R utilizes copy-on-write, which helps to avoid unnecessary copying of data.
But there are also disadvantages. In R, you can do the same thing many ways. And that can quickly become overwhelming. You search for a way how to do X. And the search returns ten different ways how to do it. Which of the methods is the best? You don't know. So you test the first one. It works. But it does not allow you to do Y, which is closely related to X. So you test the second approach. It works - it allows you to do both, X and Y. But it is too slow. So you test the third approach. There is a bug, which does not allow you to do X. You test the fourth approach. It does not work at all... I think you got the idea. Finding a way how to do something in R is much more consuming than in Python, where is commonly only a single dominant way how to do something. R community realizes that this is a problem. CRAN introduced a possibility to asses packages by stars. But it got phased out because only a few people were contributing their evaluation. Another disadvantage of R is that when you look at the source code of a function, you newer know ahead, what will you find. Is it function written in R, S or C? Sometimes it really fells like unwrapping a Christmas present - at the beginning you only see the function API. You unwrap it just to find out there is a thin R wrapper inside. So you remove another layer. It is in S. But not pure S - it is FORTRAN code ported into S. So you look into the FORTRAN code. But the FORTRAN code is not a typical FORTRAN code either - it was already ported from something else... I have to admit that I am impressed that so many levels of indirection actually works. But debugging and understanding such code is a nightmare.
My conclusion: R is awesome, if all you need is to use it as script language. Whenever I need to run some exotic statistical method, I can be pretty confident that it was ported to or written in R and distributed as a package. There are situations when I simply do not have choice but to use R unless I want to implement the method from scratch. But whenever I have to read R code written by my colleagues, it is a hell - each of my colleagues is using different set of packages, different objects (data frame vs. data table vs. tibble vs. ...), different approach to writing loops (for loop vs. apply vs. tidyverse vs. ...). In the end, it is not difficult to understand the code. But it fells like reading text riddled with typos - you understand it. But it takes mental capacity from the content to the representation layer.
The philosophy of R is beautiful. Everything is a function. Hence the syntax highly regular (predictable). And R contains the common syntax sugar that you would expect from a functional language like named and default function parameters. This is a great improvement in comparison to Matlab, where you have to fake this functionality. Also, R always passes by value. Hence, it behaves more predictably than Java, which passes primitives by value and objects, effectively, by reference (Java passes a reference to the object by value). But R utilizes copy-on-write, which helps to avoid unnecessary copying of data.
But there are also disadvantages. In R, you can do the same thing many ways. And that can quickly become overwhelming. You search for a way how to do X. And the search returns ten different ways how to do it. Which of the methods is the best? You don't know. So you test the first one. It works. But it does not allow you to do Y, which is closely related to X. So you test the second approach. It works - it allows you to do both, X and Y. But it is too slow. So you test the third approach. There is a bug, which does not allow you to do X. You test the fourth approach. It does not work at all... I think you got the idea. Finding a way how to do something in R is much more consuming than in Python, where is commonly only a single dominant way how to do something. R community realizes that this is a problem. CRAN introduced a possibility to asses packages by stars. But it got phased out because only a few people were contributing their evaluation. Another disadvantage of R is that when you look at the source code of a function, you newer know ahead, what will you find. Is it function written in R, S or C? Sometimes it really fells like unwrapping a Christmas present - at the beginning you only see the function API. You unwrap it just to find out there is a thin R wrapper inside. So you remove another layer. It is in S. But not pure S - it is FORTRAN code ported into S. So you look into the FORTRAN code. But the FORTRAN code is not a typical FORTRAN code either - it was already ported from something else... I have to admit that I am impressed that so many levels of indirection actually works. But debugging and understanding such code is a nightmare.
My conclusion: R is awesome, if all you need is to use it as script language. Whenever I need to run some exotic statistical method, I can be pretty confident that it was ported to or written in R and distributed as a package. There are situations when I simply do not have choice but to use R unless I want to implement the method from scratch. But whenever I have to read R code written by my colleagues, it is a hell - each of my colleagues is using different set of packages, different objects (data frame vs. data table vs. tibble vs. ...), different approach to writing loops (for loop vs. apply vs. tidyverse vs. ...). In the end, it is not difficult to understand the code. But it fells like reading text riddled with typos - you understand it. But it takes mental capacity from the content to the representation layer.
pondělí 23. dubna 2018
Bugs in code
It is said that there are just two hard problems in computer science: cache invalidation, naming things, and off-by-1 errors. Nevertheless, the most memorable bugs for me are race-conditions and numerical stability problems.
Race-conditions are memorable, because they are difficult to reproduce. While numerical stability related issues are memorable, because it is difficult to fix them correctly.
Race-conditions are memorable, because they are difficult to reproduce. While numerical stability related issues are memorable, because it is difficult to fix them correctly.
pondělí 26. března 2018
Type systems in programming languages
There are four approaches to data types:
But once you have many data types, you frequently find yourself in a need to write the same function but for multiple data types. An illustrative example would be a sum function that should be able to work on integer vector and floating point vector. But it can quickly become tiresome to maintain multiple copies of the same code. Languages with the rich data type system have to provide some way how to avoid plain duplication of the code. Haskell solves it by providing data types in a hierarchy - if you need to write a function that works on numbers, just write it for the common ancestor of all numbers.
The hierarchical type system is, however, quite restrictive. If there are types that fulfill requirement α and some types fulfill requirement β, then we can represent such types in a Venn diagram:

But if all 3 sections, A, B and A∩B are non-empty, we cannot say that α⊆β or β⊆α. In other words, with a hierarchical system we cannot use both, α and β characteristics, to describe the types. We can, at best, just pick one of these characteristics to describe the data types.
A concrete example of this dilemma are integers in databases. Should we treat them as additive or as "groupable"? Integers are both. But continuous numbers are, arguably, just additive. While characters are, arguably, only groupable. In a hierarchical system, we generally take the smaller of the evils and accept that we can test exact equality even on floating point numbers. Union type systems (systems that rely on union/sum types) and trait type systems (as in protocols in Swift or traits in Python or intersection types in algebraic data types) allow you to tackle this dilemma. Btw., if you are using tags to organize your bookmarks, files or photos, you are already using a trait system. A nice critique of hierarchical systems is given in Goodbye, Object Oriented Programming.
What is the difference between the union type system and the trait type system? Union type systems assume that we have a fixed set of types (e.g.: float, double, integer, ...). And each variable (e.g.: a function argument) specifies a set of data types that it accepts. On the other end, trait type systems assume that we have a fixed set of traits (e.g.: comparable, ordinal, additive, ...) and each variable defines traits that the data type must fulfill.
- Have a very few basic data structures. This is the philosophy of Scheme.
- Have a rich type system that is organized into a hierarchy. Haskel is famous for this approach.
- Have a rich type system that is using union types. This is the Ceylon way.
- Have a rich type system with traits. This approach is taken by Rust, Swift, Python and Go.
But once you have many data types, you frequently find yourself in a need to write the same function but for multiple data types. An illustrative example would be a sum function that should be able to work on integer vector and floating point vector. But it can quickly become tiresome to maintain multiple copies of the same code. Languages with the rich data type system have to provide some way how to avoid plain duplication of the code. Haskell solves it by providing data types in a hierarchy - if you need to write a function that works on numbers, just write it for the common ancestor of all numbers.
The hierarchical type system is, however, quite restrictive. If there are types that fulfill requirement α and some types fulfill requirement β, then we can represent such types in a Venn diagram:

But if all 3 sections, A, B and A∩B are non-empty, we cannot say that α⊆β or β⊆α. In other words, with a hierarchical system we cannot use both, α and β characteristics, to describe the types. We can, at best, just pick one of these characteristics to describe the data types.
A concrete example of this dilemma are integers in databases. Should we treat them as additive or as "groupable"? Integers are both. But continuous numbers are, arguably, just additive. While characters are, arguably, only groupable. In a hierarchical system, we generally take the smaller of the evils and accept that we can test exact equality even on floating point numbers. Union type systems (systems that rely on union/sum types) and trait type systems (as in protocols in Swift or traits in Python or intersection types in algebraic data types) allow you to tackle this dilemma. Btw., if you are using tags to organize your bookmarks, files or photos, you are already using a trait system. A nice critique of hierarchical systems is given in Goodbye, Object Oriented Programming.
What is the difference between the union type system and the trait type system? Union type systems assume that we have a fixed set of types (e.g.: float, double, integer, ...). And each variable (e.g.: a function argument) specifies a set of data types that it accepts. On the other end, trait type systems assume that we have a fixed set of traits (e.g.: comparable, ordinal, additive, ...) and each variable defines traits that the data type must fulfill.
How to return multiple values from a function
There are three approaches how to return multiple values from a function:
Matlab's approach is beautiful, till you have to return up to 4 variables. Bu it does not scale to higher count of return values, because you have to address the output values by position - addressing by name does not work. And just a mental picture of me trying to read the 100th return value by writing the correct amount of commas and getting it wrong by one comma is painful.
Prolog's approach requires convention to differentiate between input parameters and output attributes.
Hence, the nicest approach, in my opinion, is to return "tuple", which can be addressed by both, position and name. This is the approach taken by R.
- Return some object. This is the canonical solution in objective and functional languages (which return "tuples").
- Use function parameters for the output. This is the canonical solution in Prolog.
- Allow functions to return multiple values. This is the canonical solution in Matlab.
Matlab's approach is beautiful, till you have to return up to 4 variables. Bu it does not scale to higher count of return values, because you have to address the output values by position - addressing by name does not work. And just a mental picture of me trying to read the 100th return value by writing the correct amount of commas and getting it wrong by one comma is painful.
Prolog's approach requires convention to differentiate between input parameters and output attributes.
Hence, the nicest approach, in my opinion, is to return "tuple", which can be addressed by both, position and name. This is the approach taken by R.
pondělí 19. března 2018
Dream castle in Heroes III by unit levels
1) Skeletons
While an average level 1 unit, the necromancy skill makes the skeletons a great choice for large maps with a lot of wandering creatures, which can be harvested.
Disadvantage: On tiny maps occupied only by castles, where the majority of the battles happen during the first two weeks, updated gremlins or sprites would be a better choice since they are better suited for castle conquests during the first weeks than skeletons.
2) Harpy
Updated harpies are masters of guerrilla attacks as the enemy does not retaliate and the harpies return to their original position unharmed. If combined with mass slow, melee units can have a tough time to take down the harpies. If you dislike loosing units while cleaning the map, this is the creature for you. It was voted to be the second most favourite level 2 unit.
Disadvantage: While harpies are awesome units for map cleaning and castle defense against weak opponents because of their low attrition, harpies are lousy units for final battles where big unit losses have to be expected. If you expect early battles between the main heroes in the open field, wolfs with their impressive damage ability would be a better choice.
3) Elves
Each castle should have at least one shooting unit. Upgraded elves shoot twice during the turn, making their upgraded version highly desirable. Elves were voted to be the favourite level 3 unit.
4) Vampires
Once the count of upgraded vampires reaches ~20 units, they are awesome beasts for map cleaning as you may expect minimal losses due to their ability to resurrect from the flesh of their enemies. Rated the most favourite level 4 unit.
Disadvantage: Just like harpies, vampires are not the best units for the final battles. Furthermore, it takes two weeks to grow the necessary number of units from a single castle.
5) Pits
Upgraded pits can (or could, if they worked also on undeads) raise ridiculously expensive vampires in vast quantities from the slaughtered skeletons. Finally, you do not have to worry about skeleton losses during map cleaning as the lost skeletons get converted into vampires!
Disadvantage: Just like harpies, elves and vampires, this unit has to be upgraded to benefit from them fully.
6) Wyvern
To compensate the need to upgrade almost all lower level units, wyverns do not have to be upgraded to be of some use. Furthermore, wyverns' dwelling has very low prerequisite requirements (just a level 2 dwelling building), making it possible to recruit the wyverns during on the first day. Since they are also fliers, they can be of great use for early castle sieging.
Disadvantage: Champions, black knights and nagas are all beautiful creatures. But no other level 6 dwelling can be build on day 1. If we went with any other unit, we would not be able to recruit level 7 units on day 2 of the first week.
7) Firebird
While firebird is not the best level 7 unit, the fact that the dwelling is one of the cheapest (the second behind titan's), the unit itself is inexpensive, the weekly growth is above average, it is one of the fastest units in the game giving you the opportunity to apply mass haste/slow and decimate the opponent before he gets to the turn, and fire immunity allowing you to embrace Armageddon strategy (with the help of a support hero to store raised skeletons) make it a pretty decent choice. It is the only level 7 unit whose parameters were down-tuned in Horn of the Abyss. Furthermore, firebird dwelling building has only one prerequisite - level 6 dwelling building. Hence, if you play on normal difficulty, you just need to get 5 wood (or better, set the starting bonus to resources - wood and stone, and you do not have to worry about getting any additional resource or gold) and on the second day you will have 2 firebirds, 2 wyverns and 3900 gold in the pocket to spend it on whatever you want to (based on the situation you may want to keep it for capitol erection, or spend it on another hero, 8 harpies and 6 skeletons).
Discussion:
Since all the units would be from the same castle, there would not be morale penalty from mixing several units together. The castle would be usable for blitzkrieg strategy during the early phase of the game if played on normal difficulty level thanks to wyverns and firebirds. But the castle would also scale to long games due to the ability to harvest skeletons, which would be continuously converted into vampires. And because upgraded harpies and vampires simply do not die during the map cleaning, you may go into the final battles with vast counts of skeletons, harpies, and vampires. Furthermore, firebirds and Phoenixes are, contrary to many dragons, immune to Armageddon spell. And because Phoenixes are the fastest units in the game, you are almost guaranteed, that you get the opportunity to cast Armageddon before the opponent gets the opportunity to do anything. Hence, Phoenixes can be in some situations used for dragon slaying without any loss.
While an average level 1 unit, the necromancy skill makes the skeletons a great choice for large maps with a lot of wandering creatures, which can be harvested.
Disadvantage: On tiny maps occupied only by castles, where the majority of the battles happen during the first two weeks, updated gremlins or sprites would be a better choice since they are better suited for castle conquests during the first weeks than skeletons.
2) Harpy
Updated harpies are masters of guerrilla attacks as the enemy does not retaliate and the harpies return to their original position unharmed. If combined with mass slow, melee units can have a tough time to take down the harpies. If you dislike loosing units while cleaning the map, this is the creature for you. It was voted to be the second most favourite level 2 unit.
Disadvantage: While harpies are awesome units for map cleaning and castle defense against weak opponents because of their low attrition, harpies are lousy units for final battles where big unit losses have to be expected. If you expect early battles between the main heroes in the open field, wolfs with their impressive damage ability would be a better choice.
3) Elves
Each castle should have at least one shooting unit. Upgraded elves shoot twice during the turn, making their upgraded version highly desirable. Elves were voted to be the favourite level 3 unit.
4) Vampires
Once the count of upgraded vampires reaches ~20 units, they are awesome beasts for map cleaning as you may expect minimal losses due to their ability to resurrect from the flesh of their enemies. Rated the most favourite level 4 unit.
Disadvantage: Just like harpies, vampires are not the best units for the final battles. Furthermore, it takes two weeks to grow the necessary number of units from a single castle.
5) Pits
Upgraded pits can (or could, if they worked also on undeads) raise ridiculously expensive vampires in vast quantities from the slaughtered skeletons. Finally, you do not have to worry about skeleton losses during map cleaning as the lost skeletons get converted into vampires!
Disadvantage: Just like harpies, elves and vampires, this unit has to be upgraded to benefit from them fully.
6) Wyvern
To compensate the need to upgrade almost all lower level units, wyverns do not have to be upgraded to be of some use. Furthermore, wyverns' dwelling has very low prerequisite requirements (just a level 2 dwelling building), making it possible to recruit the wyverns during on the first day. Since they are also fliers, they can be of great use for early castle sieging.
Disadvantage: Champions, black knights and nagas are all beautiful creatures. But no other level 6 dwelling can be build on day 1. If we went with any other unit, we would not be able to recruit level 7 units on day 2 of the first week.
7) Firebird
While firebird is not the best level 7 unit, the fact that the dwelling is one of the cheapest (the second behind titan's), the unit itself is inexpensive, the weekly growth is above average, it is one of the fastest units in the game giving you the opportunity to apply mass haste/slow and decimate the opponent before he gets to the turn, and fire immunity allowing you to embrace Armageddon strategy (with the help of a support hero to store raised skeletons) make it a pretty decent choice. It is the only level 7 unit whose parameters were down-tuned in Horn of the Abyss. Furthermore, firebird dwelling building has only one prerequisite - level 6 dwelling building. Hence, if you play on normal difficulty, you just need to get 5 wood (or better, set the starting bonus to resources - wood and stone, and you do not have to worry about getting any additional resource or gold) and on the second day you will have 2 firebirds, 2 wyverns and 3900 gold in the pocket to spend it on whatever you want to (based on the situation you may want to keep it for capitol erection, or spend it on another hero, 8 harpies and 6 skeletons).
Discussion:
Since all the units would be from the same castle, there would not be morale penalty from mixing several units together. The castle would be usable for blitzkrieg strategy during the early phase of the game if played on normal difficulty level thanks to wyverns and firebirds. But the castle would also scale to long games due to the ability to harvest skeletons, which would be continuously converted into vampires. And because upgraded harpies and vampires simply do not die during the map cleaning, you may go into the final battles with vast counts of skeletons, harpies, and vampires. Furthermore, firebirds and Phoenixes are, contrary to many dragons, immune to Armageddon spell. And because Phoenixes are the fastest units in the game, you are almost guaranteed, that you get the opportunity to cast Armageddon before the opponent gets the opportunity to do anything. Hence, Phoenixes can be in some situations used for dragon slaying without any loss.
Přihlásit se k odběru:
Příspěvky (Atom)