void ComputeAndShowCamera( )
    {
        CLViewpoint result = psoSolver.SearchOptimal(solverTime, 0.999f, camLibCam, new List <CLCandidate> (), false, true);

        Debug.Log("Satisfaction after " + psoSolver.iterations + " iterations: " + result.satisfaction [0] +
                  ", best iteration: " + psoSolver.iterOfBest);

        // update camera with found solution
        camLibCam.updateCamera(result.psoRepresentation);
    }
    protected void StoreNewGlobalLeader(int leader)
    {
        CLViewpoint newLeaderViewpoint = new CLViewpoint();
        CLCandidate leaderCandidate    = new CLCandidate(candidateDimension);

        leaderCandidate.bestEvaluation = candidates [leader].bestEvaluation;
        leaderCandidate.bestPosition   = candidates [leader].bestPosition;
        leaderCandidate.evaluation     = candidates [leader].evaluation;
        leaderCandidate.inSearchSpace  = candidates [leader].inSearchSpace;
        leaderCandidate.leader         = candidates [leader].leader;

        newLeaderViewpoint.psoRepresentation = leaderCandidate.bestPosition;
        newLeaderViewpoint.properties        = new List <CLVisualProperty> (evaluator.properties);
        newLeaderViewpoint.satisfaction      = new List <float> (evaluator.properties.Count);
        newLeaderViewpoint.inScreenRatio     = new List <float> (evaluator.properties.Count);
        foreach (CLVisualProperty p in newLeaderViewpoint.properties)
        {
            newLeaderViewpoint.satisfaction.Add(p.satisfaction);
            newLeaderViewpoint.inScreenRatio.Add(p.inScreenRatio);
        }


        globalBestViewpoints.Add(newLeaderViewpoint);
    }
    //// <summary>
    /// Returns the best found CLViewpoint given the allowed time in milliseconds,
    /// the required satisfaction threshold in [0,1], a CLCameraMan for evaluating a
    /// candidate satisfaction. If init is true, we start search from scratch, i.e. by
    /// first initializing candidates; otherwise, we use the current candidates
    /// </summary>
    public CLViewpoint SearchOptimal(float _timeLimit, float _satisfactionThreshold, CLCameraMan _evaluator, List <CLCandidate> initialCandidates, bool checkGeometry = false, bool init = true)
    {
        //if init, initialize n candidates ( with r_part candidates randomly initialized )
        beginTime   = Time.realtimeSinceStartup;
        elapsedTime = 0.0f;
        timeLimit   = _timeLimit / 1000.0f;

        if (init)
        {
            rnd                  = new System.Random();
            iterations           = 0;
            iterOfBest           = 0;
            this.evaluator       = _evaluator;
            this.maxSatisfaction = _satisfactionThreshold;
            globalBestViewpoints = new List <CLViewpoint> ();
            minSearchRange       = evaluator.GetMinCameraParameters(candidateDimension);
            maxSearchRange       = evaluator.GetMaxCameraParameters(candidateDimension);
            searchRanges         = evaluator.GetParametersRange(candidateDimension);
            averageRange         = 0.0f;
            for (int i = 0; i < candidateDimension; i++)
            {
                averageRange += searchRanges[i];
            }
            averageRange = averageRange / candidateDimension;
            InitializeCandidates(initialCandidates);
            InitSolverParameters(_timeLimit);
            elapsedTime = Time.realtimeSinceStartup - beginTime;
        }

        if (elapsedTime > timeLimit)
        {
            exitCondition = 0;
        }
        else
        {
            exitCondition = 2;
        }

        while (DoAnotherIteration())
        {
            iterations++;             // we start from 1

            //  execute loop body (dependent on method).
            exitCondition = ExecuteSearchIteration();
        }

        //elaborate results and return best solution (access to other solutions should be provided)

        // this is for handling the case where no optima have been found at all (e.g. not enough time)
        if (globalBestViewpoints.Count == 0)
        {
            CLViewpoint noSolution = new CLViewpoint();
            noSolution.satisfaction = new List <float>();
            noSolution.properties   = new List <CLVisualProperty> (evaluator.properties);
            foreach (CLVisualProperty p in noSolution.properties)
            {
                noSolution.satisfaction.Add(-1.0f);
            }
            noSolution.psoRepresentation = new float[] { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 60.0f };
            globalBestViewpoints.Add(noSolution);
        }

        // this returns only one solution !!!!
        return(globalBestViewpoints[globalBestViewpoints.Count - 1]);
    }