text:
# Revealed preference

Revealed preference theory, pioneered by American economist Paul Samuelson,[1] is a method of analyzing choices made by individuals, mostly used for comparing the influence of policies on consumer behavior. These models assume that the preferences of consumers can be revealed by their purchasing habits. Revealed preference theory came about because existing theories of consumer demand were based on a diminishing marginal rate of substitution (MRS). This diminishing MRS relied on the assumption that consumers make consumption decisions to maximize their utility. While utility maximization was not a controversial assumption, the underlying utility functions could not be measured with great certainty. Revealed preference theory was a means to reconcile demand theory by defining utility functions by observing behavior.

## Definition and Theory

If bundle a is revealed preferred over bundle b in budget set B, then the WARP says that bundle b can not be strictly revealed preferred over bundle a in any budget set B'. This would be equally true had b been located anywhere else in the pink area. The bundle c never violates WARP because it is not in the pink area.

Let there be two bundles of goods; a and b available in a budget set $B$. If it is observed that a is chosen over b, we say that a is (directly) revealed preferred to b.

### Two-Dimensional Example

If the budget set $B$ is defined for two goods; $X = X_{1},X_{2}$, and determined by prices $p_{1},p_{2}$ and income $m$, then let bundle a be $(x_{1},x_{2}) \in X$ and bundle b be $(y_{1},y_{2}) \in X$. This situation would typically be represented arithmetically by the inequality $p_{1}X_{1} + p_{2}X_{2} \leq m$ and graphically by a budget line in the positive real numbers. Assuming strongly monotonic preferences, we only need to consider bundles that graphically are located on the budget line, i.e. bundles where $p_{1}x_{1} + p_{2}x_{2} = m$ and $p_{1}y_{1} + p_{2}y_{2} = m$ are satisfied. If, in this situation, it is observed that $(x_{1},x_{2})$ is chosen over $(y_{1},y_{2})$, we conclude that $(x_{1},x_{2})$ is (directly) revealed preferred to $(y_{1},y_{2})$, which can be summarized as the binary relation $(x_{1},x_{2}) \succeq (y_{1},y_{2})$ or equivalently as $\mathbf{a} \succeq \mathbf{b}$.[2]

### The Weak Axiom of Revealed Preference (WARP)

WARP is one of the criteria which need to be satisfied in order to make sure that the consumer is consistent with his preferences. If a bundle of goods a is chosen over another bundle b when both are affordable, then the consumer reveals that he prefers a over b. WARP says that when preferences remain the same, there are no circumstances (budget set) where the consumer strictly prefers b over a. By choosing a over b when both bundles are affordable, the consumer reveals that his preferences are such that he will never choose b over a, while prices remain constant. Formally,

