# Least squares minimisation

I was wondering if I could use Kangaroo to do least squares minimisation.

My setup:

I have a surface that I can scale with a scaling factor. I want to find the scaling factor that minimises the residuals between the area of this surface and a target area. I created a Length goal between the two area values, so the line length is the discrepancy and the energy of the goal is the square of the discrepancy.

I’m not sure how to proceed, however. I think the issue is that I don’t have a target for the point representing the scaling factor. Any thoughts?

Yes, Kangaroo does minimise the sum of squared differences from target lengths, but unless I’m misunderstanding your description, your problem doesn’t sound like something that requires Kangaroo, or even any minimisation process at all - you can calculate that scaling factor directly in one go.

As Daniel said you don’t need K2 for that (but it can do it).

Given the opportinity assume (general case) that you don’t know the Surface (is implicitly created with some way). Imagine a Loop where you exit only if something happens.

Start scaling using some value (SV) and a step (S) and compare current (C) to target (T) area. If the Math.Abs(C-T) < some tolerance then exit. If C < T set SV += S and continue. If C >T set S /= 2.0 and SV -= S and continue.

That’s the core logic of something called bounce solver where the step is divided by 2 each time that an “overflow” happens.

Notify if you need a C# that does that.

Hello @DanielPiker yes you’re right. Perhaps it was too simple of an example. Here’s an updated problem:

Now I have two scaling factors, and two targets that I want to reach simultaneously. I want to know the values for these factors that will get me as close as possible to my targets.

In any case, I’m still interested in the mechanics of using Kangaroo for doing least squares.

Also, apologies for not attaching a file before.

leastSquares.gh (23.7 KB)

Hello Peter, that’s a good approach, I didn’t know about bounce solvers! I like the way it steps backwards with a half step.

At the moment, I’m more interested in how I would translate this problem to a set of Kangaroo goals, but thanks!

Well … think the following: You have rnd cutting curves and some x/y Intervals that define a flat (or not) start Surface (plus some target Area in mind). That Surf is trimmed (if possible) with the cutting curves with a random way (depends on the relative position of the cutters VS the Surface). This means that you don’t know the “final” Suface at all.

Or … given the grey BrepFaces … and some start “frame” location (or not) … find the “frame” (Red) where the sum of all areas is a given value.

Or imagine a closed complex Brep/Mesh collection and you want to define a Plane (per Brep/Mesh) where all the neg Z splitted Breps/Meshes have a given target volume (the flotation puzzle). Like (spot that the hight of Box in T is not the same with h - or any other - etc etc):

That sort of stuff.

1 Like

With a target aspect ratio and area it’s still a direct calculation.

Assuming this is an over simplified example, and the real problems you want to tackle are things without such a direct solution - @PeterFotiadis’ flotation one above is a good example.

Here’s a previous discussion about just that problem where I show how to solve it with both a Kangaroo goal and a sampling approach:

For the Kangaroo version it’s also solving for the orientation, which is a more complex problem, but you could also make a goal to solve the simple 1d case of finding a height.

Kangaroo doesn’t work like Galapagos where you can just pick some sliders to adjust and a target value to minimise from the canvas - If your problem can’t be set up with existing goals then new goals need to be coded (but the advantage is that it can quickly solve types of problems that would be extremely slow or impossible to solve with something like Galapagos).

2 Likes

I think I have something with no direct solution, at least from my sources. This is all very informative stuff, the floatation problem you linked looks very interesting! I’ll have a read through. Thanks everyone!

Sorry to bother @DanielPiker but out of interest do you think there’s a direct solution for a projected area + aspect ratio combo instead? Something like this:

I’m sorry for leaving out bits of information. I was just trying to do least squares using a simple example but it would be great if there was a direct solution.

leastSquares.gh (25.0 KB)