I have a huge "sheet application", in one row, and one of the fields, called "price" literally has 433 "IF" statements in the formula bar. Basically, there's a lot of options, and based on them, I have to change the price in the field (if user picks option 1, set price 1, and so on). I was just wondering if there was a more sane way of writing that, because
it's seriously tiring and incomprehensible
it usually doesn't even work because I have to throw a IF(AND()) and IF(OR()) from time to time which breaks my workflow, and makes debugging impossible.
Any help greatly appreciated.
instead of AND you can use multiplication * and instead of OR use sum + - that way you can easily use ARRAYFORMULA if needed. also have a look here: https://webapps.stackexchange.com/q/123729/186471 for alternatives
update
try this instead of your pstebin formula:
=ARRAYFORMULA(IFERROR(IF((D24<>"")*(E24=""),
VLOOKUP(D24, {data!A:B; data!F1:G23}, 2, 0),
VLOOKUP(D24&E24, {{data!F24:F37&data!F50; data!F28&data!F49},
{data!I23; data!I23; data!I23; data!I23:I29;
data!I29; data!I29; data!I29; data!I30; data!G28}}, 2, 0))))
and data sheet:
ML and DLib beginner here, so please excuse me if the question meanders a little.
I've been looking at some of the example projects in the Dlib documentation, and roughly understand the example found here:
http://dlib.net/dnn_introduction_ex.cpp.html
The example looks straightforward, enough for me to grasp how to compile a working example.
What I would like to do however, is attempt to use a loss_mean_squared_multioutput layer in the place of the loss_multiclass_log found in the example, as I think it will be a better fit for my model.
To give a brief, high level description of the problem I have, it's a regression problem with 5 outputs. On training, I would like to feed in an 'image', and train the network on how valuable any one of those 5 outputs might be, in relation to that input.
So for example, if I feed in (pseudocode):
input = [1, 0, 0, 0, 0, 0, 0, 0]
I would like the output to produce something like this
output = [0, 0.1, 0, 0.5, -1]
With that in mind, if the labels for loss_mean_squared_multioutput are matrix<float>, is this roughly how I should go about using it?:
loss_multiclass_log<fc<5...
trainer.train([list of matrix<float>], [list of matrix<float 1, 5> = 0, 0.1, 0, 0.5, -1]);
Also, if this all seems totally misguided and nonsensical to you after reading through, by all means, please correct me. I would appreciate the guidance.
To help out anyone who might have wondered the same thing, there's an example in the Dlib test suite -
https://github.com/davisking/dlib/blob/master/dlib/test/dnn.cpp#L2343
The idea of how to use it is mostly right, but ensure that where you have the following:
loss_multiclass_log<fc<5...
You need to use a single column matrix in the trainer, like this:
// Use "matrix<float 5, 1>", not "matrix<float 1, 5>"...
trainer.train(input..., [list of matrix<float 5, 1>...);
Hope that helps.
Can anybody help me understand how to apply a GA in timetabling?
Right now I understand the steps of a GA, but don't know how to implement them in my project.
Can somebody guide me? If there is any pseudocode or links to help me it will be very much appreciated.
This is my university project. I'm not asking for working code, just some idea n pseudocode on how to implement it.
Thanks in advance!
Take a look at Solving Timetable Problem by Genetic Algorithm and Heuristic Search Case Study: Universitas Pelita Harapan Timetable
Transform the problem into a integer representation using an array to represent the chromosomes in the population.
Example {1, 2 , 5, 3, 4, 6, 7, 5}
The index in the array represents courses and the number at each index represents the time slot that the course is assigned to on a day. Random populations can then be created and evaluated based on a fitness function that would take into consideration the students associated with each course, course size, and any other constraint that may be present. I have used this approach to solving a university final examination timetable and it has worked well.
We have a NextInterestDate property on a rhinomock that we initially set to 31/03/2000. This property is accessed and this date is used in the processing of interest calculations for March. Once the March processing is complete the property needs to be updated to be the next chronological interest date which is 30/04/2000.
The next time around the loop we want the mock to return the April end date when the NextInterestDate is accessed on the mock. In short a new date is recalculated each time around the loop and the property on the mock is reassigned with the new date.
This means that the date on the mock must be updated each time around the loop.
What we are seeing however is that the date remains at 31/03/2000 rather than be updated correctly.
We are relatively new to RhinoMocks but we much prefer it to NMocks, albeit there seems to be a steep learning curve.
To solve this problem the previous developer used NMock and used a clonable class that she utilised NMocks ...Return.CloneOF which allowed the mock to be able to update the mock and supply the new value.
We just can't seem to figure out the correct syntax / or a way to achieve this.
Many thanks in advance Regards Colin
You can achieve that with .WhenCalled method. This might seem dodgy, but does exactly the thing you want to:
var expectedDates = new[]
{
new DateTime(2000, 3, 31),
new DateTime(2000, 4, 30),
new DateTime(2000, 5, 31),
};
var invocationsCount = 0;
service.Expect(s => s.NextInterestDate)
.WhenCalled(m => m.ReturnValue = expectedDates[invocationNumber++])
.Return(default(DateTime));
What code above does is simply takes next element from expectedDates array with each successive invocation. The strange call to Return(default(DateTime)) at the end must be there (otherwise Rhino will complain). You shouldn't worry tho - fake return value is ignored when we already specify one with WhenCalled.
Note that you need to be careful with code like one above (incrementing counters, arrays - all that in mock setup). Readability is not the greatest, so having proper variables/test method naming is crucial.
In Mathematica I have a list of point coordinates
size = 50;
points = Table[{RandomInteger[{0, size}], RandomInteger[{0, size}]}, {i, 1, n}];
and a list of cluster indices these points belong to
clusterIndices = {1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1};
what is the easiest way to split the points into two separate lists based on the clusterIndices values?
EDIT:
The solution I came up with:
pointIndices =
Map[#[[2]] &,
GatherBy[MapIndexed[{#1, #2[[1]]} &, clusterIndices], First],
{2}];
pointsByCluster = Map[Part[points, #] &, pointIndices];
It there a better way to do this?
As #High Performance Mark and #Nicholas Wilson said, I'd start with combining the two lists together via Transpose or Thread. In this case,
In[1]:= Transpose[{clusterIndices, points}]==Thread[{clusterIndices, points}]
Out[1]:= True
At one point, I looked at which was faster, and I think Thread is marginally faster. But, it only really matters when you are using very long lists.
#High Performance Mark makes a good point in suggesting Select. But, it would only allow you to pull a single cluster out at a time. The code for selecting cluster 1 is as follows:
Select[Transpose[{clusterIndices, points}], #[[1]]==1& ][[All, All, 2]]
Since you seem to want to generate all clusters, I'd suggest doing the following:
GatherBy[Transpose[{clusterIndices, points}], #[[1]]& ][[All, All, 2]]
which has the advantage of being a one liner and the only tricky part was in selecting the correct Part of the resulting list. The trick in determining how many All terms are necessary is to note that
Transpose[{clusterIndices, points}][[All,2]]
is required to get the points back out of the transposed list. But, the "clustered" list has one additional level, hence the second All.
It should be noted that the second parameter in GatherBy is a function that accepts one parameter, and it can be interchanged with any function you wish to use. As such, it is very useful. However, if you'd like to transform your data as your gathering it, I'd look at Reap and Sow.
Edit: Reap and Sow are somewhat under used, and fairly powerful. They're somewhat confusing to use, but I suspect GatherBy is implemented using them internally. For instance,
Reap[ Sow[#[[2]], #[[1]] ]& /# Transpose[{clusterIndices, points}], _, #2& ]
does the same thing as my previous code without the hassle of stripping off the indices from the points. Essentially, Sow tags each point with its index, then Reap gathers all of the tags (_ for the 2nd parameter) and outputs only the points. Personally, I use this instead of GatherBy, and I've encoded it into a function which I load, as follows:
SelectEquivalents[x_List,f_:Identity, g_:Identity, h_:(#2&)]:=
Reap[Sow[g[#],{f[#]}]&/#x, _, h][[2]];
Note: this code is a modified form of what was in the help files in 5.x. But, the 6.0 and 7.0 help files removed a lot of the useful examples, and this was one of them.
Here's a succinct way to do this using the new SplitBy function in version 7.0 that should be pretty fast:
SplitBy[Transpose[{points, clusterIndices}], Last][[All, All, 1]]
If you aren't using 7.0, you can implement this as:
Split[Transpose[{points, clusterIndices}], Last[#]==Last[#2]& ][[All, All, 1]]
Update
Sorry, I didn't see that you only wanted two groups, which I think of as clustering, not splitting. Here's some code for that:
FindClusters[Thread[Rule[clusterIndices, points]]]
How about this?
points[[
Flatten[Position[clusterIndices, #]]
]] & /#
Union[clusterIndices]
I don't know about 'better', but the more usual way in functional languages would not be to add indices to label each element (your MapIndexed) but instead to just run along each list:
Map[#1[[2]] &,
Sort[GatherBy[
Thread[ {#1, #2} &[clusterIndices, points]],
#1[[1]] &], #1[[1]][[1]] < #2[[1]][[1]] &], {2}]
Most people brought up in Lisp/ML/etc will write the Thread function out instantly is the way to implement the zip ideas from those languages.
I added in the Sort because it looks like your implementation will run into trouble if clusterIndices = {2[...,2],1,...}. On the other hand, I would still need to add in a line to fix the problem that if clusterIndices has a 3 but no 2, the output indices will be wrong. It is not clear from your fragment how you are intending to retrieve things though.
I reckon you will find list processing much easier if you refresh yourself with a hobby project like building a simple CAS in a language like Haskell where the syntax is so much more suited to functional list processing than Mathematica.
If I think of something simpler I will add to the post.
Map[#[[1]] &, GatherBy[Thread[{points, clusterIndices}], #[[2]] &], {2}]
My first step would be to execute
Transpose[{clusterIndices, points}]
and my next step would depend on what you want to do with that; Select comes to mind.