$\left.\begin{matrix} x,y \in B\\ x \in C(B, \succeq) \\ x,y \in B' \\ y \in C(B', \succeq) \end{matrix}\right\} ~\Rightarrow~ x \in C(B', \succeq)$

where $x$ and $y$ are arbitrary bundles and $C (B, \succeq) \subset B$ is the set of bundles chosen in budget set $B$, given preference relation $\succeq$.

Alternatively, if a is chosen over b in budget set $B$ where both a and b are feasible bundles, but b is chosen over a when the consumer faces some other budget set $B'$, then a is not a feasible bundle in budget set $B'$. This equivalent statement of WARP can formally and more generally be expressed as

$p \cdot x(p',m') \leq m ~ \wedge ~ x(p',m') \neq x(p,m) ~\Rightarrow ~ p' \cdot x(p,m) > m'~$.

To show formally that WARP holds, assume complete and transitive preferences, or in other words, a rational preference relation $\succeq$. Let $C ( \cdot )$ be a choice rule that assigns a nonempty set of elements $C(B) \subset B$ for every budget set $B$. Define a specific choice structure

$C'(B,\succeq) ~=~ \left \{ x \in B : x \succeq y ~ \forall y \in B \right \}$ ,

i.e. so that one of the most preferred bundles in the budget set is always chosen. Now suppose that for some $B$, we have $x,y \in B$ and $x \in C'(B,\succeq)$, which by definition implies $x \succeq y$ . Also consider some other budget set $B'$ with $x,y \in B'$. If $x \in C'(B',\succeq)$, the proof is done. If $y \in C'(B',\succeq)$, which by completeness must be true for some $y$, then $y \succeq z ~\forall z \in B'$. Since $x \succeq y$ and preferences are transitive, $x \succeq z ~\forall z \in B'$ follows. The specified choice structure then implies that $x \in C'(B',\succeq)$, completing the proof.

Thus, despite being referred to as an axiom, WARP is not necessarily assumed, but follows readily from rational preferences.[3]

### Completeness and Strong axiom

The strong axiom of revealed preferences (SARP) is equivalent to the weak axiom of revealed preferences, except that the consumer is not allowed to be indifferent between the two bundles that are compared. That is, if WARP concludes $\mathbf{a} \succeq \mathbf{b}$, SARP goes a step further and concludes $\mathbf{a} \succ \mathbf{b}~$ .

If A is directly revealed preferred to B, and B is directly revealed preferred to C, then we say A is indirectly revealed preferred to C. It is possible for A and C to be (directly or indirectly) revealed preferable to each other at the same time, creating a "loop". In mathematical terminology, this says that transitivity is violated.

Consider the following choices: $C(A,B)=A$ , $C(B,C)=B$ , $C(C,A)=C$, where $C$ is the choice function taking a set of options (budget set) to a choice. Then by our definition A is (indirectly) revealed preferred to C (by the first two choices) and C is (directly) revealed preferred to A (by the last choice).

It is often desirable in economic models to prevent such loops from happening, for example if we wish to model choices with utility functions (which have real-valued outputs and are thus transitive). One way to do so is to impose completeness on the revealed preference relation with regards to the situations, i.e. every possible situation must be taken into consideration by a consumer. This is useful because if we can consider {A,B,C} as a situation, we can directly tell which option is preferred to the other (or if they are the same). Using the weak axiom then prevents two choices from being preferred over each other at the same time; thus it would be impossible for "loops" to form.

Another way to solve this is to impose the strong axiom of revealed preference (SARP) which ensures transitivity. This is characterized by taking the transitive closure of direct revealed preferences and require that it is antisymmetric, i.e. if A is revealed preferred to B (directly or indirectly), then B is not revealed preferred to A (directly or indirectly).

These are two different approaches to solving the issue; completeness is concerned with the input (domain) of the choice functions; while the strong axiom imposes conditions on the output.

## Motivation

Revealed preference theory tries to understand the preferences of a consumer among bundles of goods, given their budget constraint. For instance, if the consumer buys bundle of goods A over bundle of goods B, where both bundles of goods are affordable, it is revealed that he/she directly prefers A over B. It is assumed that the consumer's preferences are stable over the observed time period, i.e. the consumer will not reverse their relative preferences regarding A and B.

As a concrete example, if a person chooses 2 apples/3 bananas over an affordable alternative 3 apples/2 bananas, then we say that the first bundle is revealed preferred to the second. It is assumed that the first bundle of goods is always preferred to the second, and that the consumer purchases the second bundle of goods only if the first bundle becomes unaffordable.

## Criticism

Stanley Wong argued that revealed preference theory was a failed research program.[4] According to Wong, in 1938 Samuelson presented revealed preference theory as an alternative to utility theory,[1] while in 1950, Samuelson took the demonstrated equivalence of the two theories as a vindication for his position, rather than as a refutation.

If there exist only an apple and an orange, and an orange is picked, then one can definitely say that an orange is revealed preferred to an apple. In the real world, when it is observed that a consumer purchased an orange, it is impossible to say what good or set of goods or behavioral options were discarded in preference of purchasing an orange. In this sense, preference is not revealed at all in the sense of ordinal utility.[5] One of the critics of the revealed preference theory states that "Instead of replacing 'metaphysical' terms such as 'desire' and 'purpose'" they "used it to legitimize them by giving them operational definitions." Thus in psychology, as in economics, the initial, quite radical operationalist ideas eventually came to serve as little more than a "reassurance fetish" for mainstream methodological practice."[6]

## Notes

1. ^ a b Samuelson, P. (1938). "A Note on the Pure Theory of Consumers' Behaviour". Economica 5 (17): 61–71. JSTOR 2548836.
2. ^ Varian, Hal R. (2006). Intermediate Microeconomics: A Modern Approach (International ed.). WW Norton & Company. ISBN 81-7671-058-X.
3. ^ Mas-Colell, A.; Whinston, M. D.; Green, J. R. (1995). Microeconomic Theory (First ed.). New York: Oxford University Press. pp. 9–12. ISBN 0-19-507340-1.
4. ^ Wong, Stanley (1978). Foundations of Paul Samuelson's Revealed Preference Theory: A Study by the Method of Rational Reconstruction. Routledge. ISBN 0-7100-8643-1.
5. ^ Koszegi, Botond; Rabin, Matthew (2007). "Mistakes in Choice-Based Welfare Analysis". American Economic Review 97 (2): 477–481. doi:10.1257/aer.97.2.477. JSTOR 30034498.
6. ^ Hands, D. Wade (2004). "On Operationalisms and Economics". Journal of Economic Issues 38 (4): 953–968. JSTOR 4228082.

## References

• Nicholson, W. (2005). Microeconomic Theory: Basic Principles and Extensions. Mason, OH: Thomson/Southwestern. ISBN 0-324-27086-0.
• Varian, Hal R. (1992). Microeconomic Analysis (Third ed.). New York: Norton. ISBN 0-393-95735-7. Section 8.7


text:
ma_terms: Calculates all mass action terms prod_{i \in N^-} S_i^|N_i| In cdiener/dycone: dycone - analyze the dynamic landscape of metabolome data

∏_{i \in N^-} S_i^|N_i|

Description

Calculates all mass action terms

∏_{i \in N^-} S_i^|N_i|

Usage

 1 ma_terms(s_matrix, concs)

Arguments

 s_matrix The stochiometric matrix. concs The concentrations of the substrates. Can be one of two A named vector containing the concentratiions for all substrates, where its names come from the same set as the rownames of S. A data frame with one column names 'name' containing the names and all other columns containing multiple concentration entries.

Value

Either vector of length n_r containing the mass-action terms, if concs was a vector, or a matrix with n_r rows containing the mass-action in its columns.

stoichiometry to calculate the stochiometric matrix.
 1 2 3 4 5 data(eryth) S <- stoichiometry(eryth) concs <- runif(nrow(S)) names(concs) <- rownames(S) mats <- ma_terms(S, concs)

format: plain


text:
# Creating table with sub-elements [closed]

I want to build a table like this:

I have tried a code but it doesn't work properly:

\begin{table}[htp]
\caption{The temporal outlier detection accuracy of hourly mean values with respect to the MFs in 2007-09-30}
\label{tab:todh30}
\centering
\footnotesize\setlength{\tabcolsep}{2.5pt}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|}
\hline
Node ID   & \multicolumn{2}{c|}{Density-based} \\ \cline{2-3} & \multicolumn{2}{c|}{Mahalanobis distance-based} \\ \cline{2-3} & \multicolumn{2}{c|}{Running average-based} \\ \cline{2-3}
& DR\%  &  FPR\% \\  \hline & DR\%  &  FPR\% \\ \hline  & DR\%  &  FPR\% \\ \hline
25   & 100    & 71      \\  \hline  & 100    & 71      \\  \hline  & 100    & 60      \\ \hline
28 & 100    & 57     \\ \hline   & 100    & 57      \\ \hline  & 66    & 60      \\ \hline
29   & 100    & 62      \\ \hline   & 100    & 57      \\  \hline  & 100    & 50      \\ \hline
31   & 100    & 57      \\  \hline & 50    & 66      \\ \hline   & 66   & 60      \\ \hline
32   & 100    & 71      \\  \hline  & 100    & 66      \\  \hline & 100    & 66      \\ \hline
\end{tabular}
\end{table}

-

## closed as too localized by doncherry, Guido, T. Verron, Stefan Kottwitz♦Feb 1 '13 at 7:04

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

It would be best if you could show what you've tried so far... have you looked at the questions tagged with table? –  cmhughes Jan 24 '13 at 23:20

I made a small example which shows the basic commands that you'll need.

Explanations

• tabular is the standard environment for tables in LaTeX that does not require extra packages
• the second argument is number and also the type of columns, in this case |c|c|c|, which means that we have 3 centered lines seperated by a vertical line (|).
• \hline inserts a line over the whole table width
• \cline inserts a line under a range of given columns, here \cline{2-3}, so we have a line under all colums from 2 to 3 (2 and 3 included)
• probably the most important: \multicolumn{2}{c|}{some text}:
• the first argument gives the number of columns over which the cell will stretch
• the second argument specifies the type of column (as before), in case your wondering why you need c| just try it without | then it should be clear
• the last argument is the text you wish to have in this particular cell
• IMPORTANT with \\ you end the line and enter a new line, so don't use it if you still want to add colums, instead of rows

Code

\documentclass{article}

\begin{document}

\begin{tabular}{|c|c|c|}
\hline
n   & \multicolumn{2}{c|}{Running average-based} \\ \cline{2-3}
& DR\%  &  FPR\% \\ \hline
6   & 19    & 1      \\ \hline
TOD & 72    & 11     \\ \hline
\end{tabular}

\end{document}

Result

Here also a improved version of your code.

\documentclass{article}

\begin{document}

\begin{table}[htp]
\caption{The temporal outlier detection accuracy of hourly mean values with respect to the MFs in 2007-09-30}
\label{tab:todh30}
\centering
\footnotesize\setlength{\tabcolsep}{2.5pt}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|}
\hline
Node ID   & \multicolumn{2}{c|}{Density-based}  & \multicolumn{2}{c|}{Mahalanobis distance-based}  & \multicolumn{2}{c|}{Running average-based} \\ \cline{2-7}

& DR\%   &  FPR\%  & DR\%   &  FPR\%  & DR\%   &  FPR\%  \\ \hline
25 & 100    & 71      & 100    & 71      & 100    & 60      \\ \hline
28 & 100    & 57      & 100    & 57      & 66     & 60      \\ \hline
29 & 100    & 62      & 100    & 57      & 100    & 50      \\ \hline
31 & 100    & 57      & 50     & 66      & 66     & 60      \\ \hline
32 & 100    & 71      & 100    & 66      & 100    & 66      \\ \hline
\end{tabular}
\end{table}

\end{document}

Resulting in

-
I have tried your code, but i had problem with it. Plz guide me on the matter. –  ali amidi Jan 25 '13 at 0:46
I added some explanations which hopefully provide guidance. –  xfoo Jan 25 '13 at 7:26
@aliamidi I also added an improved version of the code you gave in your question, now it should work as you can see in the second image. –  xfoo Jan 25 '13 at 7:46

This can be a good starting point and you can modify it based on your needs:

\documentclass{article}

\begin{document}
\begin{tabular}{|l||l|l||l|l|}
\hline
&\multicolumn{2}{l|}{A}&\multicolumn{2}{l|}{B}\\
\cline{2-5}
n&A1&\textbf{A2}&B1&\textbf{B2}\\
\hline\hline
0&3&\textbf{7}&11&\textbf{15}\\
1&4&\textbf{8}&12&\textbf{16}\\
2&5&\textbf{9}&13&\textbf{17}\\
3&6&\textbf{10}&14& \\
\hline
\end{tabular}
\end{document}

Credits to http://www.maths.tcd.ie for their template.

-


text:
Heatmap when having duplicated genes and samples
1
0
Entering edit mode
16 months ago
BioQueen ▴ 30

Hi! I have done a transcription factor inference analysis and I now want to display the result in a heatmap.

I have 3 columns: source, condition and score where the source is the gene names, the condition is the samples and the score is the enrichment score. The problem is that I have the enrichment score for each gene for each samples. Say I have the enrichment score for gene1 for sample1, gene2 for sample1 and gene3 for sample1, I also have the ES for gene1 for sample2, gene2 for sample2 and gene3 for sample2.

So my problem is that I'm not able to make the genes to rownames and the samples to colnames due to duplicates. Does anyone know how I can make a heatmap and display the enrichment score with samples on the "x-axis" and genes on the "y-axis"?

Thanks!

heatmap duplicated-genes-samples • 670 views
0
Entering edit mode

Can you post first few lines from your table and also your expected output table?

0
Entering edit mode

I only have one table and it is that table I want to use for the heatmap visualisation. Here is an example, I don't know how to write it in this comment box, but I will try to make it understandable.

1. source condition score
2. gene1 sample1 0.001
3. gene2 sample1 0.0003
4. gene3 sample1 -0.0004
5. gene1 sample2 0.003
6. gene2 sample2 0.0001
7. gene3 sample2 -0.0005

Dont mind the numbers in front of the table. Does this make it clearer?

6
Entering edit mode
16 months ago
kashiff007 ★ 1.9k

I guess you are using R. load the r package data.frame:

library(data.table)

>  df
source  condition   score
1       gene1   sample1     0.4299397
2       gene2   sample1     0.4299397
3       gene3   sample1     0.4299397
4       gene1   sample2     0.2531551
5       gene2   sample2     0.2531551
6       gene3   sample2     0.2531551

Now use this command for "unmelt" your df:

dcast(df, source ~ condition, value.var = c("score"))

The output would look like:

  source   sample1   sample2
1  gene1 0.4299397 0.2531551
2  gene2 0.4299397 0.2531551
3  gene3 0.4299397 0.2531551

3
Entering edit mode

If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one answer if they all work.

0
Entering edit mode

Thanks! That worked :)


text:
# Writing a Library

When I began coding, I focussed mainly on games. I programmed tic-tac-toe, snake1, a side scrolling ASCII game I called jumper2, a couple mario clones, a maze solving game, and a few others3. Somewhere along the way, I started losing interests in games, and gaining interests elsewhere; the types of programs I made changed. I wrote programs to do a little exploring of some mathy things, and tried to do some AI-y things as well. However, despite this, one thing stayed the same: I only wrote applications. But everything changed, when the fire nation attacked4.

# A Little Background

Less than two months ago, I started working on REnforce, a reinforcement learning5 library written in Rust. Over the summer, I had gotten interested in reinforcement learning, read bits and pieces of a book about it, and done a little messing around with OpenAI’s gym. This project was motivated by my desire to give myself an excuse to learn more about RL, and my desire to have a way to test my understanding. Also, as far as I could tell, There aren’t a lot of RL libraries out there, so there was always the chance people would notice this one, and I could have a lot of people contributing to or using it6.

# The Library

Like I said, REnforce is a RL library. My original goal was to have a library such that anyone could design their own environment, and then use my library to pick from a host standard, ready-implemented RL algorithms to train their agent on. Other goals included allowing customizability/modularity for people who wanted a lot of control over the fine details, being fast enough that people would actually be willing to use it in projects (no matter how small), and of course, having a simple, powerful, consistent, understandable API.

When I first thought of writing a library, I planned to write it in C++. C++ is my oldest language, it’s one of my favorites, it had been a while since I wrote a lot of C++, and I had recently decided to make an effort to improve my C++ coding abilities: start making use of modern features, write more safe and idiomatic code, gain an actual understanding of C++’s move semantics7, etc. After further thought, I decided to use Rust instead for one main reason: Traits.

Traits in Rust are amazing. They are like abstract base classes or Java interfaces, but way better. Once defined, they can be implemented on any type and they allow for some nice generic programming. When I say nice, I mean that you can have restrictions on what types some genertic code ues (you require that it implements some traits), but since the compile does type inferencing, the user of the code does not necessarily need to know about all these restrictions. This fit well into my goal of having both a simple, and modular API. Under the hood, the different types I define are generic over multiple parameters, but when creating and using them, the user does not have to specify any of these parameters, the compile figures it out for her. This allows me to write code like this that has multiple generic types, each required to implement some trait(s).

impl<N: Num, S: Space, A: FiniteSpace, Q, T> ParameterizedFunc<N> for EGreedyQAgent<S, A, Q, T>
where 	T: Chooser<A::Element>,
Q: QFunction<S, A> + ParameterizedFunc<N> {
// blah
}

The above is generic over 5 types; it requires that N implements the train Num8, S implements Space9, A implements FiniteSpace10, T implements Chooser11, Q implements QFunction and ParameterizedFunc12. This looks pretty ugly. However, when making use of this, the compiler figures out what type each of these is, and makes sure the types implement everything they need to for us, so we end up writing

let mut agent = EGreedyQAgent::new(q_func, action_space, 0.2, Uniform);
let mut trainer = CrossEntropy::default().eval_period(TimePeriod::TIMESTEPS(5));

trainer.train(&mut agent, &mut env);

which hides the fact that there is something ugly behind the scenes13. One thing you may notice in first code example is the strange A::Element syntax. Traits in rust allow you to specify what are called associated types. Basically, you define a trait (like Space) and say that this trait has some companion type (like Element), and you can even put restrictions on the companion type. This restrictions can be found in the trait itself, or in subsequent uses of the trait.

pub trait Space {
type Element : Debug + PartialEq + Clone;
// blah
}

impl<T, S: Space, A: FiniteSpace, M: Model<S, A>> OnlineTrainer<S, A, T> for DynaQ<S, A, M>
where T: QFunction<S, A> + Agent<S, A>,
S::Element: Hash + Eq,
A::Element: Hash + Eq {
// blah
}

In order to be a Space, your Element must be clonable, debugable, and must be have a sense of equality between members. In order to use the Dyna-Q algorithm, your space’s elements must also be hashable. Again, a lot of restrictions are put in place inside the library in order to make sure the different algorithms and concepts are properly used, but the user of the library can be completely unaware of all of this as long as she doesn’t try to do something not allowed. This is the beauty of Traits.

# The Name

The name REnforce14 was originally conceived as a homage to Rust’s trait system. The ‘Enforce’ part was due to type restrictions being enforced by the library through its heavy use of traits. The ‘R’ was just there because for some inexplicible reason15 I often feel the need to begin my projects written in rust with an ‘R’ to signify that they are written in Rust. A moments reflection caused me to realize that ‘REnforce’ could be pronounced ‘reinforce’, which seemed appropriate given the nature of the library, and so the name stuck. I really like how the ‘H’ in HLearn stands for 3 different things having to do with the library, and briefly entertained the idea of thinking up something similar for REnforce16 and putting it in the readme, but decided that having some words about it in a blog post would be plenty.

# Kinda What This Post Was Supposed to be About

Writing a library has been an interesting experience17. I don’t know if I have done a good job at it so far, since REnfoce has no users18, but I definitely feel like I am getting better at it. One of the hardest parts has been testing the code. A library could potentially be put through many different use cases, and I imagine people want a library that works in every case, or at least just the ones they plan on using it for. When developing applications, testing code is easy, because you just run it and see what happens. Working on this library, I haven’t found good, obvious tests for the code I write. Originally, for each new RL algorithm I implemented, I would write a short example making use of the algorithm to see if it could sucessfully train an agent in some environment. This is better than nothing, but it doesn’t do much to make sure the algorithm is aptly named19 and it is hard/annoying to come up with a different example for each algorithm. Recently, I started testing every algo on the same simple environment, which alleviates the second problem, but possible worsens the first as simple environments are easier to learn. However, the examples seem to work for the most part, and I have been getting a nice sense of accomplishment everytime I run an example and the agent has actually learned, so this approach isn’t all bad.

One thing that makes me nervous is that I am no expert in RL or library writing for that matter. While writing this, I am having to make a lot of decisions about implementations and type restrictions. I am not always confident that my choices accurately reflect the requirements of an algorithm, or the best approach to do what I want. When writing applications, if I have this doubt, it’s less nerve racking since the application is mainly meant to only be used by me. Working on a library, it feels more like everything I do has to be as close to perfect as possible. This concern has only been growing, as I have been trying to put it on hold while I add more features to the library in the hopes of getting it to the point where it feels like more than just a toy project, and could actually be used for something non-trivial.

Another issue I have noticed is that I am not sure what a simple, clean, nice, powerful, customizable, understandle, constructive, modern API looks like. The only way I have of getting an idea of how developing with this library feels like20 to someone else is by writing examples, but this hasn’t stopped me from wondering if things or too verbose, or if I’ve achieved a decent level of simplicity. I found several types, I would try to implement something, commit my changes, and then need to go back and tweak/add things after trying to use my latest addition in an example. Whenever I notice that I’ve missed things like this, I can’t help but wonder if there is other stuff I missed that I just didn’t notice. I think the lesson here is that the best way to know how good of job you are doing at designing an API is for it to be tested.

Overall, the expierence has been a good one. I mainly talked about the less fun points here, but it has been nice writing this code, and especially getting more familiar with Rust’s trait system. Hopefully, I continue to work on this for a while, and it grows into something bigger than I think it will.

1. Like 4 times. It was always interesting seeing the different ways I implemented it as I learned more about coding. I have lost most of my implementations, but I still have the most recent one, from when I was a sophomore in high school, a simpler time.

2. You were a square that was constantly moving to the right. You had to press space to jump in order to avoid obstacles and collect powerups that would change your speed, give you extra lives, etc. I had so much fun making it because it was my first “original” game. I’m sad I no longer have the source code for it.

3. Not in that order. Very much not in that order.

4. One of my favorite shows when I was younger. Good times.

5. Not entirely important what this is, but the basic idea is you have your program interact with some environment, and you tell it how good it is doing without telling it what to do. It then uses the rewards/punishments you give it to figure out what to do. Think 5 year old learning not a touch a hot stove (environment) by touching one (interacting) and seeing that it hurts (punishment).

6. So far, so good. I estimate I have about 0 users, and 0 non-me contributors.

7. I still have no idea when I need to use std::move() and when the compiler figures things out, or how many &’s I need to use for the different constructors and assignment operators I implement myself.

8. N must be a type of number. It is the type returned by the function represented by EGreedyQAgent

9. S must be a mathematical space (like the plane) where the agent’s obervations are drawn from

10. A must be a space like S, but must also be finite.

11. T is a function that randomly selects an element from a list

12. Q is a parameterized, Q function

13. The last line is the one that invokes the previous code snippet. CrossEntropy only trains agents that are also parameterized functions, and so the call to train forces the compile to make sure everything is as it should be.

14. Pronounced R-enforce, reinforce, or REN-force

15. Honestly not sure why I started doing it, but I think some other people do it too

16. The R is for Rust, the R is for Reinforcement learning, the R is for Really gotta think of a third word I can use.

17. Somewhere along the way, I think I forgot this was intended to be about my experiences writing the library code I had written so far, and started feeling the need to defend my choice of Rust. Speaking of which, Cargo is awesome for getting dependencies and testing code.

18. I can’t blame people for not using it. It kinda sucks.

19. It doesn’t tell me if the algorithm I implemented is what I say it is. Even though the thing I’m calling QLearner can train an agent to escape a maze, it might not actually be performing Q-learning due to me implementing it incorrectly

20. *would feel like


text:
# zbMATH — the first resource for mathematics

Total positivity and renewal theory. (English) Zbl 0682.60078
Probability, statistics, and mathematics, Pap. in Honor of Samuel Karlin, 141-162 (1989).
[For the entire collection see Zbl 0679.00013.]
The paper unifies and considerably extends several known results on the relations between a probability density f and the corresponding renewal density u by means of total positivity. Moreover for a density f with decreasing failure rate there is given a straight forward proof of the fact that (in the discrete time formulation) $$u(n)-u(n+1)$$ is asymptotic to a constant multiple of the tail probabilities of f.
Reviewer: M.Schäl

##### MSC:
 60K05 Renewal theory
##### Keywords:
renewal density; total positivity; tail probabilities


text:
# ML Aggarwal Class 8 Solutions for ICSE Maths Chapter 12 Linear Equations and Inequalities in one Variable Check Your Progress

## ML Aggarwal Class 8 Solutions for ICSE Maths Chapter 12 Linear Equations and Inequalities in One Variable Check Your Progress

Question 1.
Solve the following equations:

Solution:

Question 2.
The sum of three consecutive multiples of 11 is 363. Find these multiples.
Solution:

Question 3.
Sum of two numbers is 95. If one exceeds the other by 15, find the numbers.
Solution:

Question 4.
One-half of a number is equal to one-third of its succeeding number. Find the first number.
Solution:

Question 5.
The numerator of a rational number is 8 less than its denominator. If the numerator is increased by 2 and denominator is decreased by 1, the number obtained is $$\frac{1}{2}$$. Find the number.
Solution:

Question 6.
The present ages of Rohit and Mayank are in the ratio 11 : 8. 8 years later the sum of their ages will be 54 years. What are their present ages?
Solution:

Question 7.
A father’s age is 3 times the sum of ages of his two sons. Five years later he will be twice the sum of ages of his two sons. Find the present age of the father.
Solution:

Question 8.
The digits of a two-digit number differ by 7. If the digits are interchanged and the resulting number is added to the original number we get 121. Find the original number.
Solution:

Question 9.
The ten’s digit of a two-digit number exceeds it’s unit’s digit by 5. When digits are reversed, the new number added to the original number becomes 99. Find the original number.
Solution:

Question 10.
Sonia went to a bank with ₹2,00,000. She asked the cashier to give her ₹500 and ₹2000 currency notes in return. She got 250 currency notes in all. Find the number of each kind of currency notes.
Solution:

Question 11.
Ajay covers a distance of 240 km in $$4 \frac{1}{4}$$ hours. Some part of the journey was covered at the speed of 45 km/h and the remaining at 60 km/h. Find the distance covered by him at the rate of 60 km/h.
Solution:

Question 12.
If x ϵ {even integers), represent the solution set of the inequation -5 ≤ x < 5 on a number line.
Solution:

Question 13.
Solve the following inequality and graph its solution on a number line:

Solution:


text:
• Create Account

## How to use Perlin noise in terrain generation

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

13 replies to this topic

### #1Mekuri  Members

301
Like
0Likes
Like

Posted 07 September 2012 - 09:57 AM

So basically I am trying to make some 2d side scrolling terrain, (like Terraria). So far I've managed nicely with some simple terrain, but I'm taking the next step. Online I found some code, using the Simplex Noise. I figured I'd try it out and try to implement it to get a good idea on how it works.
The results I get are.. useless.. Just random blocks here and there.. Now the way I see it I've totally misunderstood how to implement this for terrain generation (works fine for picture generation).
First, here's the code for the Noise class, it's a bit lengthy, but I thought I should include all of it:

/// <summary>
/// Implementation of the Perlin simplex noise, an improved Perlin noise algorithm.
/// Based loosely on SimplexNoise1234 by Stefan Gustavson <http://staffwww.itn.liu.se/~stegu/aqsis/aqsis-newnoise/>
///
/// </summary>
public class Noise
{
/// <summary>
/// 1D simplex noise
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public static float Generate(float x)
{
int i0 = FastFloor(x);
int i1 = i0 + 1;
float x0 = x - i0;
float x1 = x0 - 1.0f;
float n0, n1;
float t0 = 1.0f - x0 * x0;
t0 *= t0;
n0 = t0 * t0 * grad(perm[i0 & 0xff], x0);
float t1 = 1.0f - x1 * x1;
t1 *= t1;
n1 = t1 * t1 * grad(perm[i1 & 0xff], x1);
// The maximum value of this noise is 8*(3/4)^4 = 2.53125
// A factor of 0.395 scales to fit exactly within [-1,1]
return 0.395f * (n0 + n1);
}
/// <summary>
/// 2D simplex noise
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static float Generate(float x, float y)
{
const float F2 = 0.366025403f; // F2 = 0.5*(sqrt(3.0)-1.0)
const float G2 = 0.211324865f; // G2 = (3.0-Math.sqrt(3.0))/6.0
float n0, n1, n2; // Noise contributions from the three corners
// Skew the input space to determine which simplex cell we're in
float s = (x + y) * F2; // Hairy factor for 2D
float xs = x + s;
float ys = y + s;
int i = FastFloor(xs);
int j = FastFloor(ys);
float t = (float)(i + j) * G2;
float X0 = i - t; // Unskew the cell origin back to (x,y) space
float Y0 = j - t;
float x0 = x - X0; // The x,y distances from the cell origin
float y0 = y - Y0;
// For the 2D case, the simplex shape is an equilateral triangle.
// Determine which simplex we are in.
int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
if (x0 > y0) { i1 = 1; j1 = 0; } // lower triangle, XY order: (0,0)->(1,0)->(1,1)
else { i1 = 0; j1 = 1; }	  // upper triangle, YX order: (0,0)->(0,1)->(1,1)
// A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
// a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
// c = (3-sqrt(3))/6
float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
float y1 = y0 - j1 + G2;
float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords
float y2 = y0 - 1.0f + 2.0f * G2;
// Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
int ii = i % 256;
int jj = j % 256;
// Calculate the contribution from the three corners
float t0 = 0.5f - x0 * x0 - y0 * y0;
if (t0 < 0.0f) n0 = 0.0f;
else
{
t0 *= t0;
n0 = t0 * t0 * grad(perm[ii + perm[jj]], x0, y0);
}
float t1 = 0.5f - x1 * x1 - y1 * y1;
if (t1 < 0.0f) n1 = 0.0f;
else
{
t1 *= t1;
n1 = t1 * t1 * grad(perm[ii + i1 + perm[jj + j1]], x1, y1);
}
float t2 = 0.5f - x2 * x2 - y2 * y2;
if (t2 < 0.0f) n2 = 0.0f;
else
{
t2 *= t2;
n2 = t2 * t2 * grad(perm[ii + 1 + perm[jj + 1]], x2, y2);
}
// Add contributions from each corner to get the final noise value.
// The result is scaled to return values in the interval [-1,1].
return 40.0f * (n0 + n1 + n2); // TODO: The scale factor is preliminary!
}

public static float Generate(float x, float y, float z)
{
// Simple skewing factors for the 3D case
const float F3 = 0.333333333f;
const float G3 = 0.166666667f;
float n0, n1, n2, n3; // Noise contributions from the four corners
// Skew the input space to determine which simplex cell we're in
float s = (x + y + z) * F3; // Very nice and simple skew factor for 3D
float xs = x + s;
float ys = y + s;
float zs = z + s;
int i = FastFloor(xs);
int j = FastFloor(ys);[attachment=11149:perlinBug.png]
int k = FastFloor(zs);
float t = (float)(i + j + k) * G3;
float X0 = i - t; // Unskew the cell origin back to (x,y,z) space
float Y0 = j - t;
float Z0 = k - t;
float x0 = x - X0; // The x,y,z distances from the cell origin
float y0 = y - Y0;
float z0 = z - Z0;
// For the 3D case, the simplex shape is a slightly irregular tetrahedron.
// Determine which simplex we are in.
int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
/* This code would benefit from a backport from the GLSL version! */
if (x0 >= y0)
{
if (y0 >= z0)
{ i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } // X Y Z order
else if (x0 >= z0) { i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 0; k2 = 1; } // X Z Y order
else { i1 = 0; j1 = 0; k1 = 1; i2 = 1; j2 = 0; k2 = 1; } // Z X Y order
}
else
{ // x0<y0
if (y0 < z0) { i1 = 0; j1 = 0; k1 = 1; i2 = 0; j2 = 1; k2 = 1; } // Z Y X order
else if (x0 < z0) { i1 = 0; j1 = 1; k1 = 0; i2 = 0; j2 = 1; k2 = 1; } // Y Z X order
else { i1 = 0; j1 = 1; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } // Y X Z order
}
// A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
// a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
// a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
// c = 1/6.
float x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
float y1 = y0 - j1 + G3;
float z1 = z0 - k1 + G3;
float x2 = x0 - i2 + 2.0f * G3; // Offsets for third corner in (x,y,z) coords
float y2 = y0 - j2 + 2.0f * G3;
float z2 = z0 - k2 + 2.0f * G3;
float x3 = x0 - 1.0f + 3.0f * G3; // Offsets for last corner in (x,y,z) coords
float y3 = y0 - 1.0f + 3.0f * G3;
float z3 = z0 - 1.0f + 3.0f * G3;
// Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
int ii = i % 256;
int jj = j % 256;
int kk = k % 256;
// Calculate the contribution from the four corners
float t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0;
if (t0 < 0.0f) n0 = 0.0f;
else
{
t0 *= t0;
n0 = t0 * t0 * grad(perm[ii + perm[jj + perm[kk]]], x0, y0, z0);
}
float t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1;
if (t1 < 0.0f) n1 = 0.0f;
else
{
t1 *= t1;
n1 = t1 * t1 * grad(perm[ii + i1 + perm[jj + j1 + perm[kk + k1]]], x1, y1, z1);
}
float t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2;
if (t2 < 0.0f) n2 = 0.0f;
else
{
t2 *= t2;
n2 = t2 * t2 * grad(perm[ii + i2 + perm[jj + j2 + perm[kk + k2]]], x2, y2, z2);
}
float t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3;
if (t3 < 0.0f) n3 = 0.0f;
else
{
t3 *= t3;
n3 = t3 * t3 * grad(perm[ii + 1 + perm[jj + 1 + perm[kk + 1]]], x3, y3, z3);
}
// Add contributions from each corner to get the final noise value.
// The result is scaled to stay just inside [-1,1]
return 32.0f * (n0 + n1 + n2 + n3); // TODO: The scale factor is preliminary!
}
private static byte[] perm = new byte[512] { 151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
};
private static int FastFloor(float x)
{
return (x > 0) ? ((int)x) : (((int)x) - 1);
}
private static float grad(int hash, float x)
{
int h = hash & 15;
float grad = 1.0f + (h & 7);   // Gradient value 1.0, 2.0, ..., 8.0
if ((h & 8) != 0) grad = -grad;		 // Set a random sign for the gradient
}
private static float grad(int hash, float x, float y)
{
int h = hash & 7;	  // Convert low 3 bits of hash code
float u = h < 4 ? x : y;  // into 8 simple gradient directions,
float v = h < 4 ? y : x;  // and compute the dot product with (x,y).
return ((h & 1) != 0 ? -u : u) + ((h & 2) != 0 ? -2.0f * v : 2.0f * v);
}
private static float grad(int hash, float x, float y, float z)
{
int h = hash & 15;	 // Convert low 4 bits of hash code into 12 simple
float u = h < 8 ? x : y; // gradient directions, and compute dot product.
float v = h < 4 ? y : h == 12 || h == 14 ? x : z; // Fix repeats at h = 12 to 15
return ((h & 1) != 0 ? -u : u) + ((h & 2) != 0 ? -v : v);
}
private static float grad(int hash, float x, float y, float z, float t)
{
int h = hash & 31;	  // Convert low 5 bits of hash code into 32 simple
float u = h < 24 ? x : y; // gradient directions, and compute dot product.
float v = h < 16 ? y : z;
float w = h < 8 ? z : t;
return ((h & 1) != 0 ? -u : u) + ((h & 2) != 0 ? -v : v) + ((h & 4) != 0 ? -w : w);
}
}

The way I try to use it is the following:

private void CreatePerlinWorld()
{
world = new Tile[_maxWidth, _maxHeight];
diamond = new float[_maxWidth, _maxHeight];
for (int x = 0; x < world.GetLength(0) - 1; x++)
{
for (int y = 0; y < world.GetLength(1) - 1; y++)
{
diamond[x,y] = Noise.Generate(x, y);
}
}
}
private void GeneratePerlinWorld()
{
for (int x = 0; x < _maxWidth; x++)
{
for (int y = 0; y < _maxHeight; y++)
{
if (diamond[x, y] < 0f)
world[x, y] = new Tile(TileType.None, TileCollision.Passable, ToolType.None);
if (diamond[x, y] >= -0f)
world[x, y] = new Tile(TileType.Dirt, TileCollision.Impassable, ToolType.Pickaxe);
}
}
}

First I run CreatePerlinWorld() followed by GeneratePerlinWorld().
I've attached a screenshot with the results I get.

So my questions are: What am I doing wrong? And what do I have to do to get it right?

#### Attached Thumbnails

Check out the game I am making here - http://www.youtube.com/user/NasarethMekuri

### #2FLeBlanc  Members

3133
Like
1Likes
Like

Posted 07 September 2012 - 10:29 AM

What you are doing is a common mistake:

for (int x = 0; x < world.GetLength(0) - 1; x++)
{
for (int y = 0; y < world.GetLength(1) - 1; y++)
{
diamond[x,y] = Noise.Generate(x, y);
}
}

Perlin noise is generated by interpolating values that are generated at integer boundaries. If you sample the noise at integer coordinates, then, you won't get the smooth in-between values you are expecting; what you get instead is basically white noise, as you have discovered.

Something like this might get you better results:
float frequency=1.0f/(float)world.GetLength(0);
for (int x = 0; x < world.GetLength(0) - 1; x++)
{
for (int y = 0; y < world.GetLength(1) - 1; y++)
{
diamond[x,y] = Noise.Generate((float)x*frequency (float)y*frequency);
}
}

You do need to tweak the exact mapping by playing with the frequency value.

A good rule of thumb for choosing frequency is that a Perlin function has 1 feature per unit, where a feature is a hill or valley. So if you want 1 hill, use a frequency equal to your world size dimension. If you want more hills, use a larger value.

As far as using noise in a sane manner to generate terrain, you might check out this article.

Edited by FLeBlanc, 07 September 2012 - 10:30 AM.

### #3swiftcoder  Senior Moderators

17830
Like
2Likes
Like

Posted 07 September 2012 - 10:45 AM

While FLeBlanc has a point, I'd argue that a larger problem is the fact that you are only using a single octave of simplex noise. Simplex noise is little more than smoothed white noise - to get interesting results, you need to combine multiple layers of simplex noise into a 'fractal'.

The simplest fractal is probably 'fractal brownian motion':
double noise_fractal_brownian_motion(int octaves, double x, double y, double z) {

const double lacunarity = 1.9;

const double gain = 0.65;

double sum = 0.0;

double amplitude = 1.0;

int i;

for (i = 0; i < octaves; i++) {

sum += amplitude * noise_simplex(x, y, z);

amplitude *= gain;

x *= lacunarity;

y *= lacunarity;

z *= lacunarity;

}

return sum;

}

where 'octaves' controls the number of layers in your noise (tweaking 'lacunarity' and 'gain' as needed to modify the result).

Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]

