---
description: Controls for functions related to state
---

# StateControl

### GetIntitialState:star:

```csharp
public override State GetInitialState()
```

#### Definition

Define the initial state.

#### Return

<table><thead><tr><th width="167">Type</th><th width="369">Description</th><th>Default</th></tr></thead><tbody><tr><td><a href="../data-model/state"><code>State</code></a></td><td>The initial state.</td><td>Null</td></tr></tbody></table>

#### Example <a href="#initialstate" id="initialstate"></a>

```csharp
public override State GetInitialState()
{
    CRPData data = DataManager.Instance.Data as CRPData;
    CRPState initState = new CRPState();

    initState.SetStateInfo(data.CRPFactory.Conveyors);

    return initState;
}
```

### GetKey

```csharp
public override string GetKey(State state)
```

#### Definition

Defines the key for the state.

#### Parameters

| Type                           | Description                  |
| ------------------------------ | ---------------------------- |
| [`State`](../data-model/state) | The state to define the key. |

#### Return

| Type   | Description           | Default                         |
| ------ | --------------------- | ------------------------------- |
| String | The key of the state. | [Default Logic](#default-logic) |

#### Example (Default Logic)

```csharp
public virtual string GetKey(State state)
{
    return state.Index.ToString();
}
```

### GetFeasibleSolution

```csharp
public override Solution GetFeasibleSolution(State state)
```

#### Definition

Define the logic to find a feasible solution based on a given State.

#### Parameters

| Type                           | Description                            |
| ------------------------------ | -------------------------------------- |
| [`State`](../data-model/state) | The state to find a feasible solution. |

#### Return

| Type     | Description            | Default |
| -------- | ---------------------- | ------- |
| Solution | The feasible solution. | Null    |

#### Example

```csharp
public override Solution GetFeasibleSolution(State state)
{           
    CRPState crpState = state as CRPState;
    CRPState copiedState = crpState.Clone();
    copiedState.IsInitial = false;

    List<CRPState> states = new List<CRPState>();
    states.AddRange(copiedState.GetBestStatesBackward().Cast<CRPState>().ToList());

    while (copiedState.JobCount > 0)
    {
        Stage stage = new Stage(copiedState.Stage.Index + 1);
        copiedState.Stage = stage;

        CRPJob lastJob = copiedState.LastRetrievedJob;

        CRPConveyor sameColorConv = copiedState.GetSameColorConveyor(lastJob);

        CRPJob retrievedJob = null;
        if (sameColorConv != null)
        {
            retrievedJob = copiedState.RetrieveJob(sameColorConv.ConveyorNum);
        }
        else 
        {
            retrievedJob = copiedState.RetrieveJob();
        }

        double cost = 0;
        if (lastJob != null && lastJob.Color.ColorNumber != retrievedJob.Color.ColorNumber)
            cost = 1;

        copiedState.BestValue += cost;

        states.Add(copiedState);
        copiedState = copiedState.Clone();
    }

    Solution feasibleSol = new Solution(states);

    return feasibleSol;
}
```

### CanPruneByOptimality

```csharp
public override bool CanPruneByOptimality(State state, ObjectiveFunctionType objFuncType, double pruneTolerance)
```

#### Definition

Determines whether a given state can be pruned without loss of optimality. If the state satisfies the prune conditions, it is excluded.

#### Parameters

<table><thead><tr><th width="234">Type</th><th width="513">Description</th></tr></thead><tbody><tr><td><a href="../data-model/state"><code>State</code></a></td><td>The state to be judged.</td></tr><tr><td>ObjectiveFunctionType</td><td>The type of objective function in the problem.</td></tr><tr><td>Double</td><td>The tolerance used for the comparison condition when determining the optimal condition.</td></tr></tbody></table>

<table><thead><tr><th width="115">Type</th><th width="392">Description</th><th>Default</th></tr></thead><tbody><tr><td>Boolean</td><td>True: Prunes the state.<br>False: Do not prune the state.</td><td><a href="#default-logic-1">Default Logic</a></td></tr></tbody></table>

#### Example (Default Logic)

```csharp
public override bool CanPruneByOptimality(State state, ObjectiveFunctionType objFuncType, double pruneTolerance)
{
    BoundManager boundManager = BoundManager.Instance;

    double bestPrimalBound = boundManager.BestPrimalBound;
    double dualBound = state.DualBound;
    double bestValue = state.BestValue;

    double rootDualBound = boundManager.RootDualBound;

    if (objFuncType == ObjectiveFunctionType.Minimize)
    {
        if (dualBound < rootDualBound - bestValue)
            dualBound = rootDualBound - bestValue;

        if (bestPrimalBound + pruneTolerance <= bestValue + dualBound)
            return true;
        else
            return false;
    }
    else
    {
        if (dualBound > rootDualBound - bestValue)
            dualBound = rootDualBound - bestValue;

        if (bestPrimalBound >= bestValue + dualBound + pruneTolerance)
            return true;
        else
            return false;
    }
}
```
