I am working on a program and I'm considering which data structure is most appropriate.
I have the following class -
public class item{
String name;
int value;
public item(String name, int value){
this.value = value;
this.name = name;
}
}
I have two cases -
To store a large amount of items and retrieve the top n amount of items while new items are being added and old items have their values updated.
To store all the items and retrieve top n amount of items without any new items being added or old items being updated.
I'm trying to achieve the best case time complexity for both cases.
I have thought about storing all items in a map for look up by their name, and in a maxheap, then removing n number of items from the top, storing them, then adding them back to the stack.
I've looked at priority queues and binary search trees as options, would either of these be more suitable? or are they any other structures that would fit either of these cases?
If name is unique you can use IDictionary. It is pretty fast for this uses
In a survey of 100 people, I am asking each person to choose between product A and product B. I ask each person this question 3 times, but each time I present a different set of products. Say, first time, Person 1 is asked to choose between 'Phone 1' and 'Phone 2', given certain attributes of each phone. The second time the choice is again 'Phone 1' vs. 'Phone 2', but a different set of attributes for each phone.
A person is presented three attributes associated with the two phone alternatives every time the question is asked. So, each time between Phone 1 and Phone 2, the attributes of the phone such as cost, memory and camera pixels are presented so that user can choose which set of attributes is most attractive, Phone 1's or Phone 2's.
Overall, 3*100 = 300 responses; 3 responses per person. Each time the attributes cost, memory and camera pixels presented and user asked to choose the feature set they prefer.
My goal is to analyze how users value features of a phone vs. cost of the phone.
In this scenario, can I use a MNL - even though each time I asked the person a question, I only presented two choices ? My understanding is that MNL is sued when (a) there are multiple choices and (b) the choice options do not change across observations, i.e. each person is asked to choose between multiple products, say A, B, C and A, B, C do not change across observations.
In the scenario described above, the two choices varied across the three times the same person was asked the question ? If not MNL, should I rather create a binary logit model given that user only had to choose between two options when the question was asked (even though he was asked the question three times)? If I can use binary logit, should I be concerned that the choice set of products change across observations ? or should I let the attributes defined in each of the rows address the differences in product choices across observations.
I have setup the data as follows (thinking I can do MNL but may be I should set it up differently and use another modeling approach?):
I am working on designing and analyzing similar survey but mine is related to transportation. I am at the beginning level and I am still new to the whole concept, however I will give you an advice and reference maybe it is helpful.
First point: I have come cross 3 models as following from a useful video on YouTube:
MNL refers to Multinomial Logit Model. MNL is used with
alternative-invariant regressors (for example salary of participant
in the survey, or his/her gender …).
Conditional logit model is used with alternative-invariant (gender,
salary, education level …) and alternative-variant regressors (cost
of the product, memory, camera pixel …)
Mixed logit model which uses random parameters. It is also used with
alternative-invariant (gender, salary, education level …) and
alternative-variant regressors (cost of the product, memory, camera
pixel …)
Note regarding alternative-invariant and alternative-variant regressors:
The gender of person participating in the survey will NOT vary between Product A or Product P, so it is alternative-invariant regressor. While price of product could vary between Product A and Product B so it is called alternative-variant regressors.
Based on above I assume you need to use conditional logit model or mixed logit model.
For me I couldn’t find a special function in R for the conditional logit model or mixed logit model. The same mlogit function is used, refer to the examples below for the help of mlogit package:
a pure "multinomial model"
summary(mlogit(mode ~ 0 | income, data = Fish))
a pure "conditional" model
summary(mlogit(mode ~ price + catch, data = Fish))
a "mixed" model
m <- mlogit(mode ~ price+ catch | income, data = Fish)
summary(m)
same model with charter as the reference level
m <- mlogit(mode ~ price+ catch | income, data = Fish, reflevel = "charter")
From the examples above, I think (but NOT sure) that in the Manual of mlogit package, they refer to mixed logit when you used both alternative-invariant and alternative-variant regressors. While conditional model when you have only alternative-variant regressors. On the other hand, multinomial model when you have only multinomial alternative-invariant regressors.
Second point: There is something called “panel data” when you are asking the same person to choose one product for each choice-set. Same person here means that in your model you are taking into consideration, the gender, the salary, the education level … which they will stay the same for the same person. Check this: https://en.wikipedia.org/wiki/Panel_data
To use panel techniques please refer to help in mlogit package in R. I am quoting from it the following:
“panel only relevant if rpar is not NULL and if the data are repeated observations of the same unit ; if TRUE, the mixed-logit model is estimated using panel techniques”
So in my understanding, if you want to use the panel techniques you have to use random draws because panel will be true and rpar will not be NULL.
Moreover, for example about using the panel data, please refer to the below example from “Estimation of multinomial logit models in R : The mlogit Packages” by Yves Croissant
data("Train", package = "mlogit")
Tr <- mlogit.data(Train, shape = "wide", varying = 4:11, choice = "choice", sep = "_", opposite = c("price", "time", "change", "comfort"), alt.levels=c("A", "B"), id.var ="id")
Train.ml <- mlogit(choice ~ price + time + change + comfort, Tr)
Train.mxlc <- mlogit(choice ~ price + time + change + comfort, Tr, panel = TRUE, rpar = c(time = "cn", change = "n", comfort = "ln"), correlation = TRUE, R = 100, halton = NA)
Train.mxlu <- update(Train.mxlc, correlation = FALSE)
I hope that is useful to you.
In Magento 1.7, is there any way to show Flat Rate Shipping only if chart is over a certain amount?
By rule the only option seems to be free shipping, but my goal is to have Flat Rate if sub total in chart is xx (if sub total is lower, normal rates will be used).
Not by default. It's easy to extend the flat rate shipping carrier method though to do this. Simply create a new module, extend the flat rate shipping system.xml so that there is a new option for minimum cart price (this will allow you to configure it through admin). Extend the following class Mage_Shipping_Model_Carrier_Flatrate and override the collectRates method to check the cart total vs the admin config value. Return false if the cart value is not enough and it will not show up as an option, otherwise it will return the cost set in the admin.
Edit: Something like the following would do it. Extend the class, don't mod the core!
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
// Get The Minimum Order Value From Admin Config & Compare To Cart Subtotal (Base Prices)
if (!$this->getConfigFlag('minimum_order') || $request->getBaseSubtotalInclTax() < $this->getConfigFlag('minimum_order')) {
return false;
} else {
return parent::collectRates($request);
}
}
How can I add an optional 10% fee over the whole order? I want my customers to be able to choose between no fee or a optional 10% fee (with some advantages for them).
At this moment, I've tried to represent this by enabling "Free shipping" and "Flat rate".
So at System/Configuration/Shipping Methods I've put the following values:
- 'Handling Fee' => '0.10'
- 'Calculate Handling Fee'=>'Percent'
- 'Type' => 'Per order'
As a result, the generated orders have a '10 cents' fee, instead of a percentage over its value.
How does can I represent this with Magento? Should I use Flat rates?
PS: I'm testing it only with back-end, should I face any difference compared with end user?
I have achieved what I was looking for by editing app/code/core/Mage/Shipping/Model/Carrier/Flatrate.php by adding the following method
protected function _getPerorderPrice($cost, $handlingType, $handlingFee){
if ($handlingType == self::HANDLING_TYPE_PERCENT) {
$val = ($cost * $this->_numBoxes * $handlingFee);
return $val;
}
return ($cost * $this->_numBoxes) $handlingFee;
}
and by changing the line 78 to:
$shippingPrice = $request->getPackageValue();
I'm not sure if this will cause any side-effect, so please comment if you identify any :)
At this moment everything seems to be working fine.
I have a product list and want to send them to view part by part in every page refresh, 5 per page. When all parts have been displayed start over at the beginning of the list on the next refresh. If the product list count is not dividend of 5, for example, count = 23, to come back to first. i.e. to take 15-20, then 20-2 (21, 22, 23, 1 and 2). Then, 3-8 and to continue.
I cannot use the paging rule as:
ViewBag.Result = db.Products.Skip( ( pageNo - 1 ) * 5).Take( 5 ).ToList();
Because, I will send that 5 products from all actions to views and update in every refreshing site.
I have an idea that to take random index and start, but I need to know the last sending index and in every refresh, send it to controller's current action. How can I accomplish this?
You need to keep the last sending index between the HTTP calls. Since HTTP is stateless, you need a medium to keep it in between. You may use Session Variable do to that.
Session["LastIndexSent"]= 5;
return View(someITemList);
Also try to avoid sending such Results in ViewBag. Using dynamic stuff like ViewBag/ViewData to transfer data between controller and view makes your code ugly.Use strongly typed approach.
List<Product> productList=GetProductListFromSomeWhere();
return View(productList);
And in your strongly typed view,
#model List<Product>
#foreach(var prod in Model)
{
<p>#prod.Name</p>
}