### #4FLeBlanc  Members

3133
Like
3Likes
Like

Posted 07 September 2012 - 11:09 AM

I don't know that I'd say that was a larger problem. It doesn't matter how many layers of detail you stack on; unless you do your mapping correctly, all you're adding is ever smaller detail that still gets lost in the mapping. The first layer defines the over-all feature size, successive layers are detail. Fix the mapping first, then add more detail.

### #5Mekuri  Members

301
Like
0Likes
Like

Posted 07 September 2012 - 01:26 PM

Thank you both for your quick responses. I will try playing around with the frequency and see what I get. Also, depending on the results I will try with more octaves.

Thanks- Really appreciated, both of you

Check out the game I am making here - http://www.youtube.com/user/NasarethMekuri

### #6jefferytitan  Members

2508
Like
2Likes
Like

Posted 07 September 2012 - 03:24 PM

Agreed on the integer problem, it gets so many people. Also potentially agreed about octaves. But I wonder if there's an even more fundamental problem. From your screenshot it looks like you're using 2D Perlin noise. Often people use 2D Perlin noise to generate a heightfield for a 3D world. For a side-scrolling platformer I would expect you'd only want 1D Perlin noise, and then to use those values as the height of your hills. I would think that 2D Perlin noise is only applicable if you want to allow mining (like Minecraft/Infiniminer) or if you want to allow caves. Even then, raw 2D Perlin noise is generally not useful as it would create islands floating in space. Instead you'd use 1D Perlin noise to create the general contour of the land, and then subtract 2D Perlin noise from anything that appears under that curve to make caves.

