Guide in solving spoj GiveAway - binary-search-tree

I was unable to solve the problem Spoj Give away after thinking for a while and searching for help I came to know that it involves AVL Tree + Segement Tree . Since I haven't used this data Structure yet a detail explanation of Working code and the approach is much encouraged.
Link : http://www.spoj.com/problems/GIVEAWAY/

In SPOJ GIVEAWAY, the problem requires you to address a lot of queries and whenever we have to handle more than one queries, SQRT decomposition technique or Segment tree comes forward. I would suggest you to solve the problem using SQRT decomposition method. If you're new to both the methods, you can watch tutorials or some documentation on the internet. I'm providing some links I hope it'll help you ;)
SQRT Decomposition
Segment Tree

Related

Best way to implement a primal heuristic that fixes certain Variables

I am using PySCIPOpt and have a MIP with some quadratic constraints (works). Now, I want to implement a Primal Heuristic (should run once before presolving), that fixes certain Variables and optimizes afterwards.
Ìn pseudo-code something like:
For x in ToFIX:
model.fixVar(x, my_guess(x))
model.optimize()
*Any found solution is used as solution of the original problem*
For x in ToFIX:
model.unFixVar(x)
I worked around that problem by creating a second model, solving that, identifying the variables by their name and using model.trySol().
This mostly works but is slow and certainly not the way it is meant to be implemented.
Any hint, which functionalities to use is appreciated.
sorry this took a while to answer.
What you want to implement is a sub-scip heuristic. This is certainly possible, but if you want to do it in PySCIPOpt you might have to wrap some missing methods from the C-API.
I suggest you take a look at heur_rens.c in the SCIP code. The methods you would need to wrap are probably SCIPcopyLargeNeighborhoodSearch and SCIPtranslateSubSol which should save you a lot of trouble. Please refer to the section extending the interface in the PySCIPopt Readme.

R quantstrat: Is it possible to use quantstrat to create complex rules?

I went over demos within quantstrat GitHub, also took a bootcamp course on how to use the package and read several posts and Q&As concerning signals and rules, however I am still failing to implement complex rules where thresholds are hit multiple times in a given sequence/chain in order to create my entry point.
By now I am pretty much convinced I yet don't understand the mechanics so I can stretch examples I went through to implement rules such as this:
Beforehand, my expectation is just to get a hint on how to build a cross-over or threshold rule that is 'aware' of its place in a workflow so I can expand it into more complex cases, such as the one in the image above.

how to get started with Optaplanner

I looked at the user guide http://docs.jboss.org/optaplanner/release/6.1.0.Final/optaplanner-docs/html_single/index.html#d0e2669
I looked at the example code from git, but still feel quite lost about how to model my planning problem (for example some special TSP variant, let's say TSPTW ). the way OptaPlanner user code is structured is that the user code is devided into several packages, and there is not a entire "end-to-end" flow of code, and as a developer I only see these "modules" , sort of. So as a result it's very difficult for me to figure out how to connect the dots, and how these "dots" interact together to accomplish the overall algorithm task.
the section in the link above "Model your planning problem " sort of touches on the above question I have, but still seems out of sync with the example code, so it's difficult to figure out how the current examples work, also it's difficult for me to see how to add my own special logic for the TSP variant .
is there a better "code walk-through" document to show the usage of the framework?
thanks
Yang
Take a look here this guy create a simple optaplanner project to test some concepts and learn how to use it.

How to go about learning R-tree?

I'm currently taking part in a "Data modeling" course. And for my final project, I need to make a research about "R-tree index for spatial searching". However, I'm not at all familiar with many concepts concerning the subject matter (spatial data, multi-dimentional data,...).So, I read the wiki and as I encountered new concepts, I tried to learn them on the way.
However, I don't think this top-down approach is a very efficient way to go about this. Thus, I'd really appreciate it if anybody can suggest a way/lists of things that I need to read up in advance, in order to understand R-tree, and hopefully make some kind of implementation out of it.
Start with the original paper and check out (shameless self promotion) a basic R-Tree implementation in Java.

How can I decide when to use linear programming?

When I look at optimization problems I see a lot of options. One is linear programming. I understand in abstract terms how LP works, but I find it difficult to see whether a particular problem is suitable for LP or not. Are there any heuristics that can help guide this decision?
For example, the work described in Is there a good way to do this type of mining? took weeks before I saw how to structure the problem correctly. Is it possible to know "in advance" that problem could be solved by LP, without first seeing "how to phrase it"?
Is there a checklist I can use to decide whether a problem is suitable for LP? Is there a standard (readable) reference for this topic?
Heuristics (and/or checklists) to decide if the problem at hand is really a Linear Program.
Here's my attempt at answering, and I have also tried to outline how I'd approach this problem.
Questions that indicate that a given problem is suitable to be formulated as an LP/IP:
Are there decisions that need to be taken regularly, at different time intervals?
Are there a number of resources (workers, machines, vehicles) that need to be assigned tasks? (hours, jobs, destinations)
Is this a routing problem, where different "points" have to be visited?
Is this a location or a "layout" problem? (Whole class of Stock-cutting problems fall into this group)
Answering yes to these questions means that an LP formulation might work.
Commonly encountered LP's include: Resource allocation.: (Assignment, Transportation, Trans-shipment, knapsack) ,Portfolio Allocation, Job Scheduling, and network flow problems.
Here's a good list of LP Applications for anyone new to LPs or IPs.
That said, there are literally 1000s of different types of problems that can be formulated as LP/IP. The people I've worked with (researchers, colleagues) develop an intuition. They are good at recognizing that a problem is a certain type of an Integer Program, even if they don't remember the details, which they can then look up.
Why this question is tricky to answer:
There are many reasons why it is not always straightforward to know if an LP formulation will cut it.
There is a lot of "art" (subjectivity) in the approach to modeling/formulation.
Experience helps a lot. People get good at recognizing that this problem can be "likened" to another known formulation
Even if a problem is not a straight LP, there are many clever master-slave techniques (sub-problems), or nesting techniques that make the overall formulation work.
What looks like multiple objectives can be combined into one objective function, with an appropriate set of weights attached.
Experienced modelers employ decomposition and constraint-relaxation techniques and later compensate for it.
How to Proceed to get the basic formulation done?
The following has always steered me in the right direction. I typically start by listing the Decision Variables, Constraints, and the Objective Function. I then usually iterate among these three to make sure that everything "fits."
So, if you have a problem at hand, ask yourself:
What are the Decision Variables (DV)? I find that this is always a good place to start the process of formulation. How many types of DV's are there? (Which resource gets which task, and when should it start?)
What are the Constraints?
Some constraints are very readily visible. Others take a little bit of teasing out. The constraints have to be written in terms of your decision variables, and any constants/limits that are imposed.
What is the Objective Function?
What are the quantities that need to be maximized or minimized? Note: Sometimes, it is not clear what the objective function is. That is okay, because it could well be a constraint-satisfaction problem.
A couple of quick Sanity Checks once you think your LP formulation is done:
I always try to see if a trivial solution (all 0s or all big
numbers) is not part of the solution set. If yes, then the
formulation is most probably not correct. Some constraint is
missing.
Make sure that each and every constraint is "related"' to
the Decision Variables. (I occasionally find constraints that are
just "hanging out there." This means that a "bookkeeping constraint"
has been missed.)
In my experience, people who keep at it almost always develop the needed intuition. Hope this helps.