Difference between revisions of "Array Manipulation Examples and Challenge Problems"
Line 105: | Line 105: | ||
The model contains the starting data, the desired result, and one possible solution broken down step-by-step. For the full challenge, find a way to compute the solution yourself without looking. If you aren't up for that, then take on the four individual steps in the solution as four smaller challenge exercises. | The model contains the starting data, the desired result, and one possible solution broken down step-by-step. For the full challenge, find a way to compute the solution yourself without looking. If you aren't up for that, then take on the four individual steps in the solution as four smaller challenge exercises. | ||
+ | |||
+ | == Tiered Billing == | ||
+ | |||
+ | Some rate schedules such as tax rate schedules and utility billing schedules use a tiered rate scheme. For example, in California, Pacific Gas & Electric uses a 5-tiered billing system for residential customers for electricity usage. For a household assigned a baseline allotment of 200kWh per month, the rate pricing schedule (at the time of this writing) is as follows: | ||
+ | |||
+ | :{| border="1" | ||
+ | ! !!Tier 1 !! Tier 2 !! Tier 3 !! Tier 4 !! Tier 5 | ||
+ | |- | ||
+ | | consumed (kWh) || 0-200 || 201-260 || 261-400 || 401-600 || over 600 | ||
+ | |- | ||
+ | | rate ($/kWh) || $0.12 || $0.14 || $0.29 || $0.39 || $0.44 | ||
+ | |} | ||
+ | |||
+ | If you were to consume 300 kWh of electricity in a given month, your bill would be determined as follows. The "first" 200kWh would be billed at $0.12/kWh, the next 60kWh at $0.14/kWh, and then the residual 40kWh at $0.29/kWh. The bill would thus be: | ||
+ | |||
+ | :0.12*200 + 0.14*60 + 0.29*40 = $38 | ||
+ | |||
+ | <b>Given:</b> A rate schedule with usage limits for each tier; an actual usage amount. | ||
+ | |||
+ | <b>Compute:</b> Usage amount that falls into each tier, and total price to be billed. | ||
+ | |||
+ | The model file contains a rate schedule and an example usage amount. A sample solution is provided. (A second solution is also provided that uses a function new to [[Analytica 4.3]]). | ||
+ | |||
+ | <b>Model file:</b> [[media:Tiered Billing.ana|Tiered Billing.ana]] |
Revision as of 19:39, 14 December 2010
This page is a place to collect array-manipulation examples and challenge problems.
If you are learning to use Analytica, try these challenge problems before looking at the solutions.
If you have an example that might be helpful to others, post it!
These should be problems that can be described concisely, and are focused on manipulations of arrays. Beyond the scope would be questions like how to model some particular problem, or how to structure a problem.
List and 1-D manipulations
Reversing a list
Given:Index I, as a list of labels.
Define:Index I_rev containing the same labels as I, but in reverse order.
Solution 1
Define I_rev as
Slice( I, size(I)..1 )
Reversing an array
Given: Array A, indexed by I.
Compute: A_rev, also indexed by I, but with the order of elements along I reversed.
Solution 1
Define A_rev as
A[@I=size(I)-@I+1]
Re-indexing
Table Lookups
Includes 2-step lookup, aka outer-product.
Flattening and Un-flattening
Flattening a 2-D matrix to a 1-D vector
Given: Array A[I,J], indexed by I and J.
Compute: 1-D Array B[K], where K has length size(I)*size(J), containing the elements of A.
Model file: Flattening 2-D to 1-D.ana (contains setup and solutions)
The need to flatten a matrix into 1-D occurs in several contexts. When setting up an optimization, for example, the decision variables must be collected into a single 1-D vector. If you are finding the optimal weight-matrix, for example, you may need to "flatten" your initial guess, in order to pass it to NlpDefine. Similarly, you may have a 2-D array of coefficients identified in a Regression problem, that must be flattened into a 1-D basis index.
Solution 1
Demonstrated in the Solution_1 module of Flattening 2-D to 1-D.ana.
- Add Library..., Concatentation.ana
- Define K as: K := 1..size(I)*size(J)
- Define B as ConcatRows(A,I,J,K)
Solution 2
This is the more general solution, in that it generalized to flattening N-D arrays.
Demonstrated in the Solution_2 module of Flattening 2-D to 1-D.ana.
- Define K as: K := 1..size(I)*size(J)
- Define L := ['I','J','A']
- Define B := MdArrayToTable(A,K,L)[L='A']
Solution 3
Demonstrated in the Solution_3 module of Flattening 2-D to 1-D.ana.
- Define K as: K := 1..size(I)*size(J)
- Define B as: A[ @I = floor((@K-1)/size(J)+1), @J=Mod( (@K-1), size(J) )+1 ]
Unflattening a 1-D vector into a 2-D array
Given: Vector B, indexed by K. Indexes I,J. K has [[size](I)*size(J) elements.
Compute:Array A, indexed by I and J, containing all the elements of B.
Model file: Unflattening 2-D to 1-D.ana (contains setup and one possible solution)
Flattening a symmetrical 2-D matrix into a 1-D vector
Given: Array A[I,J], indexed by I and J.
Compute:1-D Array B[K], containing the elements of the upper-half triangle of the matrix, but without their duplicate lower-half elements.
Model file: Flattening triangular matrix.ana
The model file contains two possible solutions.
Multi-Step Hybrid Challenges
Counting Pairs
Contruct a table of all pairs of contributing factors that occur together, showing how many times the pair occurs and sorted by decreasing frequency.
Given: An array of booleans, B indexed by Failure and Factor, with a 1 indicating that the factor was a contributor to the given failure.
Compute: A table showing each pair of factors that occur together, with how many times that pair was observed to co-occur, and sorted by decreasing count.
Model file: Pair Counting.ana
The model contains the starting data, the desired result, and one possible solution broken down step-by-step. For the full challenge, find a way to compute the solution yourself without looking. If you aren't up for that, then take on the four individual steps in the solution as four smaller challenge exercises.
Tiered Billing
Some rate schedules such as tax rate schedules and utility billing schedules use a tiered rate scheme. For example, in California, Pacific Gas & Electric uses a 5-tiered billing system for residential customers for electricity usage. For a household assigned a baseline allotment of 200kWh per month, the rate pricing schedule (at the time of this writing) is as follows:
Tier 1 Tier 2 Tier 3 Tier 4 Tier 5 consumed (kWh) 0-200 201-260 261-400 401-600 over 600 rate ($/kWh) $0.12 $0.14 $0.29 $0.39 $0.44
If you were to consume 300 kWh of electricity in a given month, your bill would be determined as follows. The "first" 200kWh would be billed at $0.12/kWh, the next 60kWh at $0.14/kWh, and then the residual 40kWh at $0.29/kWh. The bill would thus be:
- 0.12*200 + 0.14*60 + 0.29*40 = $38
Given: A rate schedule with usage limits for each tier; an actual usage amount.
Compute: Usage amount that falls into each tier, and total price to be billed.
The model file contains a rate schedule and an example usage amount. A sample solution is provided. (A second solution is also provided that uses a function new to Analytica 4.3).
Model file: Tiered Billing.ana
Enable comment auto-refresher