### #7Mekuri  Members

301
Like
0Likes
Like

Posted 07 September 2012 - 03:52 PM

Agreed on the integer problem, it gets so many people. Also potentially agreed about octaves. But I wonder if there's an even more fundamental problem. From your screenshot it looks like you're using 2D Perlin noise. Often people use 2D Perlin noise to generate a heightfield for a 3D world. For a side-scrolling platformer I would expect you'd only want 1D Perlin noise, and then to use those values as the height of your hills. I would think that 2D Perlin noise is only applicable if you want to allow mining (like Minecraft/Infiniminer) or if you want to allow caves. Even then, raw 2D Perlin noise is generally not useful as it would create islands floating in space. Instead you'd use 1D Perlin noise to create the general contour of the land, and then subtract 2D Perlin noise from anything that appears under that curve to make caves.

I've played around with the 2D algorithm for a while, and I think you might be right. I do get some decent terrain when I play around with the frequency, but my "groundlevel" is always flat.

I am getting a better understanding of this by playing around, so I think I'll try throwing myself into doing what you suggested. Thanks a lot

Check out the game I am making here - http://www.youtube.com/user/NasarethMekuri

### #8FLeBlanc  Members

3133
Like
4Likes
Like

Posted 07 September 2012 - 08:33 PM

1D noise is useful if you want to do just simple rolling terrain. 2D is useful if you want to do things like caves and overhangs. Again, I refer you to this article written by JTippetts. It's pretty interesting, and I think you can do some cool stuff with it. I hope he doesn't mind me linking to his images, but I think they really help to make the point. (JTippetts, if you don't want me linking to them, let me know and I'll remove them.) The premise of it is this: a function differentiates between ground and sky (anything below a certain level is ground, anything above is sky). Normally this threshold is a flat line at a certain level (Y=0, for example). A 1D noise function is used to push the ground level up or down by a certain amount.

