[Canonical page](<https://docs.dhtmlx.com/planning-engine/net/resources-and-assignment/evaluating-alternatives/>)
[Full documentation index](<https://docs.dhtmlx.com/planning-engine/llms.txt>)


# Evaluating Alternatives

This guide shows how to compare several possible edits in one call and pick the best one, for example to decide which resource should take a task. It covers building candidates, scoring them with and without leveling, reading the metrics, handling candidates that fail, scoring on several threads and committing the winner.

## How evaluation works

[`Project.Evaluate`](<https://docs.dhtmlx.com/planning-engine/net/api/project-evaluate.md>) takes a list of **candidates**. A [`Candidate`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/candidate.md>) is an `Id` plus a [`SchedulingChanges`](<https://docs.dhtmlx.com/planning-engine/net/api/core/changes/scheduling-changes.md>), the same change set you'd pass to [`Apply`](<https://docs.dhtmlx.com/planning-engine/net/api/project-apply.md>). Each candidate is one alternative: "give task B to dev2", "split the review between two people", "add a link". The call scores every candidate against the project's accepted baseline and returns an [`EvaluateResult`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-result.md>). The project doesn't change, just as with [`Preview`](<https://docs.dhtmlx.com/planning-engine/net/api/project-preview.md>).

Every candidate goes through the same run on a disposable copy of the project:

1. Copy the reference state. That's the accepted baseline, or its cleared copy with [`ClearFirst`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-options.md#clearfirst>) (see [Options](<https://docs.dhtmlx.com/planning-engine/net/resources-and-assignment/evaluating-alternatives.md#options>)).
2. `Apply` the candidate's changes.
3. Call [`Solve(null, …)`](<https://docs.dhtmlx.com/planning-engine/net/api/project-solve.md>) and measure the **scheduled** stage.
4. If [`EvaluateOptions.Level`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-options.md#level>) is set, call [`Level`](<https://docs.dhtmlx.com/planning-engine/net/api/project-level.md>) with those options and measure the **leveled** stage.

You'd run the same sequence to commit the candidate for real. Each candidate starts from the reference state, never from the previous candidate, and both stages are compared with the same baseline.

The result has three parts:

- [`Baseline`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-result.md#baseline>): the metrics of the reference state itself. All its deltas are 0.
- [`Candidates`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-result.md#candidates>): one [`CandidateResult`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/candidate-result.md>) per candidate, in input order. It holds `Scheduled` metrics, `Leveled` metrics (only with `Level`) and a `Cost`.
- [`BaselineId`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-result.md#baselineid>): the token of the accepted baseline the candidates were scored against.

Each stage is described by a [`Metrics`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/metrics.md>) object: the project finish and the finish of each focus task, resource overload, conflicts, cycles and a single `Cost` number. Every measurement is an absolute `Value` plus a `Delta` against the baseline, so you can show either one.

Errors come in two levels. Problems with the call itself throw and fail the whole call:

- no solved baseline: [`ProjectException`](<https://docs.dhtmlx.com/planning-engine/net/api/core/errors/project-exception.md>) with [`Code == ProjectError.NoBaseline`](<https://docs.dhtmlx.com/planning-engine/net/api/core/errors/project-exception.md#projecterror>)
- a stale `BaselineId` in the options or in any candidate: `ProjectError.StaleBaseline`
- an unknown focus task, or leveling options that don't fit the baseline: [`InputException`](<https://docs.dhtmlx.com/planning-engine/net/api/core/errors/input-exception.md>)

Everything else fails only its own candidate. That candidate gets an `Error` and no metrics, and the other candidates are scored as usual.

`Evaluate` reports scores, not dates. The result has no task updates and no overallocation windows. Once you've picked a candidate, commit it yourself with `Apply` and `Level`, or look at its dates with `Preview`.

## Comparing candidates

All examples on this page use the same small project: tasks A and B, two working days each, both starting on Monday, August 10, 2026, and both assigned to `dev1`. The resource `dev2` is idle, and `dev1` is overloaded for two days.

### Candidates

Build each alternative as a [`Candidate`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/candidate.md>) and pass them all to [`Evaluate`](<https://docs.dhtmlx.com/planning-engine/net/api/project-evaluate.md>). An empty change set is a useful candidate too: it scores "leave the plan as it is" next to the other options.

```csharp
using Plen;

var monday = Moment.FromYmdHm(2026, 8, 10, 8, 0) ?? throw new InvalidOperationException("invalid date");
var data = new ProjectData
{
    Tasks =
    [
        new TaskData("A") { StartDate = monday, Duration = 2.0 },
        new TaskData("B") { StartDate = monday, Duration = 2.0 },
    ],
    Resources = [new ResourceData { Id = "dev1" }, new ResourceData { Id = "dev2" }],
    Assignments =
    [
        new AssignmentData { Id = "aA", Task = "A", Resource = "dev1", Value = 1.0 },
        new AssignmentData { Id = "aB", Task = "B", Resource = "dev1", Value = 1.0 },
    ],
};
var project = new Project();
project.Solve(data, new SolveOptions());

// Candidate 1: keep the plan as it is.
var stay = new Candidate { Id = "stay", Changes = new SchedulingChanges() };

// Candidate 2: give task B to dev2.
var toDev2 = new SchedulingChanges();
toDev2.Assignments.Update.Add(new AssignmentUpdate("aB") { Resource = Patch<Id>.Of("dev2") });
var dev2 = new Candidate { Id = "dev2", Changes = toDev2 };

var result = project.Evaluate([stay, dev2], new EvaluateOptions { Focus = ["B"] });

Console.WriteLine($"baseline: cost {result.Baseline.Cost}");
foreach (var candidate in result.Candidates)
    Console.WriteLine($"{candidate.Id}: cost {candidate.Cost}");
```

```text
baseline: cost 2
stay: cost 2
dev2: cost 0
```

Without leveling, only the scheduled stage is scored. `stay` keeps both tasks on `dev1` and costs 2, two resource-days of overload. `dev2` removes the overload and costs 0.

The candidate `Id` is echoed back so you can match results to inputs. Ids aren't checked for uniqueness, and results keep input order anyway. See the [`Candidate` reference](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/candidate.md>).

### Options

[`EvaluateOptions`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-options.md>) has four properties, all optional:

- [`Level`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-options.md#level>): a [`LevelingOptions`](<https://docs.dhtmlx.com/planning-engine/net/api/core/resources/leveling-options.md>) levels each candidate after scheduling it and adds the leveled stage. `null` (the default) scores the scheduled stage only.
- [`Focus`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-options.md#focus>): the tasks to report finish dates and focus overload for. There's no implicit "modified task", so list the tasks your candidates are about.
- [`BaselineId`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-options.md#baselineid>): a baseline token. The call throws `ProjectException` with [`ProjectError.StaleBaseline`](<https://docs.dhtmlx.com/planning-engine/net/api/core/errors/project-exception.md#projecterror>) when the token no longer names the accepted baseline. Each candidate's [`Changes.BaselineId`](<https://docs.dhtmlx.com/planning-engine/net/api/core/changes/scheduling-changes.md#baselineid>) is checked the same way.
- [`ClearFirst`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-options.md#clearfirst>): score against a copy with the in-scope `LevelingDelay` and `Pauses` cleared.

Leveling changes the picture. Without it, an overloaded plan looks bad on overload. With it, the overload turns into delay, which is what the user gets after committing and leveling. The rest of the examples share two helpers, `SolvedProject` and `Reassign`:

```csharp
using Plen;

var project = SolvedProject();
Candidate[] candidates =
[
    new() { Id = "stay", Changes = new SchedulingChanges() },
    new() { Id = "dev2", Changes = Reassign("aB", "dev2") },
];
var options = new EvaluateOptions
{
    Level = new LevelingOptions(),
    Focus = ["A", "B"],
};
var result = project.Evaluate(candidates, options);

foreach (var candidate in result.Candidates)
{
    if (candidate.Leveled is not { } leveled)
        continue;
    if (leveled.Metrics.Finish.Project is { } finish)
        Console.WriteLine($"{candidate.Id}: project ends {finish.Date} ({finish.Delta:+0;-0;+0} days), cost {candidate.Cost}");
    foreach (var task in leveled.Metrics.Finish.Tasks)
    {
        if (task.Finish is { } taskFinish)
            Console.WriteLine($"  {task.Id} ends {taskFinish.Date} ({taskFinish.Delta:+0;-0;+0})");
    }
}

// Tasks A and B, two days each, both on dev1 in the same week.
static Project SolvedProject()
{
    var monday = Moment.FromYmdHm(2026, 8, 10, 8, 0) ?? throw new InvalidOperationException("invalid date");
    var data = new ProjectData
    {
        Tasks =
        [
            new TaskData("A") { StartDate = monday, Duration = 2.0 },
            new TaskData("B") { StartDate = monday, Duration = 2.0 },
        ],
        Resources = [new ResourceData { Id = "dev1" }, new ResourceData { Id = "dev2" }],
        Assignments =
        [
            new AssignmentData { Id = "aA", Task = "A", Resource = "dev1", Value = 1.0 },
            new AssignmentData { Id = "aB", Task = "B", Resource = "dev1", Value = 1.0 },
        ],
    };
    var project = new Project();
    project.Solve(data, new SolveOptions());
    return project;
}

static SchedulingChanges Reassign(Id assignment, Id resource)
{
    var changes = new SchedulingChanges();
    changes.Assignments.Update.Add(new AssignmentUpdate(assignment) { Resource = resource });
    return changes;
}
```

```text
stay: project ends 2026-08-14 08:00 (+2 days), cost 2
  A ends 2026-08-12 08:00 (+0)
  B ends 2026-08-14 08:00 (+2)
dev2: project ends 2026-08-12 08:00 (+0 days), cost 0
  A ends 2026-08-12 08:00 (+0)
  B ends 2026-08-12 08:00 (+0)
```

For `stay`, leveling resolves the overload by pushing B two days later, so the cost is now the delay. For `dev2` there's nothing to level. Inside `Reassign`, `resource` is already an `Id`, so it converts to [`Patch<Id>`](<https://docs.dhtmlx.com/planning-engine/net/api/core/changes/patch.md>) directly. With a string literal, write `Patch<Id>.Of("dev2")` as in the first example.

`Level` runs with the options exactly as you pass them, so [`LevelingOptions.ClearFirst`](<https://docs.dhtmlx.com/planning-engine/net/api/core/resources/leveling-options.md>) (default `true`) re-levels everything from zero in each candidate. That's a pitfall after a previous `Level` run. The stored delays are already in the baseline finish, and they hide the overload the current assignments cause. A candidate that removes the overload then shows a negative finish delta, because leveling from zero drops the old delays.

[`EvaluateOptions.ClearFirst`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-options.md#clearfirst>) fixes this. It replaces the reference state with a copy that has the in-scope delays and pauses cleared, solved again. The baseline metrics and every candidate run then use that copy. The scope is [`Level.Scope.Tasks`](<https://docs.dhtmlx.com/planning-engine/net/api/core/resources/leveling-options.md#levelingscope>) when you set it, otherwise every task. It's independent of `LevelingOptions.ClearFirst`, so it also works without `Level`.

```csharp
using Plen;

var project = SolvedProject();
// The plan was leveled earlier: B carries a two-day leveling delay.
var leveled = project.Level(new LevelingOptions());

Candidate[] candidates = [new() { Id = "dev2", Changes = Reassign("aB", "dev2") }];
var options = new EvaluateOptions
{
    BaselineId = leveled.Schedule.BaselineId,
    Level = new LevelingOptions(),
    ClearFirst = true,
};
var result = project.Evaluate(candidates, options);

// The cleared baseline shows the overload that B's delay was hiding.
Console.WriteLine($"baseline overload: {result.Baseline.Overload.Total.Value}");
foreach (var candidate in result.Candidates)
{
    if (candidate.Leveled?.Metrics.Finish.Project is { } finish)
        Console.WriteLine($"{candidate.Id}: finish delta {finish.Delta:+0;-0;+0}");
}

// Tasks A and B, two days each, both on dev1 in the same week.
static Project SolvedProject()
{
    var monday = Moment.FromYmdHm(2026, 8, 10, 8, 0) ?? throw new InvalidOperationException("invalid date");
    var data = new ProjectData
    {
        Tasks =
        [
            new TaskData("A") { StartDate = monday, Duration = 2.0 },
            new TaskData("B") { StartDate = monday, Duration = 2.0 },
        ],
        Resources = [new ResourceData { Id = "dev1" }, new ResourceData { Id = "dev2" }],
        Assignments =
        [
            new AssignmentData { Id = "aA", Task = "A", Resource = "dev1", Value = 1.0 },
            new AssignmentData { Id = "aB", Task = "B", Resource = "dev1", Value = 1.0 },
        ],
    };
    var project = new Project();
    project.Solve(data, new SolveOptions());
    return project;
}

static SchedulingChanges Reassign(Id assignment, Id resource)
{
    var changes = new SchedulingChanges();
    changes.Assignments.Update.Add(new AssignmentUpdate(assignment) { Resource = resource });
    return changes;
}
```

```text
baseline overload: 2
dev2: finish delta +0
```

With `ClearFirst`, the baseline reports 2 resource-days of overload and the `dev2` candidate a finish delta of 0. Without it, the baseline has no overload and the same candidate shows a finish delta of -2. Both views are valid. `ClearFirst` is the one that matches a commit with `Level` and `LevelingOptions.ClearFirst`, which re-levels from zero.

A few details:

- `BaselineId`, candidate tokens, `Level` options and focus ids are all checked against the accepted state before anything is cleared. `ClearFirst` adds no new errors.
- A candidate's own `Changes.BaselineId` is dropped before its trial run, because the cleared copy has a different token. The result's `BaselineId` is still the accepted state's.
- When there's nothing to clear in scope, the output is the same as with `ClearFirst = false`.

The full property list is on the [`EvaluateOptions` reference](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-options.md>). For the leveling options themselves, see [Leveling resources](<https://docs.dhtmlx.com/planning-engine/net/resources-and-assignment/leveling-resources.md>).

### Metrics and cost

Every stage reports the same [`Metrics`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/metrics.md>):

| Property | What it measures |
|---|---|
| [`Finish.Project`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/metrics.md#finish>) | Latest end over scheduled tasks. `null` when the stage has no tasks. |
| `Finish.Tasks` | One [`TaskFinish`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/metrics.md#taskfinish>) per focus task, in focus order. |
| [`Overload.Total`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/overload.md>) | Overload over all resources. |
| `Overload.Resources` | Overload per resource, for every resource that's non-zero in the stage or the baseline. |
| `Overload.Focus` | Per focus task: the overload per resource in the buckets that intersect the task. |
| `Conflicts`, `Cycles` | How many conflicts and cycles the stage has. |
| `Cost` | One number for ranking, lower is better. |

Dates come as a [`Shift`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/metrics.md#shift>): `Date` plus `Delta`, the working time from the baseline finish in [`Config.DurationUnit`](<https://docs.dhtmlx.com/planning-engine/net/api/core/data/scheduling-config.md#durationunit>), positive when later. A task's delta is measured on its own calendar, the project's on the global calendar. Counts and overload come as an [`Amount`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/overload.md#amount>): `Value` plus `Delta`.

Overload is measured the same way the leveler sees it: for each time bucket, how far a resource's load goes over its capacity, summed over buckets. The unit is capacity × buckets, so on a day basis 2.0 means "two resource-days over". One bucket size (basis) applies to the baseline and every stage of every candidate: `Level.Basis` when you set it, otherwise the one derived from `Config.DurationUnit`. [`LoadProfile`](<https://docs.dhtmlx.com/planning-engine/net/resources-and-assignment/resource-load.md>) shows the same per-resource sum as excess load at that basis.

Focus overload shows where the trouble is. At the scheduled stage, it's the overload around the task. At the leveled stage, it's what leveling couldn't resolve there. A milestone has zero length and covers no bucket.

The leveled stage is a [`LeveledMetrics`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/leveled-metrics.md>): the same `Metrics` in its `Metrics` property, plus `Converged`, `Rounds` and `Windows`, the number of overallocation windows left after leveling.

`Cost` is one fixed heuristic:

```text
Cost = max(0, Finish.Project.Delta)
     + Overload.Total.Value
     + 1000 × (Conflicts.Value + Cycles.Value)
```

When the basis follows `DurationUnit`, a day of delay and a resource-day of overload weigh the same. An earlier finish doesn't earn a bonus. The 1000 penalty keeps any broken schedule, with conflicts or cycles, behind every valid one. [`CandidateResult.Cost`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/candidate-result.md>) is the cost of the last stage present: leveled when you level, scheduled otherwise. The baseline carries its own `Cost`.

If the heuristic doesn't fit your product, rank on the raw values instead. This method prints the per-resource overload change of each candidate's scheduled stage:

```csharp
using Plen;

static void PrintOverload(EvaluateResult result)
{
    foreach (var candidate in result.Candidates)
    {
        if (candidate.Scheduled is not { } scheduled)
            continue;
        foreach (var row in scheduled.Overload.Resources)
            Console.WriteLine($"{candidate.Id}: {row.Resource} overload {row.Overload.Value} ({row.Overload.Delta:+0;-0;+0} vs baseline)");
    }
}
```

For the project above, the `dev2` candidate prints a row for `dev1` with a delta of -2. The row is there because `dev1` was overloaded in the baseline, even though it's zero in the stage. Resources come in stage order, then resources that exist only in the baseline. See the [`Metrics` reference](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/metrics.md>) and the [`Overload` reference](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/overload.md>).

### Failed and cyclic candidates

A candidate can fail without failing the call. Three cases need handling:

- **Rejected.** [`Apply`](<https://docs.dhtmlx.com/planning-engine/net/api/project-apply.md>) rejects an operation in the change set (the result reports only the first rejection), or [`Solve`](<https://docs.dhtmlx.com/planning-engine/net/api/project-solve.md>) or [`Level`](<https://docs.dhtmlx.com/planning-engine/net/api/project-level.md>) fails on the candidate's input, for example on a leveling scope id the candidate removed. The result has an `Error`, a [`RejectedOperation`](<https://docs.dhtmlx.com/planning-engine/net/api/core/changes/rejected-operation.md>) with a `Path` and a `Message`. `Scheduled`, `Leveled` and `Cost` are `null`.
- **Cyclic.** The candidate creates a dependency cycle. `Scheduled` is measured on the unchanged dates the cyclic solve returns, and `Cycles.Value` is above 0. Leveling stops at round 0 on a cyclic plan, so `Leveled` is `null`. The cycle penalty pushes its cost over 1000.
- **Focus task removed.** When the candidate removes a focus task, that task's `Finish` and its focus overload `Resources` are `null`. The rest of the metrics are normal.

```csharp
using Plen;

var project = SolvedProject();

var cycle = new SchedulingChanges();
cycle.Links.Add.Add(new LinkAdd { Id = "ab", Source = "A", Target = "B" });
cycle.Links.Add.Add(new LinkAdd { Id = "ba", Source = "B", Target = "A" });
var removed = new SchedulingChanges();
removed.Tasks.Remove.Add("B");

Candidate[] candidates =
[
    new() { Id = "missing", Changes = Reassign("nope", "dev2") },
    new() { Id = "cycle", Changes = cycle },
    new() { Id = "removed", Changes = removed },
];
var options = new EvaluateOptions
{
    Level = new LevelingOptions(),
    Focus = ["B"],
};
var result = project.Evaluate(candidates, options);

foreach (var candidate in result.Candidates)
{
    if (candidate.Error is { } error)
    {
        Console.WriteLine($"{candidate.Id} rejected at {error.Path}: {error.Message}");
        continue;
    }
    if (candidate.Scheduled is not { } scheduled)
        continue;
    if (scheduled.Cycles.Value > 0)
        Console.WriteLine($"{candidate.Id} is cyclic, leveled: {candidate.Leveled is not null}");
    foreach (var task in scheduled.Finish.Tasks)
    {
        if (task.Finish is null)
            Console.WriteLine($"{candidate.Id} removes focus task {task.Id}");
    }
    Console.WriteLine($"{candidate.Id} cost {candidate.Cost}");
}

// Tasks A and B, two days each, both on dev1 in the same week.
static Project SolvedProject()
{
    var monday = Moment.FromYmdHm(2026, 8, 10, 8, 0) ?? throw new InvalidOperationException("invalid date");
    var data = new ProjectData
    {
        Tasks =
        [
            new TaskData("A") { StartDate = monday, Duration = 2.0 },
            new TaskData("B") { StartDate = monday, Duration = 2.0 },
        ],
        Resources = [new ResourceData { Id = "dev1" }, new ResourceData { Id = "dev2" }],
        Assignments =
        [
            new AssignmentData { Id = "aA", Task = "A", Resource = "dev1", Value = 1.0 },
            new AssignmentData { Id = "aB", Task = "B", Resource = "dev1", Value = 1.0 },
        ],
    };
    var project = new Project();
    project.Solve(data, new SolveOptions());
    return project;
}

static SchedulingChanges Reassign(Id assignment, Id resource)
{
    var changes = new SchedulingChanges();
    changes.Assignments.Update.Add(new AssignmentUpdate(assignment) { Resource = resource });
    return changes;
}
```

```text
missing rejected at assignments.update[0]: unknown assignment id
cycle is cyclic, leveled: False
cycle cost 1002
removed removes focus task B
removed cost 0
```

Rank only candidates that have a `Cost`. The cyclic candidate has one, but the penalty keeps it at the bottom. Its 1002 is the cycle penalty plus the two resource-days of overload `dev1` still has.

Call-level errors still throw. An unknown focus id fails the whole call with [`InputException`](<https://docs.dhtmlx.com/planning-engine/net/api/core/errors/input-exception.md>) at path `focus[i]`, and a stale token with [`ProjectException`](<https://docs.dhtmlx.com/planning-engine/net/api/core/errors/project-exception.md>) and [`ProjectError.StaleBaseline`](<https://docs.dhtmlx.com/planning-engine/net/api/core/errors/project-exception.md#projecterror>). See [Error handling](<https://docs.dhtmlx.com/planning-engine/net/error-handling.md>).

### Parallel evaluation

Each candidate runs on its own copy, so candidates are independent. On a multi-core host, [`Project.EvaluateWith`](<https://docs.dhtmlx.com/planning-engine/net/api/project-evaluate.md#evaluatewith>) spreads them over threads:

```csharp
using Plen;

var project = SolvedProject();
// One candidate per resource that could take task B.
var candidates = new List<Candidate>();
foreach (var dev in new[] { "dev1", "dev2" })
    candidates.Add(new Candidate { Id = dev, Changes = Reassign("aB", dev) });
var options = new EvaluateOptions();

var result = project.EvaluateWith(candidates, options, Execution.Parallel(4));

// Same results, same order as project.Evaluate(candidates, options).
foreach (var candidate in result.Candidates)
    Console.WriteLine($"{candidate.Id}: cost {candidate.Cost}");

// Tasks A and B, two days each, both on dev1 in the same week.
static Project SolvedProject()
{
    var monday = Moment.FromYmdHm(2026, 8, 10, 8, 0) ?? throw new InvalidOperationException("invalid date");
    var data = new ProjectData
    {
        Tasks =
        [
            new TaskData("A") { StartDate = monday, Duration = 2.0 },
            new TaskData("B") { StartDate = monday, Duration = 2.0 },
        ],
        Resources = [new ResourceData { Id = "dev1" }, new ResourceData { Id = "dev2" }],
        Assignments =
        [
            new AssignmentData { Id = "aA", Task = "A", Resource = "dev1", Value = 1.0 },
            new AssignmentData { Id = "aB", Task = "B", Resource = "dev1", Value = 1.0 },
        ],
    };
    var project = new Project();
    project.Solve(data, new SolveOptions());
    return project;
}

static SchedulingChanges Reassign(Id assignment, Id resource)
{
    var changes = new SchedulingChanges();
    changes.Assignments.Update.Add(new AssignmentUpdate(assignment) { Resource = resource });
    return changes;
}
```

```text
dev1: cost 2
dev2: cost 0
```

[`Execution.Parallel()`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/execution.md#parallel>) with no argument uses `Environment.ProcessorCount` threads. A count of 0 or less throws `ArgumentOutOfRangeException`. `Execution.Sequential`, the default value of the struct, is what [`Evaluate`](<https://docs.dhtmlx.com/planning-engine/net/api/project-evaluate.md>) does. The candidates run through `Parallel.For` with the thread count as the maximum degree of parallelism, so peak memory is one candidate copy per worker.

The results are identical to the sequential run and keep input order. If the call fails, you get the same exception as in a sequential run: the first one in input order. Later candidates may already have run.

`EvaluateWith` blocks the calling thread until every candidate is scored, and so does `Evaluate`. Every call in the library is synchronous and CPU-bound. In ASP.NET Core or another async host, run large evaluations with `Task.Run` or on a background worker. See [Call from async code](<https://docs.dhtmlx.com/planning-engine/net/how-to/call-from-async-code.md>).

A [`Project`](<https://docs.dhtmlx.com/planning-engine/net/api/core/project/project.md>) isn't synchronized, and `Evaluate` with `ClearFirst` updates its internal copy-on-write state. Don't call it on one instance from several threads at once. `EvaluateWith` is the safe way to use several cores on one project. For other cases, see [Share a project between threads](<https://docs.dhtmlx.com/planning-engine/net/how-to/share-a-project-between-threads.md>).

### Committing the winner

[`Evaluate`](<https://docs.dhtmlx.com/planning-engine/net/api/project-evaluate.md>) never changes the project. To commit the chosen candidate, run its changes for real: [`Apply`](<https://docs.dhtmlx.com/planning-engine/net/api/project-apply.md>) the same change set, then call [`Level`](<https://docs.dhtmlx.com/planning-engine/net/api/project-level.md>) with the same [`LevelingOptions`](<https://docs.dhtmlx.com/planning-engine/net/api/core/resources/leveling-options.md>), [`Step`](<https://docs.dhtmlx.com/planning-engine/net/api/core/resources/leveling-options.md#levelingstep>) included. With other options, the committed plan isn't the one that was scored. If you scored without `Level`, follow `Apply` with [`Solve(null, …)`](<https://docs.dhtmlx.com/planning-engine/net/api/project-solve.md>) instead.

```csharp
using Plen;

var project = SolvedProject();
Candidate[] candidates =
[
    new() { Id = "stay", Changes = new SchedulingChanges() },
    new() { Id = "dev2", Changes = Reassign("aB", "dev2") },
];
var leveling = new LevelingOptions();
var result = project.Evaluate(candidates, new EvaluateOptions { Level = leveling });

// Lowest cost wins; rejected candidates have no cost.
Candidate? winner = null;
double best = double.PositiveInfinity;
for (int i = 0; i < candidates.Length; i++)
{
    if (result.Candidates[i].Cost is { } cost && cost < best)
        (winner, best) = (candidates[i], cost);
}

if (winner is not null)
{
    var changes = winner.Changes.DeepClone();
    // Apply throws StaleBaseline if the project changed since the evaluation.
    changes.BaselineId = result.BaselineId;
    var report = project.Apply(changes);
    if (report.Rejected.Count == 0)
    {
        // The same options that scored the candidate.
        project.Level(leveling);
        Console.WriteLine($"committed {winner.Id} (cost {best})");
    }
}

// Tasks A and B, two days each, both on dev1 in the same week.
static Project SolvedProject()
{
    var monday = Moment.FromYmdHm(2026, 8, 10, 8, 0) ?? throw new InvalidOperationException("invalid date");
    var data = new ProjectData
    {
        Tasks =
        [
            new TaskData("A") { StartDate = monday, Duration = 2.0 },
            new TaskData("B") { StartDate = monday, Duration = 2.0 },
        ],
        Resources = [new ResourceData { Id = "dev1" }, new ResourceData { Id = "dev2" }],
        Assignments =
        [
            new AssignmentData { Id = "aA", Task = "A", Resource = "dev1", Value = 1.0 },
            new AssignmentData { Id = "aB", Task = "B", Resource = "dev1", Value = 1.0 },
        ],
    };
    var project = new Project();
    project.Solve(data, new SolveOptions());
    return project;
}

static SchedulingChanges Reassign(Id assignment, Id resource)
{
    var changes = new SchedulingChanges();
    changes.Assignments.Update.Add(new AssignmentUpdate(assignment) { Resource = resource });
    return changes;
}
```

```text
committed dev2 (cost 0)
```

Results come back in input order, so index `i` pairs each score with its change set. `DeepClone()` copies the change set before you set its token, so the candidate list stays as it was scored. Setting [`BaselineId`](<https://docs.dhtmlx.com/planning-engine/net/api/core/changes/scheduling-changes.md#baselineid>) to the token from the result makes `Apply` refuse the commit when another edit landed in between. In that case, evaluate again.

If you scored with [`EvaluateOptions.ClearFirst`](<https://docs.dhtmlx.com/planning-engine/net/api/core/evaluate/evaluate-options.md#clearfirst>), commit with `Level` and `LevelingOptions.ClearFirst` (the default) so the commit re-levels from zero, like the scoring did.

To handle a rejected `Apply`, see [Recover from rejected changes](<https://docs.dhtmlx.com/planning-engine/net/how-to/recover-from-rejected-changes.md>). To generate candidates automatically instead of writing them by hand, use [`SuggestAssignments`](<https://docs.dhtmlx.com/planning-engine/net/api/project-suggest-assignments.md>), which scores its steps the same way. See [Suggesting assignments](<https://docs.dhtmlx.com/planning-engine/net/resources-and-assignment/suggesting-assignments.md>).