Here is the basic ground/sky:

The white part (which is hard to see, of course, on the white background here) is the ground.

The article talks about using a 1D function to perturb the ground, which would end up looking like this:

It also talks about using a 2D function to perturb it, which would look like this:

The second one, imo, is "cooler" looking, but it does result in floating islands.

The article also talks about using a second function to impose caves upon the terrain:

It's really an interesting article, and while it is written sort of centric to his own library, the idea of it should be usable with any library.

### #9larspensjo  Members

1561
Like
3Likes
Like

Posted 08 September 2012 - 01:59 AM

A common solution is to use 2D simplex noise of a couple octaves to get some nice random landscape.

Then you use a 3D simplex noise, interpreted as density, to get holes in the landscape. This will make a much more dramatic view, where you can get over hang, etc. Using an inverted 3D density, you can get flying islands if you want that.

3D can of course be used to make caves, which is a side effect of the density interpretation. However, they don't look very good. They are round and not long and windy. With a few tricks, I got some nice caves going, with 3D simplex, see Making caves from Simplex noise.

At last a warning about 3d Simplex: It can be very costly. There are ways to improve the situation, for example using interpolation.

I also use 2D Simplex noise to control vegetation density. A low frequency variant to get forests, and a high frequency to control the individual trees.

The possibilities are unlimited. You can use a 2D low frequency Simplex to interpret as temperature, which would give you different biomes.

What I find very difficult to do, however, is making nice rivers from Simplex algorithms. Of course, there are other ways.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

### #10jefferytitan  Members

2508
Like
1Likes
Like

Posted 08 September 2012 - 03:15 AM

Very interesting larspensjo, but keep in mind that the OP will have to subtract 1 dimension from all your advice as they are making a 2D side scroller rather than a 3D world.

### #11riuthamus  Moderators

8194
Like
1Likes
Like

Posted 08 September 2012 - 06:01 AM

I actually like the idea of the 3d one... so thanks for posting it!

For the OP, we found that perlin worked the best, I would play with it, see if you can create an in game render system that will let you modify values and click GENERATE, this is what we did and it let us find the best results quick.

### #12swiftcoder  Senior Moderators

17830
Like
2Likes
Like

Posted 08 September 2012 - 06:35 AM

For the OP, we found that perlin worked the best

Simplex and perlin noise are pretty much indistinguishable - the one is a further development of the other.

Unless, of course, you learned about perlin noise here, which is wrong (it actually describes 'Value Noise', combined with 'Fractal Brownian Motion').

Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]

### #13riuthamus  Moderators

8194
Like
1Likes
Like

Posted 08 September 2012 - 09:40 AM

For the OP, we found that perlin worked the best

Simplex and perlin noise are pretty much indistinguishable - the one is a further development of the other.

Unless, of course, you learned about perlin noise here, which is wrong (it actually describes 'Value Noise', combined with 'Fractal Brownian Motion').

I never said they were or were not... i simply said what we went with. We have not touched terrain generation in months since we have been focused on character creation, items, crafting, spells, so when we get back to it I will most certainly take a look at the Simplex process. I can tell you, that the main point of my post was that having an in game generation setup was much more helpful than closing, recompiling, and doing it all over again. *shrugs* that is how it has worked for us.... perhaps that wont for you, or anybody else. Simply sharing my thoughts friend!

Edited by riuthamus, 08 September 2012 - 09:42 AM.

### #14Mekuri  Members

301
Like
0Likes
Like

Posted 08 September 2012 - 12:50 PM

I have been playing around with both 1D and 2D for some time now. I'm still learning as I go, since the math of this is still beyond my full comprehension.

I've managed to do some decent terrain with 1D and then use 2D noise to add some caves and such. I'm not satisfied with the results yet, but I am further than I've been for weeks
Now I need some way to randomize my output, as it seems atm things only change when I change the frequency, and I hope that it isn't the only variable that can change the output, since the results can easly go from good to disaster if the value is changed too much.
Also I think I'll try looking into doing some octaves, since I wouldn't mind the terrain to be slightly more jagged.

1D noise is useful if you want to do just simple rolling terrain. 2D is useful if you want to do things like caves and overhangs. Again, I refer you to this article written by JTippetts. It's pretty interesting, and I think you can do some cool stuff with it.

I actually did read about half of that article yesterday, I really liked the first part of it. I did kinda lose focus about halfway through. I think I'll need to read it a few times, but I'll probably wait til I'm over this flu

For the OP, we found that perlin worked the best, I would play with it, see if you can create an in game render system that will let you modify values and click GENERATE, this is what we did and it let us find the best results quick.

That's a good idea, and I think I could save quite a bit of time doing that, and it could also be a useful addition to the final game. - Thanks for the idea.

Thanks a lot for all the input- This really is an interesting, but difficult topic (at least for me ).

Check out the game I am making here - http://www.youtube.com/user/NasarethMekuri

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


text:
# GLSL if-else statement unexpected behaviour

This question is related to this other one I asked a few days ago. Because I have finally get to the bottom of the issue, I have rather preferred to open a new question with a more detailed information of what is going on.

I basically have a GLSL fragment shader that is giving me unexpected results, depending on whether it is executed using a NVIDIA Quadro, in which works as expected, or a NVIDIA GeForce, in which does something weird. After a detailed evaluation of what is going on, I have realised that the issue is around the discard operation when using the NVIDIA GeForce 560 Ti.

Can someone explain me why these the two codes behave different in a NVIDIA GeForce 560 Ti, but exactly the sume in a NVIDIA Quadro?

EDIT #1: After some deeper evaluation I can conclude that the discard statement it is not the problem. The problem occurs when running the sumw if-else statement at the end of the shader, once colour has been computed. For whatever reason, if I run the sumw if-else at the end the shader, the final render is not the expected one. On the other hand, if the sumw if-else statement is in the middle of the shader, right before running the code that computes the final value of colour, the shaders works as expected.

VERSION #1 (works wrong in GeForce 560, basically doesn't discard the pixel)

i = 0;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[0],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 1;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[1],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 2;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[2],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 3;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[3],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 4;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[4],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 5;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[5],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 6;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[6],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 7;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[7],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}

if (sumw<=0.0)           // Evaluating this here makes that the shader behave wrong
{
discard;
}
else
{
gl_FragColor = vec4(colour, 1);
}

VERSION #2 (works the same in both graphics card)

if(sumw<=0.0)            // Evaluating this here makes that the shader perform as expected
{
discard;
}
else
{
i = 0;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[0],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 1;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[1],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 2;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[2],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 3;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[3],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 4;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[4],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 5;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[5],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 6;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[6],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
i = 7;
if (i<numCameras && sumw>0.0)
{
vec4 texCoord = gl_TextureMatrix[i] * p;
vec4 texColour = texture2DProj(texSampler[7],texCoord);
float w = camw[i] / sumw;
if (w>1.0) w = 1.0;
colour += texColour.rgb*w;
}
}
gl_FragColor = vec4(colour, 1);

If I am not mistaken, the only difference between the two codes is that the first version does not discard the pixel until the rest of operation have been compleated, whereas the second version discards the pixel if necessary, otherwise carries on with the rest of the computation.

However, they should behave the same, because the computation of colour dos not affect the condition to discard or not, because sumw doesn't change its value.

Any explanation to this?

• Try use 0.001 instead of 0.0 in condition Jul 18 '12 at 15:17
• @Miro The same... I suspect that the difference is in how discard; is interpreted by the two graphics card, but I can't see why.
– Dan
Jul 18 '12 at 15:39

## 1 Answer

You are inducing undefined behavior.

The behavior of the texture functions that need implicit derivatives is undefined if the texture coordinate provided to it is not within uniform flow (ie: in a conditional branch or uses the results of such a conditional branch).

The texture coordinates you use, not to mention the samplers, are all based on the results of conditional branching logic. Therefore, the results of accessing the texture with them are undefined.

To resolve this, you must either do all of your texture accesses before deciding which results to use, or you must get gradients for all of your texture coordinates and then use the texture "Grad" function(s). Or you can use a texture "Lod" function for a specific Lod of the image (but you won't get mipmaping, for obvious reasons).

• Brilliant answer! I've been fighting with this issue for days! Now I really understood the problem. I've moved all the texture2DProj's out of the conditional branches and works fantastic! this is how the shader finally looks. THANKS!
– Dan
Jul 18 '12 at 18:16


text:
# Resize tikzfigure in tcolorbox depending on height of tcolorbox

in my book I got many different type of boxes so I made some macros placing the boxes automatically according to their arguments.

That's one of them. So far I made some boxes that have big "+" and "-" inside and some that have small ones. It'd be nice, if I could manage to adapt the size of the signs according to the height of the box.

\documentclass{book}

\usepackage{xstring}
\usepackage[many]{tcolorbox}
\usepackage{calc}
\usetikzlibrary{calc}

\definecolor{mp}{RGB}{240,240,240}

\newcommand{\bround}{
\draw[rounded corners=15pt,color=mp,line width=1pt,fill=white]
(frame.south west) --
(frame.north west) --
(frame.north east) --
(frame.south east) -- cycle;
}
\newcommand{\sep}{
\draw[color=mp,line width=1pt]
([yshift=10pt]segmentation.south) -- ([yshift=-10pt]segmentation.north);
}

\newcommand{\ds}[1]{
\IfEqCase{#1}{%
{bpl}{\dosign{0.25}{x}{y}{0}{0}{20}{-50}{50}{-60}}%
{bmr}{\dosign{0.75}{x}{x}{0}{0}{20}{-50}{50}{-60}}%
}[\PackageError{ds}{Undefined option to ds: #1}{}]%
}

\newcommand{\dosign}[9]{
\coordinate (xl) at ($(frame.west)!#1!(frame.east)$ );
\coordinate (yl) at ([yshift=#9pt] $(frame.north)$ );
\draw[line width=#6pt,mp]
([#2shift=#7pt,yshift=#4pt]xl|-yl) -- ([#2shift=#8pt,yshift=#4pt]xl|-yl);
\draw[line width=#6pt,mp]
([#3shift=#7pt,yshift=#5pt]xl|-yl) -- ([#3shift=#8pt,yshift=#5pt]xl|-yl);
}

\newcommand{\doibs}[4]{\scantokens{%
\begin{tcolorbox}[
colback=white,
arc=10pt,
colframe=black,
freelance,
sidebyside,
sidebyside align=top,
frame code=\bround,
segmentation code=\sep,
interior code=\ds{#3}\ds{#4}
]
\raggedright #1 \tcblower \raggedright #2
\end{tcolorbox}}}

\begin{document}

\doibs{A\\A\\A\\A}{B}{bpl}{bmr}

\end{document}

As I have every type of combination (+-,++,--,+=,==,..), I wrote those macros keeping the source code as minimal as possible (according to my skills). If I had the possibility to get the height in macro \ds, I could ajust the signs by recalculating their dimensions, coordinates, etc.

Does anyone have a clue?

• Do you want just the height/widht of the symbols to adapt or also the line width used for the symbols? – Gonzalo Medina Apr 14 '14 at 14:39
• they should be stretched proportional, therefore the line width as well – Moldevort Apr 14 '14 at 14:58
• I thought so. The second solution in my answer below also adjusts the line width. – Gonzalo Medina Apr 14 '14 at 14:59

## Updated version:

The first version of the code (see below) had one problem: if the box height was too big, then the signs will protrude their corresponding boxes; this can be solved choosing a maximum value for the length of the signs. The following code shows one such possibility; the length is (approximately) the minimum between the height of the box and half its width:

\documentclass{book}
\usepackage[paperheight=30cm,textheight=25cm]{geometry}% just for the example
\usepackage{xstring}
\usepackage[many]{tcolorbox}
\usetikzlibrary{calc}

\definecolor{mp}{RGB}{240,240,240}

% To control the line width for the signs
\def\RuleFactor{0.15}

\newcommand{\bround}{
\draw[rounded corners=15pt,color=mp,line width=1pt,fill=white]
(frame.south west) --
(frame.north west) --
(frame.north east) --
(frame.south east) -- cycle;
}
\newcommand{\sep}{
\draw[color=mp,line width=1pt]
([yshift=10pt]segmentation.south) -- ([yshift=-10pt]segmentation.north);
}

\newcommand{\ds}[1]{
\IfEqCase{#1}{%
{pp}{\dosign{0.25}{plus}\dosign{0.75}{plus}}%
{pm}{\dosign{0.25}{plus}\dosign{0.75}{minus}}%
{pe}{\dosign{0.25}{plus}\dosign{0.75}{equal}}%
{mp}{\dosign{0.25}{minus}\dosign{0.75}{plus}}%
{mm}{\dosign{0.25}{minus}\dosign{0.75}{minus}}%
{me}{\dosign{0.25}{minus}\dosign{0.75}{equal}}%
{ep}{\dosign{0.25}{equal}\dosign{0.75}{plus}}%
{em}{\dosign{0.25}{equal}\dosign{0.75}{minus}}%
{ee}{\dosign{0.25}{equal}\dosign{0.75}{equal}}%
}[\PackageError{ds}{Undefined option to ds: #1}{}]%
}

\newcommand{\dosign}[2]{
\coordinate (xl) at ($(frame.west)!#1!(frame.east)$ );
\IfEq{#2}{plus}{%
\path[mp,draw]
let \p1 = ( $([yshift=-5pt]xl|-frame.north) - ([yshift=5pt]xl|-frame.south)$ ),
\p2 = ( $([xshift=10pt]frame.west) - ([xshift=-10pt]segmentation.center)$ )
in
([yshift=-{0.5*min(veclen(\x1,\y1),veclen(\x2,\y2))}]xl)
edge[line width={\RuleFactor*min(veclen(\x1,\y1),veclen(\x2,\y2))}]
([yshift={0.5*min(veclen(\x1,\y1),veclen(\x2,\y2))}]xl)
([xshift=-{0.5*min(veclen(\x1,\y1),veclen(\x2,\y2))}]xl)
edge[line width={\RuleFactor*min(veclen(\x1,\y1),veclen(\x2,\y2))}]
([xshift={0.5*min(veclen(\x1,\y1),veclen(\x2,\y2))}]xl)
;
}{}
\IfEq{#2}{minus}{%
\path[line width=6pt,mp,draw]
let \p1 = ( $([yshift=-5pt]xl|-frame.north) - ([yshift=5pt]xl|-frame.south)$ ),
\p2 = ( $([xshift=10pt]frame.west) - ([xshift=-10pt]segmentation.center)$ )
in
([xshift=-{0.5*min(veclen(\x1,\y1),veclen(\x2,\y2))}]xl)
edge[line width={\RuleFactor*min(veclen(\x1,\y1),veclen(\x2,\y2))}]
([xshift={0.5*min(veclen(\x1,\y1),veclen(\x2,\y2))}]xl)
;
}{}
\IfEq{#2}{equal}{%
\path[line width=6pt,mp,draw]
let \p1 = ( $([yshift=-5pt]xl|-frame.north) - ([yshift=5pt]xl|-frame.south)$ ),
\p2 = ( $([xshift=10pt]frame.west) - ([xshift=-10pt]segmentation.center)$ )
in
([xshift=-{0.5*min(veclen(\x1,\y1),veclen(\x2,\y2))},yshift={0.12*min(veclen(\x1,\y1),veclen(\x2,\y2))}]xl)
edge[line width={\RuleFactor*min(veclen(\x1,\y1),veclen(\x2,\y2))}]
([xshift={0.5*min(veclen(\x1,\y1),veclen(\x2,\y2))},yshift={0.12*min(veclen(\x1,\y1),veclen(\x2,\y2))}]xl)
([xshift=-{0.5*min(veclen(\x1,\y1),veclen(\x2,\y2))},yshift=-{0.12*min(veclen(\x1,\y1),veclen(\x2,\y2))}]xl)
edge[line width={\RuleFactor*min(veclen(\x1,\y1),veclen(\x2,\y2))}]
([xshift={0.5*min(veclen(\x1,\y1),veclen(\x2,\y2))},yshift=-{0.12*min(veclen(\x1,\y1),veclen(\x2,\y2))}]xl)
;
}{}
}

\newcommand{\doibs}[3]{\scantokens{%
\begin{tcolorbox}[
colback=white,
arc=10pt,
colframe=black,
freelance,
sidebyside,
sidebyside align=top,
frame code=\bround,
segmentation code=\sep,
interior code=\ds{#3}
]
\raggedright #1 \tcblower \raggedright #2
\end{tcolorbox}}}

\begin{document}

\doibs{A\\A\\A\\A}{B}{pm}

\doibs{A\\A\\A\\A}{B}{me}

\doibs{A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A}{B}{pe}

\doibs{A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A\\A}{B}{ep}

\end{document}

## First version:

Here's one possibility (see remarks below):

\documentclass{book}
\usepackage{xstring}
\usepackage[many]{tcolorbox}
\usetikzlibrary{calc}

\definecolor{mp}{RGB}{240,240,240}

\newcommand{\bround}{
\draw[rounded corners=15pt,color=mp,line width=1pt,fill=white]
(frame.south west) --
(frame.north west) --
(frame.north east) --
(frame.south east) -- cycle;
}
\newcommand{\sep}{
\draw[color=mp,line width=1pt]
([yshift=10pt]segmentation.south) -- ([yshift=-10pt]segmentation.north);
}

\newcommand{\ds}[1]{
\IfEqCase{#1}{%
{pp}{\dosign{0.25}{plus}\dosign{0.75}{plus}}%
{pm}{\dosign{0.25}{plus}\dosign{0.75}{minus}}%
{pe}{\dosign{0.25}{plus}\dosign{0.75}{equal}}%
{mp}{\dosign{0.25}{minus}\dosign{0.75}{plus}}%
{mm}{\dosign{0.25}{minus}\dosign{0.75}{minus}}%
{me}{\dosign{0.25}{minus}\dosign{0.75}{equal}}%
{ep}{\dosign{0.25}{equal}\dosign{0.75}{plus}}%
{em}{\dosign{0.25}{equal}\dosign{0.75}{minus}}%
{ee}{\dosign{0.25}{equal}\dosign{0.75}{equal}}%
}[\PackageError{ds}{Undefined option to ds: #1}{}]%
}

\newcommand{\dosign}[2]{aaa
\coordinate (xl) at ($(frame.west)!#1!(frame.east)$ );
\IfEq{#2}{plus}{%
\path[mp,draw]
let \p1 = ( $([yshift=-5pt]xl|-frame.north) - ([yshift=5pt]xl|-frame.south)$ )
in
([yshift=-5pt]xl|-frame.north)
edge[line width={0.12*veclen(\x1,\y1)}]
([yshift=5pt]xl|-frame.south)
([xshift=-{0.5*veclen(\x1,\y1)}]xl)
edge[line width={0.12*veclen(\x1,\y1)}]
([xshift={0.5*veclen(\x1,\y1)}]xl)
;
}{}
\IfEq{#2}{minus}{%
\path[line width=6pt,mp,draw]
let \p1 = ( $([yshift=-5pt]xl|-frame.north) - ([yshift=5pt]xl|-frame.south)$ )
in
([xshift=-{0.5*veclen(\x1,\y1)}]xl)
edge[line width={0.12*veclen(\x1,\y1)}]
([xshift={0.5*veclen(\x1,\y1)}]xl)
;
}{}
\IfEq{#2}{equal}{%
\path[line width=6pt,mp,draw]
let \p1 = ( $([yshift=-5pt]xl|-frame.north) - ([yshift=5pt]xl|-frame.south)$ )
in
([xshift=-{0.5*veclen(\x1,\y1)},yshift={0.1*veclen(\x1,\y1)}]xl)
edge[line width={0.12*veclen(\x1,\y1)}]
([xshift={0.5*veclen(\x1,\y1)},yshift={0.1*veclen(\x1,\y1)}]xl)
([xshift=-{0.5*veclen(\x1,\y1)},yshift=-{0.1*veclen(\x1,\y1)}]xl)
edge[line width={0.12*veclen(\x1,\y1)}]
([xshift={0.5*veclen(\x1,\y1)},yshift=-{0.1*veclen(\x1,\y1)}]xl)
;
}{}
}

\newcommand{\doibs}[3]{\scantokens{%
\begin{tcolorbox}[
colback=white,
arc=10pt,
colframe=black,
freelance,
sidebyside,
sidebyside align=top,
frame code=\bround,
segmentation code=\sep,
interior code=\ds{#3}
]
\raggedright #1 \tcblower \raggedright #2
\end{tcolorbox}}}

\begin{document}

\doibs{A\\A\\A\\A}{B}{pp}

\doibs{A}{B}{me}

\doibs{A\\A\\A\\A\\A\\A\\A\\A}{B}{pe}

\doibs{A\\A\\A}{B}{ep}

\doibs{A\\A}{B}{ee}

\end{document}

## Remarks

1. The size of the shapes and the line width will adjust automatically depending on the height of the box.

2. Notice that now \dosign has just two mandatory arguments (instead of the original nine arguments) and that \doibs has only three arguments.

3. The third argument for \doibs is used to decide which symbols to draw; I implemented nine combinations for "plus" (p), "minus" (m) and "equal" (e). For example using me in the third argument means draw "minus" sign to the left and "equal" sign to the right; using pm in the third argument means draw "plus" sign to the left and "minus" sign to the right.

• Unfortunately that's not what I wanted to achieve. When I follow your source code I will have to build a macro for every box (+-,--,++,-+,=+,+=, etc) and that's what I wanted to prevent by creating \ds and \dosign. Isn't there any way to get the height out of the tcolorbox-environment? – Moldevort Apr 14 '14 at 15:06
• @Moldevort please see my updated answer. – Gonzalo Medina Apr 14 '14 at 16:33
• @Moldevort I've updated my answer with a new version that might be of interest for you. – Gonzalo Medina Apr 16 '14 at 2:45
• The freelance option is deprecated since version 3.00 of tcolorbox, as mentioned in the latest Package Documentation (version 3.05 2014-05-28). The key can simply be removed in @Gonzalo's elaborate answer. – feculededentier Jul 7 '14 at 4:01


text:
## Twin-Delayed Deep Deterministic Policy Gradient Agents

The twin-delayed deep deterministic policy gradient (TD3) algorithm is a model-free, online, off-policy reinforcement learning method. A TD3 agent is an actor-critic reinforcement learning agent that computes an optimal policy that maximizes the long-term reward.

For more information on the different types of reinforcement learning agents, see Reinforcement Learning Agents.

The TD3 algorithm is an extension of the DDPG algorithm. DDPG agents can overestimate value functions, which can produce suboptimal policies. To reduce value function overestimation includes the following modifications of the DDPG algorithm.

1. A TD3 agent learns two Q-value functions and uses the minimum value function estimate during policy updates.

2. A TD3 agent updates the policy and targets less frequently than the Q functions.

3. When updating the policy, a TD3 agent adds noise to the target action, which makes the policy less likely to exploit actions with high Q-value estimates.

You can use a TD3 agent to implement one of the following training algorithms, depending on the number of critics you specify.

• TD3 — Train the agent with two Q-value functions. This algorithm implements all three of the preceding modifications.

• Delayed DDPG — Train the agent with a single Q-value function. This algorithm trains a DDPG agent with target policy smoothing and delayed policy and target updates.

TD3 agents can be trained in environments with the following observation and action spaces.

Observation SpaceAction Space
Continuous or discreteContinuous

During training, a TD3 agent:

• Updates the actor and critic properties at each time step during learning.

• Stores past experience using a circular experience buffer. The agent updates the actor and critic using a mini-batch of experiences randomly sampled from the buffer.

• Perturbs the action chosen by the policy using a stochastic noise model at each training step.

### Actor and Critic Function

To estimate the policy and value function, a TD3 agent maintains the following function approximators:

• Deterministic actor μ(S) — The actor takes observation S and outputs the corresponding action that maximizes the long-term reward.

• Target actor μ'(S) — To improve the stability of the optimization, the agent periodically updates the target actor based on the latest actor parameter values.

• One or two Q-value critics Qk(S,A) — The critics take observation S and action A as inputs and output the corresponding expectation of the long-term reward.

• One or two target critics Q'k(S,A) — To improve the stability of the optimization, the agent periodically updates the target critics based on the latest parameter values of the critics. The number of target critics matches the number of critics.

Both μ(S) and μ'(S) have the same structure and parameterization.

For each critic, Qk(S,A) and Q'k(S,A) have the same structure and parameterization.

When using two critics, Q1(S,A) and Q2(S,A), each critic can have a different structure, though TD3 works best when the critics have the same structure. When the critics have the same structure, they must have different initial parameter values.

When training is complete, the trained optimal policy is stored in actor μ(S).

For more information on creating actors and critics for function approximation, see Create Policy and Value Function Representations.

### Agent Creation

To create a TD3 agent:

1. Create an actor using an rlDeterministicActorRepresentation object.

2. Create one or two critics using rlQValueRepresentation objects.

3. Specify agent options using an rlTD3AgentOptions object.

4. Create the agent using an rlTD3Agent object.

### Training Algorithm

TD3 agents use the following training algorithm, in which they update their actor and critic models at each time step. To configure the training algorithm, specify options using rlDDPGAgentOptions. Here, K = 2 is the number of critics and k is the critic index.

• Initialize each critic Qk(S,A) with random parameter values θQk, and initialize each target critic with the same random parameter values: ${\theta }_{Qk\text{'}}={\theta }_{Qk}$.

• Initialize the actor μ(S) with random parameter values θμ, and initialize the target actor with the same parameter values: ${\theta }_{\mu \text{'}}={\theta }_{\mu }$.

• For each training time step:

1. For the current observation S, select action A = μ(S) + N, where N is stochastic noise from the noise model. To configure the noise model, use the ExplorationModel option.

2. Execute action A. Observe the reward R and next observation S'.

3. Store the experience (S,A,R,S') in the experience buffer.

4. Sample a random mini-batch of M experiences (Si,Ai,Ri,S'i) from the experience buffer. To specify M, use the MiniBatchSize option.

5. If S'i is a terminal state, set the value function target yi to Ri. Otherwise set it to:

${y}_{i}={R}_{i}+\gamma *\underset{k}{\mathrm{min}}\left({Q}_{k}\text{'}\left({S}_{i}\text{'},\text{clip}\left(\mu \text{'}\left({S}_{i}\text{'}|{\theta }_{\mu }\right)+\epsilon \right)|{\theta }_{Qk\text{'}}\right)\right)$

The value function target is the sum of the experience reward Ri and the minimum discounted future reward from the critics. To specify the discount factor γ, use the DiscountFactor option.

To compute the cumulative reward, the agent first computes a next action by passing the next observation S'i from the sampled experience to the target actor. Then, the agent adds noise ε to the computed action using the TargetPolicySmoothModel, and clips the action based on the upper and lower noise limits. The agent finds the cumulative rewards by passing the next action to the target critics.

6. At every time training step, update the parameters of each critic by minimizing the loss Lk across all sampled experiences.

${L}_{k}=\frac{1}{M}\sum _{i=1}^{M}{\left({y}_{i}-Q\left({S}_{i},{A}_{i}|{\theta }_{Q}\right)\right)}^{2}$

7. Every D1 steps, update the actor parameters using the following sampled policy gradient to maximize the expected discounted reward. To set D1, use the PolicyUpdateFrequency option.

$\begin{array}{l}{\nabla }_{{\theta }_{\mu }}J\approx \frac{1}{M}\sum _{i=1}^{M}{G}_{ai}{G}_{\mu i}\\ {G}_{ai}={\nabla }_{A}\underset{k}{\mathrm{min}}\left({Q}_{k}\left({S}_{i},A|{\theta }_{Q}\right)\right)\text{ }\text{where}\text{\hspace{0.17em}}A=\mu \left({S}_{i}|{\theta }_{\mu }\right)\\ {G}_{\mu i}={\nabla }_{{\theta }_{\mu }}\mu \left({S}_{i}|{\theta }_{\mu }\right)\end{array}$

Here, Gai is the gradient of the minimum critic output with respect to the action computed by the actor network, and Gμi is the gradient of the actor output with respect to the actor parameters. Both gradients are evaluated for observation Si.

8. Every D2 steps, update the target actor and critics depending on the target update method. To specify D2, use the TargetUpdateFrequency option. For more information, see Target Update Methods.

For simplicity, the actor and critic updates in this algorithm show a gradient update using basic stochastic gradient descent. The actual gradient update method depends on the optimizer specified using rlRepresentationOptions.

### Target Update Methods

TD3 agents update their target actor and critic parameters using one of the following target update methods.

• Smoothing — Update the target parameters at every time step using smoothing factor τ. To specify the smoothing factor, use the TargetSmoothFactor option.

• Periodic — Update the target parameters periodically without smoothing (TargetSmoothFactor = 1). To specify the update period, use the TargetUpdateFrequency parameter.

$\begin{array}{l}{\theta }_{Qk\text{'}}={\theta }_{Qk}\\ {\theta }_{\mu \text{'}}={\theta }_{\mu }\end{array}$

• Periodic Smoothing — Update the target parameters periodically with smoothing.

To configure the target update method, create a rlTD3AgentOptions object, and set the TargetUpdateFrequency and TargetSmoothFactor parameters as shown in the following table.

Update MethodTargetUpdateFrequencyTargetSmoothFactor
Smoothing (default)1Less than 1
PeriodicGreater than 11
Periodic smoothingGreater than 1Less than 1

## References

[1] Fujimoto, Scott, Herke van Hoof, and David Meger. 'Addressing Function Approximation Error in Actor-Critic Methods'. ArXiv:1802.09477 [Cs, Stat], 22 October 2018. https://arxiv.org/abs/1802.09477.


text:
# Joule's heating effect

Can we derive joule heating effect or heating of current using energy conservation for electrons when current is varying with time ?Like using concept of drift velocity,potential diffrence across resistor in which heat is produced,work done on charge,kinetic energy of charge and coming up with formula

H=∫i²rdt where terms has usual meaning

(note i want mathematical proof using basic theory)

• You must mean $$H=\int {R\cdot I^2\cdot \mathrm{d}t}$$ – Poutnik Jun 9 at 17:39
• Wasn’t Joule’s heating effect (original work) based on mechanical work? – Solar Mike Jun 9 at 17:45
• @Poutnik you are correct but please answer it – sarthak Jun 10 at 2:54
• It would be proving a definition, what is nonsense by principle. By definition, a charge q, passing potential difference U, releases energy q.U., If that release depend on the way, potential would not be exact potential with the round integrall of dU=0. One could then create a perpetuum mobile. – Poutnik Jun 10 at 7:26
• P.S. Unless there is kinetic energy change,of course. – Poutnik Jun 10 at 7:58


text:
# Chapter 6, Exploration 48

The graph below is for the function $$x_{n+1} = f_{4.5}(x) = 4.5x(1-x)$$. You can select the value of $$n$$ to display $$f^n_{4.5}(x)$$. Notice that, as $$n$$ increases, certain intervals along the $$x$$-axis are colored red.

• How do the intervals relate to properties of the graphs of $$y=f^n_{4.5}(x)$$ for each $$n$$?
• How many intervals of each color are there for each $$n$$? Is there a general formula in terms of $$n$$? Why?
• Are there points in the interval [0, 1] whose orbits remain in that interval for all $$n$$? If so, describe them. If not, explain why.
• Does the process demonstrated here remind you of anything else that you have encountered in mathematics? If so, what?

n:


text:
Error in proof: $\mathbb{C} \cong \mathbb{C} \times \mathbb{C}$??

I've unintentionally "proved" the following: $$\mathbb{C} \cong \mathbb{C} \times \mathbb{C}$$ Can you help me tracing the error I made resulting to this non-proof? Here it is.

First of all, I recall an algebraic theorem about the complex plane: $\mathbb{C} \cong \mathbb{R}[X]/(X^2+1)$. The rest of the non-proof is about the apparent isomorphism $\mathbb{C} \times \mathbb{C} \cong \mathbb{R}[X]/(X^2+1)$.

The ring $R[X]$ is a commutative ring containing a unit, so we can write the ideal $(X^2+1)$ as a product of ideals $(X-i)(X+i)$. These ideals are indivisable: $(X+i)(X-i) \ni \frac{1}{2}(X+i)-\frac{1}{2}(X-i)=i$, so every polynomial $q = i^3q \cdot i$ is contained in the sum of ideals. Now we can use the generalisation of the Chinese remainder theorem: For a commutative ring R with unity, and indivisible ideals $I,J$ applies $R/IJ \cong R/I \times R/J$.

Now we have $\mathbb{R}[X]/(X^2+1) \cong \mathbb{R}[X]/(X+i) \times \mathbb{R}[X]/(X-i)$. I recall that for every homomorphism of rings $f: R \rightarrow S$ the isomorphism $R/\ker(f) \cong f(R)$ holds. Consider the mapping $s_r \ : \ R[X] \rightarrow S \ : \ \sum a_i X^i \mapsto \sum a_i r^i$. This substitution map is an homomorphism. A special case is the homomorphism $s_i \ : \ \mathbb{R}[X] \rightarrow \mathbb{C} \ : \ \sum a_i X^i \mapsto \sum a_i r^i$ that substitutes $i$ in $X$. Its kernel is exactly the ideal $(X-i)$. The image clairy contains $\{ a + bi: a,b \in \mathbb{R} \} = \mathbb{C}$, and the image is contained in \matbb{C} as well. So we obtain $\mathbb{C} \cong \mathbb{R}[X]/(X+i)$. The same trick with the substitution s_{-i} shows that $\mathbb{C} \cong \mathbb{R}[X]/(X-i)$. This results into $\mathbb{C} \cong \mathbb{C} \times \mathbb{C}$. Here ends the "proof."

I feel a little bad about using $s_i$, because $i \notin \mathbb{R}$, but I remember that a similar mapping had to be used to proof that $\mathbb{C} \cong \mathbb{R}[X]/(X^2+1)$.

I'd be thankful if you help me to sift this through and find the error(s).

-
Why the downvote? – user1729 Aug 1 '13 at 20:44
By the way, you should always specify what type of isomorphisms you mean. For example, how people would have answered why this couldn't be true depends on the isomorphism probably. If you mean isomorphism of $\mathbb{C}$-algebras/spaces people would have told you to count dimensions, if you had said of rings people may have told you to look for zero divisors, and if you had said as groups people would have told you that the isomorphism you wrote is correct. – Alex Youcis Aug 1 '13 at 20:49
@AlexYoucis +1 for detail and completeness. – Pedro Tamaroff Aug 1 '13 at 21:05

It's not too tough to spot! $i\notin\mathbb R$.

What your proof does in fact show is that by Chinese Remainder, $\mathbb C[X]/(X^2+1)\cong \mathbb C\times\mathbb C$.

The kernel of your homomorphism is $(X^2+1)$, not $(X-i)$, since $X-i\notin\mathbb R[X]$.

-
+1, More specifically, $x^2+1$ is irreducible in $\mathbb{R}[x]$. – Alex Youcis Aug 1 '13 at 20:44
Indeed. Which is why the quotient is a field in the first place. :) – Ted Shifrin Aug 1 '13 at 20:48

What does $\mathbb{R}[X]/(X+\operatorname{i})$ mean? The polynomial $X+\operatorname{i}$ does not belong to $\mathbb{R}[X]$, and so $\mathbb{R}[X]/(X+\operatorname{i})$ does not make sense. This is because $\operatorname{i}$ does not belong to $\mathbb{R}$.

-

$\Bbb R[x]/(x+i)$ is not defined as $x+i$ is not even in $\Bbb R[x]$.

-


text:
# Peterson correspondence

A correspondence between two surfaces under which their tangent planes at corresponding points are parallel. It was considered in general form by K.M. Peterson [1] in connection with the problem of deformation over a principal base. For example, there is a Peterson correspondence between a surface and it spherical image (cf. Spherical map), between a surface and its rotation indicatrix and between adjoint minimal surfaces (cf. Adjoint surface).

If two surfaces $S$ and $S ^ {*}$ in Peterson correspondence have a common parametrization, then their third fundamental forms are equal. The principal net for the asymptotic nets (cf. Asymptotic net) of $S$ and $S ^ {*}$ is conjugate (cf. Conjugate net) on each of them. This net is uniquely defined if the asymptotic nets do not have common families of lines, it degenerates if these nets are linked and it becomes undefined if these nets correspond to one another. Corresponding tangents to the lines of the principal net in $S$ and $S ^ {*}$ are parallel.

If the principal net is taken to be a $( u, v)$ coordinate net, then the position vectors $x$ and $x ^ {*}$ of $S$ and $S ^ {*}$ are related by

$$x _ {u} ^ {*} = \rho x _ {u} ,\ \ x _ {v} ^ {*} = \sigma x _ {v} ,$$

where the functions $\rho$ and $\sigma$ satisfy the system of equations

$$\rho _ {v} E - \sigma _ {u} F = \frac{\sigma - \rho }{2} ,\ \ \rho _ {v} F - \sigma _ {u} G = \frac{\sigma - \rho }{2} G _ {u} ,$$

i.e. $\rho$ and $\sigma$ depend only on the metric of $S$( $E$, $F$ and $G$ are the coefficients of the first fundamental form). One therefore naturally can apply a Peterson correspondence to a pair of isometric surfaces $S$ and $\widetilde{S}$: it gives another pair of isometric surfaces $S ^ {*}$ and $\widetilde{S} {} ^ {*}$ with the same normals $n= n ^ {*}$ and $\widetilde{n} = \widetilde{n} {} ^ {*}$, respectively. Also, the rotation diagrams for these surfaces are the same, and the base of the isometry for the new surfaces has the same spherical image as the original ones. For example, a sphere and a surface of constant positive curvature isometric with it correspond under a Peterson correspondence to isometric surfaces with corresponding lines of curvature, so-called Bonnet surfaces.

In particular, if the base of the isometry for $S$ and $S ^ {*}$ is a principal base, then it remains such. This is the so-called Peterson transformation of a surface deformed over a principal base into a surface of the same type. There is an extension of this transformation to the case of a family of nets [2].

A special case of a Peterson correspondence is that in which the principal net is a curvature net simultaneously in $S$ and $S ^ {*}$. This is called a Combescour correspondence.

If a Peterson correspondence is conformal, then either one of the surfaces is minimal and the other is a sphere (i.e. the Peterson correspondence is a spherical mapping), or the two surfaces are minimal and the conformal mapping vector satisfies the Laplace equation, or the surfaces are similar, or the surfaces are isothermal and are in Combescour correspondence.

There is a multi-dimensional generalization of the Peterson correspondence [4].

#### References

 [1] K.M. Peterson, "Об отношениях и средствах между кривыми поверхностями", Mat. Sb. , 1 (1866) pp. 391–438 [2] V.I. Shulikovskii, "Classical differential geometry in a tensor setting" , Moscow (1963) (In Russian) [3] S.P. Finikov, "Bending and related geometrical problems" , Moscow-Leningrad (1937) (In Russian) [4] P.A. Shirokov, A.P. Shirokov, "Affine differentialgeometrie" , Teubner (1962) (Translated from Russian)
How to Cite This Entry:
Peterson correspondence. Encyclopedia of Mathematics. URL: http://encyclopediaofmath.org/index.php?title=Peterson_correspondence&oldid=51211
This article was adapted from an original article by M.I. Voitsekhovskii (originator), which appeared in Encyclopedia of Mathematics - ISBN 1402006098. See original article